Index: chrome/installer/util/experiment.cc |
diff --git a/chrome/installer/util/experiment.cc b/chrome/installer/util/experiment.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c7bd43b18e651be7574b3b5b4d5ccd40408ae199 |
--- /dev/null |
+++ b/chrome/installer/util/experiment.cc |
@@ -0,0 +1,182 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/installer/util/experiment.h" |
+ |
+#include <cmath> |
+ |
+#include "base/logging.h" |
+ |
+namespace installer { |
+ |
+// Returns closest integer of logarithm of |x| with base |b|. |
+static double LogFloor(double x, double b) { |
+ return round(log(x) / log(b)); |
grt (UTC plus 2)
2017/05/24 13:43:38
nit: std::round
nikunjb
2017/05/30 21:45:01
Done.
|
+} |
+ |
+// Returns the base to use for exponential buckets so that buckets |
+// 0,1,.. 2^|bits|-1 cover range [0, max_val]. If this function return b |
+// then Bucket value i will store values from [b^i, b^(i+1)] |
+static double ExpBucketBase(int max_val, int bits) { |
+ return exp(log(max_val - 1) / (1 << bits)); |
grt (UTC plus 2)
2017/05/24 13:43:38
std::exp, std::log
nikunjb
2017/05/30 21:45:01
Done.
|
+} |
+ |
+Experiment::Experiment() = default; |
+Experiment::Experiment(Experiment&&) = default; |
+Experiment::Experiment(const Experiment&) = default; |
+Experiment::~Experiment() = default; |
+ |
+void Experiment::InitializeFromMetrics(const ExperimentMetrics& metrics) { |
+ *this = Experiment(); |
+ metrics_ = metrics; |
+ state_ = metrics.state; |
+ group_ = metrics.group; |
+ toast_location_ = metrics.toast_location; |
+ toast_count_ = metrics.toast_count; |
+ if (metrics_.last_used_bucket > 0) { |
+ double log_base = ExpBucketBase(ExperimentMetrics::kMaxLastUsed, |
+ ExperimentMetrics::kLastUsedBucketBits); |
+ inactive_days_ = |
+ static_cast<int>(round(pow(log_base, metrics_.last_used_bucket) - 1)); |
grt (UTC plus 2)
2017/05/24 13:43:38
std::pow
nikunjb
2017/05/30 21:45:01
Done.
|
+ if (inactive_days_ > ExperimentMetrics::kMaxLastUsed) { |
+ inactive_days_ = ExperimentMetrics::kMaxLastUsed; |
+ } |
+ } |
+ if (metrics_.session_length_bucket > 0) { |
+ double log_base = ExpBucketBase(ExperimentMetrics::kMaxSessionLength, |
+ ExperimentMetrics::kSessionLengthBits); |
+ user_session_uptime_ = base::TimeDelta::FromMinutes(static_cast<int>( |
+ round(pow(log_base, metrics_.session_length_bucket) - 1))); |
+ if (user_session_uptime_.InMinutes() > |
+ ExperimentMetrics::kMaxSessionLength) { |
+ user_session_uptime_ = |
+ base::TimeDelta::FromMinutes(ExperimentMetrics::kMaxSessionLength); |
+ } |
+ } |
+ |
+ if (metrics_.display_time_bucket > 0) { |
+ double log_base = ExpBucketBase(ExperimentMetrics::kMaxDisplayTime, |
+ ExperimentMetrics::kDisplayTimeBucketBits); |
+ action_delay_ = base::TimeDelta::FromSeconds(static_cast<int>( |
+ round(pow(log_base, metrics_.display_time_bucket) - 1))); |
+ if (action_delay_.InSeconds() > ExperimentMetrics::kMaxDisplayTime) { |
+ action_delay_ = |
+ base::TimeDelta::FromSeconds(ExperimentMetrics::kMaxDisplayTime); |
+ } |
+ } |
+ |
+ if (metrics_.first_toast_offset > 0) { |
+ // Accurate time information is not kept in |metrics|. Reconstruct |
+ // the toast display time to the closest day. |
+ first_display_time_ = |
+ (base::Time::UnixEpoch() + |
+ base::TimeDelta::FromSeconds( |
+ ExperimentMetrics::kExperimentStartSeconds) + |
+ base::TimeDelta::FromDays(metrics_.first_toast_offset)); |
+ DCHECK_LE(metrics_.first_toast_offset, |
+ ExperimentMetrics::kExperimentStartSeconds); |
+ } |
+ // Latest display time is not stored in experiment metrics. So, it will |
+ // be initialized to be same as |first_display_time_| for now. |
+ latest_display_time_ = first_display_time_; |
+} |
+ |
+void Experiment::SetState(ExperimentMetrics::State state) { |
+ state_ = state; |
+ metrics_.SetState(state); |
+} |
+ |
+void Experiment::AssignGroup(int group) { |
+ DCHECK_GE(group, 0); |
+ DCHECK_LT(group, ExperimentMetrics::kNumGroups); |
+ DCHECK(metrics_.InInitialState()); |
+ |
+ group_ = group; |
+ metrics_.group = group; |
+ SetState(ExperimentMetrics::kGroupAssigned); |
+} |
+ |
+void Experiment::SetToastLocation(ExperimentMetrics::ToastLocation location) { |
+ DCHECK(!metrics_.InTerminalState()); |
+ DCHECK(!metrics_.InInitialState()); |
+ toast_location_ = location; |
+ metrics_.toast_location = location; |
+} |
+ |
+void Experiment::SetInactiveDays(int days) { |
+ DCHECK(!metrics_.InTerminalState()); |
+ DCHECK(!metrics_.InInitialState()); |
+ DCHECK_GE(days, 0); |
+ inactive_days_ = days; |
+ double log_base = ExpBucketBase(ExperimentMetrics::kMaxLastUsed, |
+ ExperimentMetrics::kLastUsedBucketBits); |
+ metrics_.last_used_bucket = LogFloor(1 + days, log_base); |
+ if (days > ExperimentMetrics::kMaxLastUsed) { |
+ metrics_.last_used_bucket = |
+ LogFloor(1 + ExperimentMetrics::kMaxLastUsed, log_base); |
+ } |
+} |
+ |
+void Experiment::SetToastCount(int count) { |
+ DCHECK(!metrics_.InTerminalState()); |
+ DCHECK(!metrics_.InInitialState()); |
+ toast_count_ = count; |
+ metrics_.toast_count = count; |
+ if (count > ExperimentMetrics::kMaxToastCount) { |
+ metrics_.toast_count = ExperimentMetrics::kMaxToastCount; |
+ } |
+} |
+ |
+void Experiment::SetDisplayTime(const base::Time& time) { |
+ DCHECK(!metrics_.InTerminalState()); |
+ DCHECK(!metrics_.InInitialState()); |
+ if (metrics_.first_toast_offset == 0) { |
+ // This is the first time toast is shown so add user to today's cohort. |
+ first_display_time_ = time; |
+ metrics_.first_toast_offset = |
+ (time - base::Time::UnixEpoch() - |
+ base::TimeDelta::FromSeconds( |
+ ExperimentMetrics::kExperimentStartSeconds)) |
+ .InDays(); |
+ if (metrics_.first_toast_offset > ExperimentMetrics::kMaxFirstToastOffset || |
+ metrics_.first_toast_offset < 0) { |
+ // First display time is outside the experiment range. Invalid local time. |
+ // Reset to highest value of first display time. |
+ metrics_.first_toast_offset = ExperimentMetrics::kMaxFirstToastOffset; |
+ } |
+ } |
+ latest_display_time_ = time; |
+ metrics_.toast_hour = (time - time.LocalMidnight()).InHours(); |
+ DCHECK_LE(metrics_.toast_hour, 24); |
+ DCHECK_GE(metrics_.toast_hour, 0); |
+} |
+ |
+void Experiment::SetUserSessionUptime(const base::TimeDelta& time_delta) { |
+ DCHECK(!metrics_.InTerminalState()); |
+ DCHECK(!metrics_.InInitialState()); |
+ user_session_uptime_ = time_delta; |
+ double log_base = ExpBucketBase(ExperimentMetrics::kMaxSessionLength, |
+ ExperimentMetrics::kSessionLengthBits); |
+ metrics_.session_length_bucket = |
+ LogFloor(1 + time_delta.InMinutes(), log_base); |
+ if (time_delta.InMinutes() > ExperimentMetrics::kMaxSessionLength) { |
+ metrics_.session_length_bucket = |
+ LogFloor(1 + ExperimentMetrics::kMaxSessionLength, log_base); |
+ } |
+} |
+ |
+void Experiment::SetActionDelay(const base::TimeDelta& time_delta) { |
+ DCHECK(!metrics_.InTerminalState()); |
+ DCHECK(!metrics_.InInitialState()); |
+ action_delay_ = time_delta; |
+ double log_base = ExpBucketBase(ExperimentMetrics::kMaxDisplayTime, |
+ ExperimentMetrics::kDisplayTimeBucketBits); |
+ metrics_.display_time_bucket = LogFloor(1 + time_delta.InSeconds(), log_base); |
+ if (time_delta.InSeconds() > ExperimentMetrics::kMaxDisplayTime) { |
+ metrics_.display_time_bucket = |
+ LogFloor(1 + ExperimentMetrics::kMaxDisplayTime, log_base); |
+ } |
+} |
+ |
+} // namespace installer |