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