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

Unified Diff: components/metrics/daily_event.cc

Issue 511623002: Add a mechanism for collecting Rappor samples on a daily interval. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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
« no previous file with comments | « components/metrics/daily_event.h ('k') | components/metrics/daily_event_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/metrics/daily_event.cc
diff --git a/components/metrics/daily_event.cc b/components/metrics/daily_event.cc
new file mode 100644
index 0000000000000000000000000000000000000000..34c34265a3bba1104018a1ee85e94ab269292f32
--- /dev/null
+++ b/components/metrics/daily_event.cc
@@ -0,0 +1,106 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/metrics/daily_event.h"
+
+#include "base/i18n/time_formatting.h"
+#include "base/metrics/histogram.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+
+namespace metrics {
+
+namespace {
+
+enum IntervalType {
+ FIRST_RUN,
+ DAY_ELAPSED,
+ CLOCK_CHANGED,
+ NUM_INTERVAL_TYPES
+};
+
+void RecordIntervalTypeHistogram(const std::string& histogram_name,
+ IntervalType type) {
+ base::Histogram::FactoryGet(
+ histogram_name,
+ 1,
+ NUM_INTERVAL_TYPES,
+ NUM_INTERVAL_TYPES + 1,
+ base::HistogramBase::kUmaTargetedHistogramFlag)->Add(type);
+}
+
+} // namespace
+
+DailyEvent::Observer::Observer() {
+}
+
+DailyEvent::Observer::~Observer() {
+}
+
+DailyEvent::DailyEvent(PrefService* pref_service,
+ const char* pref_name,
+ const std::string& histogram_name)
+ : pref_service_(pref_service),
+ pref_name_(pref_name),
+ histogram_name_(histogram_name) {
+}
+
+DailyEvent::~DailyEvent() {
+}
+
+// static
+void DailyEvent::RegisterPref(PrefRegistrySimple* registry,
+ const char* pref_name) {
+ registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue());
+}
+
+void DailyEvent::AddObserver(scoped_ptr<DailyEvent::Observer> observer) {
+ DVLOG(2) << "DailyEvent observer added.";
+ DCHECK(last_fired_.is_null());
+ observers_.push_back(observer.release());
+}
+
+void DailyEvent::CheckInterval() {
+ base::Time now = base::Time::Now();
+ if (last_fired_.is_null()) {
+ // The first time we call CheckInterval, we read the time stored in prefs.
+ last_fired_ = base::Time::FromInternalValue(
+ pref_service_->GetInt64(pref_name_));
+ DVLOG(1) << "DailyEvent time loaded: "
+ << base::TimeFormatShortDateAndTime(last_fired_);
+ if (last_fired_.is_null()) {
+ DVLOG(1) << "DailyEvent first run.";
+ RecordIntervalTypeHistogram(pref_name_, FIRST_RUN);
+ OnInterval(now);
+ return;
+ }
+ }
+ int days_elapsed = (now - last_fired_).InDays();
+ if (days_elapsed >= 1) {
+ DVLOG(1) << "DailyEvent day elapsed.";
+ RecordIntervalTypeHistogram(pref_name_, DAY_ELAPSED);
+ OnInterval(now);
+ } else if (days_elapsed <= -1) {
+ // The "last fired" time is more than a day in the future, so the clock
+ // must have been changed.
+ DVLOG(1) << "DailyEvent clock change detected.";
+ RecordIntervalTypeHistogram(pref_name_, CLOCK_CHANGED);
+ OnInterval(now);
+ }
+}
+
+void DailyEvent::OnInterval(base::Time now) {
+ DCHECK(!now.is_null());
+ last_fired_ = now;
+ pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue());
+
+ // Notify all observers
+ for (ScopedVector<DailyEvent::Observer>::iterator it = observers_.begin();
+ it != observers_.end();
+ ++it) {
+ (*it)->OnDailyEvent();
+ }
+}
+
+} // namespace metrics
« no previous file with comments | « components/metrics/daily_event.h ('k') | components/metrics/daily_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698