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

Side by Side Diff: components/metrics/daily_interval.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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/daily_interval.h"
6
7 #include "base/i18n/time_formatting.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11
12 namespace metrics {
13
14 namespace {
15
16 enum IntervalType {
17 FIRST_RUN,
18 DAY_ELAPSED,
19 CLOCK_CHANGED,
20 NUM_INTERVAL_TYPES
21 };
22
23 void RecordIntervalTypeHistogram(const std::string& histogram_name,
24 IntervalType t) {
25 base::Histogram::FactoryGet(
26 histogram_name,
27 1,
28 NUM_INTERVAL_TYPES,
29 NUM_INTERVAL_TYPES + 1,
30 base::HistogramBase::kUmaTargetedHistogramFlag)->Add(t);
31 }
32
33 } // namespace
34
35 DailyInterval::DailyInterval(PrefService* pref_service,
36 const char* pref_name,
37 const std::string& histogram_name)
38 : pref_service_(pref_service),
39 pref_name_(pref_name),
40 histogram_name_(histogram_name) {
41 }
42
43 DailyInterval::~DailyInterval() {
44 }
45
46 // static
47 void DailyInterval::RegisterPref(PrefRegistrySimple* registry,
48 const char* pref_name) {
49 registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue());
50 }
51
52 void DailyInterval::AddObserver(DailyObserver* observer) {
53 DVLOG(2) << "DailyInterval observer added.";
54 DCHECK(last_fired_.is_null());
55 observers_.push_back(observer);
56 }
57
58 void DailyInterval::CheckInterval() {
59 base::Time now = base::Time::Now();
60 if (last_fired_.is_null()) {
61 // The first time we call CheckInterval, we read the time stored in prefs.
62 last_fired_ = base::Time::FromInternalValue(
63 pref_service_->GetInt64(pref_name_));
64 DVLOG(1) << "DailyInterval time loaded: "
65 << base::TimeFormatShortDateAndTime(last_fired_);
66 if (last_fired_.is_null()) {
67 DVLOG(1) << "DailyInterval first run.";
68 RecordIntervalTypeHistogram(pref_name_, FIRST_RUN);
69 OnInterval(now);
70 return;
71 }
72 }
73 int days_elapsed = (now - last_fired_).InDays();
74 if (days_elapsed >= 1) {
75 DVLOG(1) << "DailyInterval day elapsed.";
76 RecordIntervalTypeHistogram(pref_name_, DAY_ELAPSED);
77 OnInterval(now);
78 } else if (days_elapsed <= -1) {
79 // The "last fired" time is more than a day in the future, so the clock
80 // must have been changed.
81 DVLOG(1) << "DailyInterval clock change detected.";
82 RecordIntervalTypeHistogram(pref_name_, CLOCK_CHANGED);
83 OnInterval(now);
84 }
85 }
86
87 void DailyInterval::OnInterval(base::Time now) {
88 DCHECK(!now.is_null());
89 last_fired_ = now;
90 pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue());
91
92 // Notify all observers
93 for (ScopedVector<DailyObserver>::iterator it = observers_.begin();
94 it != observers_.end();
95 ++it) {
96 (*it)->OnDailyInterval();
97 }
98 }
99
100 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698