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

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: Histogram 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/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10
11 namespace metrics {
12
13 namespace {
14
15 enum IntervalType {
16 FIRST_RUN,
17 DAY_ELAPSED,
18 CLOCK_CHANGED,
19 NUM_INTERVAL_TYPES
20 };
21
22 void RecordIntervalTypeHistogram(const char* pref_name, IntervalType t) {
23 std::string metric_name("DailyInterval.IntervalType.");
24 metric_name += pref_name;
25 base::Histogram::FactoryGet(
26 metric_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, const char* pref_name)
36 : pref_service_(pref_service), pref_name_(pref_name) {
37 }
38
39 DailyInterval::~DailyInterval() {
40 }
41
42 // static
43 void DailyInterval::RegisterPref(PrefRegistrySimple* registry,
44 const char* pref_name) {
45 registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue());
46 }
47
48 void DailyInterval::AddObserver(DailyObserver* observer) {
49 DVLOG(2) << "DailyInterval observer added.";
50 DCHECK(last_fired_.is_null());
51 observers_.AddObserver(observer);
52 }
53
54 void DailyInterval::CheckInterval() {
55 base::Time now = base::Time::Now();
56 if (last_fired_.is_null()) {
57 // The first time we call CheckInterval, we read the time stored in prefs.
58 last_fired_ = base::Time::FromInternalValue(
59 pref_service_->GetInt64(pref_name_));
60 DVLOG(1) << "DailyInterval time loaded: "
61 << base::TimeFormatShortDateAndTime(last_fired_);
62 if (last_fired_.is_null()) {
63 DVLOG(1) << "DailyInterval first run.";
64 RecordIntervalTypeHistogram(pref_name_, FIRST_RUN);
65 OnInterval(now);
66 return;
67 }
68 }
69 int days_elapsed = (now - last_fired_).InDays();
70 if (days_elapsed > 1) {
71 DVLOG(1) << "DailyInterval day elapsed.";
72 RecordIntervalTypeHistogram(pref_name_, DAY_ELAPSED);
73 OnInterval(now);
74 } else if (days_elapsed <= -1) {
75 // The "last fired" time is more than a day in the future, so the clock
76 // must have been changed.
77 DVLOG(1) << "DailyInterval clock change detected.";
78 RecordIntervalTypeHistogram(pref_name_, CLOCK_CHANGED);
79 OnInterval(now);
80 }
81 }
82
83 void DailyInterval::OnInterval(base::Time now) {
84 DCHECK(!now.is_null());
85 last_fired_ = now;
86 pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue());
87 FOR_EACH_OBSERVER(
88 DailyObserver, observers_, OnDailyInterval());
Alexei Svitkine (slow) 2014/09/05 20:00:33 Nit: Does this need to wrap?
Steven Holte 2014/09/05 21:15:07 Done.
89 }
90
91 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698