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

Side by Side Diff: chrome/installer/util/experiment.cc

Issue 2889323004: Win 10 Inactive toast experiment metrics and storage modifications. (Closed)
Patch Set: Revert extra files Created 3 years, 6 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 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_NE(ExperimentMetrics::kUninitialized, metrics.state);
38 DCHECK(metrics.InInitialState() ||
39 metrics.state == ExperimentMetrics::kGroupAssigned);
40 metrics_ = metrics;
41 state_ = metrics.state;
42 group_ = metrics.group;
43 }
44
45 void Experiment::SetState(ExperimentMetrics::State state) {
46 DCHECK_NE(ExperimentMetrics::kUninitialized, state);
47 state_ = state;
48 metrics_.state = state;
49 }
50
51 void Experiment::AssignGroup(int group) {
52 DCHECK_GE(group, 0);
53 DCHECK_LT(group, ExperimentMetrics::kNumGroups);
54 DCHECK(metrics_.InInitialState());
55
56 group_ = group;
57 metrics_.group = group;
58 SetState(ExperimentMetrics::kGroupAssigned);
59 }
60
61 void Experiment::SetToastLocation(ExperimentMetrics::ToastLocation location) {
62 DCHECK(!metrics_.InTerminalState());
63 DCHECK(!metrics_.InInitialState());
64 toast_location_ = location;
65 metrics_.toast_location = location;
66 }
67
68 void Experiment::SetInactiveDays(int days) {
69 DCHECK(!metrics_.InTerminalState());
70 DCHECK(!metrics_.InInitialState());
71 DCHECK_GE(days, 0);
72 inactive_days_ = days;
73 double log_base = ExpBucketBase(ExperimentMetrics::kMaxLastUsed,
74 ExperimentMetrics::kLastUsedBucketBits);
75 metrics_.last_used_bucket =
76 LogFloor(1 + std::min(days, ExperimentMetrics::kMaxLastUsed), log_base);
77 }
78
79 void Experiment::SetToastCount(int count) {
80 DCHECK(!metrics_.InTerminalState());
81 DCHECK(!metrics_.InInitialState());
82 toast_count_ = count;
83 metrics_.toast_count = std::min(count, ExperimentMetrics::kMaxToastCount);
84 }
85
86 void Experiment::SetDisplayTime(base::Time time) {
87 DCHECK(!metrics_.InTerminalState());
88 DCHECK(!metrics_.InInitialState());
89 if (metrics_.first_toast_offset == 0) {
90 // This is the first time toast is shown so add user to today's cohort.
91 first_display_time_ = time;
92 metrics_.first_toast_offset =
93 (time - base::Time::UnixEpoch() -
94 base::TimeDelta::FromSeconds(
95 ExperimentMetrics::kExperimentStartSeconds))
96 .InDays();
97 // If display time is outside the experiment range (possible due to
98 // invalid local time), then set it to be kMaxFirstToastOffset.
99 if (metrics_.first_toast_offset < 0) {
100 metrics_.first_toast_offset = ExperimentMetrics::kMaxFirstToastOffset;
101 } else {
102 metrics_.first_toast_offset = std::min(
103 metrics_.first_toast_offset, ExperimentMetrics::kMaxFirstToastOffset);
104 }
105 }
106 latest_display_time_ = time;
107 metrics_.toast_hour = (time - time.LocalMidnight()).InHours();
108 DCHECK_LE(metrics_.toast_hour, 24);
109 DCHECK_GE(metrics_.toast_hour, 0);
110 }
111
112 void Experiment::SetUserSessionUptime(base::TimeDelta time_delta) {
113 DCHECK(!metrics_.InTerminalState());
114 DCHECK(!metrics_.InInitialState());
115 user_session_uptime_ = time_delta;
116 double log_base = ExpBucketBase(ExperimentMetrics::kMaxSessionLength,
117 ExperimentMetrics::kSessionLengthBucketBits);
118 metrics_.session_length_bucket = LogFloor(
119 1 + std::min(time_delta, base::TimeDelta::FromMinutes(
120 ExperimentMetrics::kMaxSessionLength))
121 .InMinutes(),
122 log_base);
123 }
124
125 void Experiment::SetActionDelay(base::TimeDelta time_delta) {
126 DCHECK(!metrics_.InTerminalState());
127 DCHECK(!metrics_.InInitialState());
128 action_delay_ = time_delta;
129 double log_base = ExpBucketBase(ExperimentMetrics::kMaxActionDelay,
130 ExperimentMetrics::kActionDelayBucketBits);
131 metrics_.action_delay_bucket =
132 LogFloor(1 + std::min(time_delta, base::TimeDelta::FromSeconds(
133 ExperimentMetrics::kMaxActionDelay))
134 .InSeconds(),
135 log_base);
136 }
137
138 } // namespace installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698