Chromium Code Reviews| 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 metrics_ = metrics; | |
| 40 state_ = metrics.state; | |
| 41 group_ = metrics.group; | |
| 42 if (metrics.state == ExperimentMetrics::kUninitialized) { | |
| 43 // Reset any value stored in experiment. | |
| 44 toast_location_ = ExperimentMetrics::kOverTaskbarPin; | |
| 45 inactive_days_ = 0; | |
| 46 toast_count_ = 0; | |
| 47 first_display_time_ = base::Time(); | |
| 48 latest_display_time_ = base::Time(); | |
| 49 user_session_uptime_ = base::TimeDelta(); | |
| 50 action_delay_ = base::TimeDelta(); | |
| 51 return; | |
|
grt (UTC plus 2)
2017/06/14 21:13:51
nit: omit
nikunjb
2017/06/14 22:42:59
Done.
| |
| 52 } | |
| 53 } | |
| 54 | |
| 55 void Experiment::SetState(ExperimentMetrics::State state) { | |
| 56 DCHECK_NE(ExperimentMetrics::kUninitialized, state); | |
| 57 state_ = state; | |
| 58 metrics_.state = state; | |
| 59 } | |
| 60 | |
| 61 void Experiment::AssignGroup(int group) { | |
| 62 DCHECK_GE(group, 0); | |
| 63 DCHECK_LT(group, ExperimentMetrics::kNumGroups); | |
| 64 DCHECK(metrics_.InInitialState()); | |
| 65 | |
| 66 group_ = group; | |
| 67 metrics_.group = group; | |
| 68 SetState(ExperimentMetrics::kGroupAssigned); | |
| 69 } | |
| 70 | |
| 71 void Experiment::SetToastLocation(ExperimentMetrics::ToastLocation location) { | |
| 72 DCHECK(!metrics_.InTerminalState()); | |
| 73 DCHECK(!metrics_.InInitialState()); | |
| 74 toast_location_ = location; | |
| 75 metrics_.toast_location = location; | |
| 76 } | |
| 77 | |
| 78 void Experiment::SetInactiveDays(int days) { | |
| 79 DCHECK(!metrics_.InTerminalState()); | |
| 80 DCHECK(!metrics_.InInitialState()); | |
| 81 DCHECK_GE(days, 0); | |
| 82 inactive_days_ = days; | |
| 83 double log_base = ExpBucketBase(ExperimentMetrics::kMaxLastUsed, | |
| 84 ExperimentMetrics::kLastUsedBucketBits); | |
| 85 metrics_.last_used_bucket = | |
| 86 LogFloor(1 + std::min(days, ExperimentMetrics::kMaxLastUsed), log_base); | |
| 87 } | |
| 88 | |
| 89 void Experiment::SetToastCount(int count) { | |
| 90 DCHECK(!metrics_.InTerminalState()); | |
| 91 DCHECK(!metrics_.InInitialState()); | |
| 92 toast_count_ = count; | |
| 93 metrics_.toast_count = std::min(count, ExperimentMetrics::kMaxToastCount); | |
| 94 } | |
| 95 | |
| 96 void Experiment::SetDisplayTime(base::Time time) { | |
| 97 DCHECK(!metrics_.InTerminalState()); | |
| 98 DCHECK(!metrics_.InInitialState()); | |
| 99 if (metrics_.first_toast_offset_days == 0) { | |
| 100 // This is the first time toast is shown so add user to today's cohort. | |
| 101 first_display_time_ = time; | |
| 102 metrics_.first_toast_offset_days = | |
| 103 (time - base::Time::UnixEpoch() - | |
| 104 base::TimeDelta::FromSeconds( | |
| 105 ExperimentMetrics::kExperimentStartSeconds)) | |
| 106 .InDays(); | |
| 107 // If display time is outside the experiment range (possible due to | |
| 108 // invalid local time), then set it to be kMaxFirstToastOffsetDays. | |
| 109 if (metrics_.first_toast_offset_days < 0) { | |
| 110 metrics_.first_toast_offset_days = | |
| 111 ExperimentMetrics::kMaxFirstToastOffsetDays; | |
| 112 } else { | |
| 113 metrics_.first_toast_offset_days = | |
| 114 std::min(metrics_.first_toast_offset_days, | |
| 115 ExperimentMetrics::kMaxFirstToastOffsetDays); | |
| 116 } | |
| 117 } | |
| 118 latest_display_time_ = time; | |
| 119 metrics_.toast_hour = (time - time.LocalMidnight()).InHours(); | |
| 120 DCHECK_LE(metrics_.toast_hour, 24); | |
| 121 DCHECK_GE(metrics_.toast_hour, 0); | |
| 122 } | |
| 123 | |
| 124 void Experiment::SetUserSessionUptime(base::TimeDelta time_delta) { | |
| 125 DCHECK(!metrics_.InTerminalState()); | |
| 126 DCHECK(!metrics_.InInitialState()); | |
| 127 user_session_uptime_ = time_delta; | |
| 128 double log_base = ExpBucketBase(ExperimentMetrics::kMaxSessionLength, | |
| 129 ExperimentMetrics::kSessionLengthBucketBits); | |
| 130 if (time_delta.InMinutes() < 0 || | |
| 131 time_delta.InMinutes() > ExperimentMetrics::kMaxSessionLength) { | |
| 132 time_delta = base::TimeDelta::FromMinutes( | |
| 133 ExperimentMetrics::ExperimentMetrics::kMaxSessionLength); | |
| 134 } | |
| 135 metrics_.session_length_bucket = | |
| 136 LogFloor(1 + time_delta.InMinutes(), log_base); | |
| 137 } | |
| 138 | |
| 139 void Experiment::SetActionDelay(base::TimeDelta time_delta) { | |
| 140 DCHECK(!metrics_.InTerminalState()); | |
| 141 DCHECK(!metrics_.InInitialState()); | |
| 142 action_delay_ = time_delta; | |
| 143 if (time_delta.InSeconds() < 0 || | |
| 144 time_delta.InSeconds() > ExperimentMetrics::kMaxActionDelay) { | |
| 145 time_delta = | |
| 146 base::TimeDelta::FromSeconds(ExperimentMetrics::kMaxActionDelay); | |
| 147 } | |
| 148 double log_base = ExpBucketBase(ExperimentMetrics::kMaxActionDelay, | |
| 149 ExperimentMetrics::kActionDelayBucketBits); | |
| 150 metrics_.action_delay_bucket = LogFloor(1 + time_delta.InSeconds(), log_base); | |
| 151 } | |
| 152 | |
| 153 } // namespace installer | |
| OLD | NEW |