OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "chrome/installer/util/experiment.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 | |
10 #include "base/logging.h" | |
11 | |
12 namespace installer { | |
13 | |
14 namespace { | |
15 | |
16 // Returns closest integer of logarithm of |x| with base |b|. | |
17 double LogFloor(double x, double b) { | |
18 return std::round(std::log(x) / std::log(b)); | |
19 } | |
20 | |
21 // Returns the base to use for exponential buckets so that buckets | |
22 // 0,1,.. 2^|bits|-1 cover range [0, max_val]. If this function return b | |
23 // then Bucket value i will store values from [b^i, b^(i+1)] | |
24 double ExpBucketBase(int max_val, int bits) { | |
25 return std::exp(std::log(max_val + 1) / ((1 << bits) - 1)); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 Experiment::Experiment() = default; | |
31 Experiment::Experiment(Experiment&&) = default; | |
32 Experiment::Experiment(const Experiment&) = default; | |
33 Experiment::~Experiment() = default; | |
34 | |
35 void Experiment::InitializeFromMetrics(const ExperimentMetrics& metrics) { | |
36 *this = Experiment(); | |
37 DCHECK(metrics.InInitialState() || | |
38 metrics.state == ExperimentMetrics::kGroupAssigned); | |
39 if (metrics.state == ExperimentMetrics::kUninitialized) { | |
40 // Ignore uninitialized metrics. | |
grt (UTC plus 2)
2017/06/09 14:04:22
it shouldn't ignore, but rather make the state of
nikunjb
2017/06/13 00:27:35
Done.
One point to note regarding kUninitialized
grt (UTC plus 2)
2017/06/13 12:24:48
Right. We should never store uninitialized metrics
| |
41 return; | |
42 } | |
43 metrics_ = metrics; | |
44 state_ = metrics.state; | |
45 group_ = metrics.group; | |
46 } | |
47 | |
48 void Experiment::SetState(ExperimentMetrics::State state) { | |
49 DCHECK_NE(ExperimentMetrics::kUninitialized, state); | |
50 state_ = state; | |
51 metrics_.state = state; | |
52 } | |
53 | |
54 void Experiment::AssignGroup(int group) { | |
55 DCHECK_GE(group, 0); | |
56 DCHECK_LT(group, ExperimentMetrics::kNumGroups); | |
57 DCHECK(metrics_.InInitialState()); | |
58 | |
59 group_ = group; | |
60 metrics_.group = group; | |
61 SetState(ExperimentMetrics::kGroupAssigned); | |
62 } | |
63 | |
64 void Experiment::SetToastLocation(ExperimentMetrics::ToastLocation location) { | |
65 DCHECK(!metrics_.InTerminalState()); | |
66 DCHECK(!metrics_.InInitialState()); | |
67 toast_location_ = location; | |
68 metrics_.toast_location = location; | |
69 } | |
70 | |
71 void Experiment::SetInactiveDays(int days) { | |
72 DCHECK(!metrics_.InTerminalState()); | |
73 DCHECK(!metrics_.InInitialState()); | |
74 DCHECK_GE(days, 0); | |
75 inactive_days_ = days; | |
76 double log_base = ExpBucketBase(ExperimentMetrics::kMaxLastUsed, | |
77 ExperimentMetrics::kLastUsedBucketBits); | |
78 metrics_.last_used_bucket = | |
79 LogFloor(1 + std::min(days, ExperimentMetrics::kMaxLastUsed), log_base); | |
80 } | |
81 | |
82 void Experiment::SetToastCount(int count) { | |
83 DCHECK(!metrics_.InTerminalState()); | |
84 DCHECK(!metrics_.InInitialState()); | |
85 toast_count_ = count; | |
86 metrics_.toast_count = std::min(count, ExperimentMetrics::kMaxToastCount); | |
87 } | |
88 | |
89 void Experiment::SetDisplayTime(base::Time time) { | |
90 DCHECK(!metrics_.InTerminalState()); | |
91 DCHECK(!metrics_.InInitialState()); | |
92 if (metrics_.first_toast_offset == 0) { | |
93 // This is the first time toast is shown so add user to today's cohort. | |
94 first_display_time_ = time; | |
95 metrics_.first_toast_offset = | |
96 (time - base::Time::UnixEpoch() - | |
97 base::TimeDelta::FromSeconds( | |
98 ExperimentMetrics::kExperimentStartSeconds)) | |
99 .InDays(); | |
100 // If display time is outside the experiment range (possible due to | |
101 // invalid local time), then set it to be kMaxFirstToastOffset. | |
102 if (metrics_.first_toast_offset < 0) { | |
103 metrics_.first_toast_offset = ExperimentMetrics::kMaxFirstToastOffset; | |
104 } else { | |
105 metrics_.first_toast_offset = std::min( | |
106 metrics_.first_toast_offset, ExperimentMetrics::kMaxFirstToastOffset); | |
107 } | |
108 } | |
109 latest_display_time_ = time; | |
110 metrics_.toast_hour = (time - time.LocalMidnight()).InHours(); | |
111 DCHECK_LE(metrics_.toast_hour, 24); | |
112 DCHECK_GE(metrics_.toast_hour, 0); | |
113 } | |
114 | |
115 void Experiment::SetUserSessionUptime(base::TimeDelta time_delta) { | |
116 DCHECK(!metrics_.InTerminalState()); | |
117 DCHECK(!metrics_.InInitialState()); | |
118 user_session_uptime_ = time_delta; | |
119 double log_base = ExpBucketBase(ExperimentMetrics::kMaxSessionLength, | |
120 ExperimentMetrics::kSessionLengthBucketBits); | |
121 metrics_.session_length_bucket = LogFloor( | |
122 1 + std::min(time_delta, base::TimeDelta::FromMinutes( | |
123 ExperimentMetrics::kMaxSessionLength)) | |
124 .InMinutes(), | |
125 log_base); | |
126 } | |
127 | |
128 void Experiment::SetActionDelay(base::TimeDelta time_delta) { | |
129 DCHECK(!metrics_.InTerminalState()); | |
130 DCHECK(!metrics_.InInitialState()); | |
131 action_delay_ = time_delta; | |
132 double log_base = ExpBucketBase(ExperimentMetrics::kMaxActionDelay, | |
133 ExperimentMetrics::kActionDelayBucketBits); | |
134 metrics_.action_delay_bucket = | |
135 LogFloor(1 + std::min(time_delta, base::TimeDelta::FromSeconds( | |
136 ExperimentMetrics::kMaxActionDelay)) | |
137 .InSeconds(), | |
138 log_base); | |
139 } | |
140 | |
141 } // namespace installer | |
OLD | NEW |