Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Unified Diff: components/metrics/persisted_logs.cc

Issue 2358223002: Add log date to the metrics log (Closed)
Patch Set: Adding log date in persisted logs Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/metrics/persisted_logs.cc
diff --git a/components/metrics/persisted_logs.cc b/components/metrics/persisted_logs.cc
index 52292da55ccd5d18f99e30898f2424502e8163ef..d4e5e3dbcdabf674019beb9ac46da31cf5defcc9 100644
--- a/components/metrics/persisted_logs.cc
+++ b/components/metrics/persisted_logs.cc
@@ -18,6 +18,9 @@
namespace metrics {
namespace {
Alexei Svitkine (slow) 2016/09/28 18:50:30 Nit: add line after
gayane -on leave until 09-2017 2016/09/28 20:50:50 Done.
+const char kLogHashKey[] = "hash";
+const char kLogTimestampKey[] = "timestamp";
+const char kLogDataKey[] = "data";
PersistedLogs::LogReadStatus MakeRecallStatusHistogram(
PersistedLogs::LogReadStatus status) {
@@ -37,16 +40,21 @@ bool ReadBase64String(const base::ListValue& list_value,
return base::Base64Decode(base64_result, result);
}
-// Base64-encodes |str| and appends the result to |list_value|.
-void AppendBase64String(const std::string& str, base::ListValue* list_value) {
- std::string base64_str;
- base::Base64Encode(str, &base64_str);
- list_value->AppendString(base64_str);
+std::string EncodeToBase64(const std::string& to_convert) {
+ std::string base64_result;
+ base::Base64Encode(to_convert, &base64_result);
+ return base64_result;
+}
+
+std::string DecodeFromBase64(const std::string& to_convert) {
+ std::string result;
+ base::Base64Decode(to_convert, &result);
+ return result;
}
} // namespace
-void PersistedLogs::LogHashPair::Init(const std::string& log_data) {
+void PersistedLogs::LogInfo::Init(const std::string& log_data) {
DCHECK(!log_data.empty());
if (!compression::GzipCompress(log_data, &compressed_log_data)) {
@@ -59,6 +67,7 @@ void PersistedLogs::LogHashPair::Init(const std::string& log_data) {
static_cast<int>(100 * compressed_log_data.size() / log_data.size()));
hash = base::SHA1HashString(log_data);
+ timestamp = (base::Time::Now() - base::Time::UnixEpoch()).InSeconds();
Alexei Svitkine (slow) 2016/09/28 18:50:30 Isn't return value int64_t but your field is an in
gayane -on leave until 09-2017 2016/09/28 20:50:49 hmm right. I had a look and dictionaries have only
Alexei Svitkine (slow) 2016/09/29 16:46:40 That's a 100 years since unix epoch? So 56 years a
}
PersistedLogs::PersistedLogs(PrefService* local_state,
@@ -89,7 +98,7 @@ PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() {
}
void PersistedLogs::StoreLog(const std::string& log_data) {
- list_.push_back(LogHashPair());
+ list_.push_back(LogInfo());
list_.back().Init(log_data);
}
@@ -109,7 +118,46 @@ void PersistedLogs::DiscardStagedLog() {
staged_log_index_ = -1;
}
-void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const {
+void PersistedLogs::MigrateFromOldFormat(const char* old_pref_name) {
+ ReadLogsFromPrefList_OldFormat(*local_state_->GetList(old_pref_name));
+ ListPrefUpdate update(local_state_, pref_name_);
+ WriteLogsToPrefList(update.Get());
Alexei Svitkine (slow) 2016/09/28 18:50:30 I don't see where WriteLogsToPrefList is implement
gayane -on leave until 09-2017 2016/09/28 20:50:50 opps deleted that instead of WriteLogsToPrefList_O
+}
Alexei Svitkine (slow) 2016/09/28 18:50:30 I'm not sure we need an explicit migrate function.
gayane -on leave until 09-2017 2016/09/28 20:50:49 But then when we read we need to read from two dif
Alexei Svitkine (slow) 2016/09/29 16:46:40 Well, you're already reading from both prefs - jus
+
+PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList(
+ const base::ListValue& list_value) {
+ if (list_value.empty())
+ return MakeRecallStatusHistogram(LIST_EMPTY);
+
+ const size_t log_count = list_value.GetSize();
+
+ DCHECK(list_.empty());
+ list_.resize(log_count);
+
+ for (size_t i = 0; i < log_count; ++i) {
+ const base::DictionaryValue* dict;
+ if (!list_value.GetDictionary(i, &dict) ||
+ !dict->GetString(kLogDataKey, &list_[i].compressed_log_data) ||
+ !dict->GetString(kLogHashKey, &list_[i].hash)) {
+ list_.clear();
+ return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION);
+ }
+
+ list_[i].compressed_log_data =
+ DecodeFromBase64(list_[i].compressed_log_data);
+ list_[i].hash = DecodeFromBase64(list_[i].hash);
+ // Ignoring the success of this step as timestamp might not be there for
+ // older logs.
+ // NOTE: Should be added to the check with other fields once migration is
+ // over.
+ dict->GetInteger(kLogTimestampKey, &list_[i].timestamp);
+ }
+
+ return MakeRecallStatusHistogram(RECALL_SUCCESS);
+}
+
+void PersistedLogs::WriteLogsToPrefList_OldFormat(
Alexei Svitkine (slow) 2016/09/28 18:50:30 Do we need this anymore?
gayane -on leave until 09-2017 2016/09/28 20:50:49 Nope
+ base::ListValue* list_value) const {
list_value->Clear();
// Keep the most recent logs which are smaller than |max_log_size_|.
@@ -147,7 +195,7 @@ void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const {
UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num);
}
-PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList(
+PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList_OldFormat(
Alexei Svitkine (slow) 2016/09/28 18:50:30 Nit: ReadLogsFromOldFormatPrefList
gayane -on leave until 09-2017 2016/09/28 20:50:49 Done.
const base::ListValue& list_value) {
if (list_value.empty())
return MakeRecallStatusHistogram(LIST_EMPTY);

Powered by Google App Engine
This is Rietveld 408576698