Chromium Code Reviews| 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..bcf87d9022b8a548ff9885642b005634523219b6 |
| --- /dev/null |
| +++ b/chrome/installer/util/experiment.cc |
| @@ -0,0 +1,141 @@ |
| +// 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 <algorithm> |
| +#include <cmath> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace installer { |
| + |
| +namespace { |
| + |
| +// Returns closest integer of logarithm of |x| with base |b|. |
| +double LogFloor(double x, double b) { |
| + return std::round(std::log(x) / std::log(b)); |
| +} |
| + |
| +// 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)] |
| +double ExpBucketBase(int max_val, int bits) { |
| + return std::exp(std::log(max_val + 1) / ((1 << bits) - 1)); |
| +} |
| + |
| +} // namespace |
| + |
| +Experiment::Experiment() = default; |
| +Experiment::Experiment(Experiment&&) = default; |
| +Experiment::Experiment(const Experiment&) = default; |
| +Experiment::~Experiment() = default; |
| + |
| +void Experiment::InitializeFromMetrics(const ExperimentMetrics& metrics) { |
| + *this = Experiment(); |
| + DCHECK(metrics.InInitialState() || |
| + metrics.state == ExperimentMetrics::kGroupAssigned); |
| + if (metrics.state == ExperimentMetrics::kUninitialized) { |
| + // 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
|
| + return; |
| + } |
| + metrics_ = metrics; |
| + state_ = metrics.state; |
| + group_ = metrics.group; |
| +} |
| + |
| +void Experiment::SetState(ExperimentMetrics::State state) { |
| + DCHECK_NE(ExperimentMetrics::kUninitialized, state); |
| + state_ = state; |
| + metrics_.state = 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 + std::min(days, ExperimentMetrics::kMaxLastUsed), log_base); |
| +} |
| + |
| +void Experiment::SetToastCount(int count) { |
| + DCHECK(!metrics_.InTerminalState()); |
| + DCHECK(!metrics_.InInitialState()); |
| + toast_count_ = count; |
| + metrics_.toast_count = std::min(count, ExperimentMetrics::kMaxToastCount); |
| +} |
| + |
| +void Experiment::SetDisplayTime(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 display time is outside the experiment range (possible due to |
| + // invalid local time), then set it to be kMaxFirstToastOffset. |
| + if (metrics_.first_toast_offset < 0) { |
| + metrics_.first_toast_offset = ExperimentMetrics::kMaxFirstToastOffset; |
| + } else { |
| + metrics_.first_toast_offset = std::min( |
| + 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(base::TimeDelta time_delta) { |
| + DCHECK(!metrics_.InTerminalState()); |
| + DCHECK(!metrics_.InInitialState()); |
| + user_session_uptime_ = time_delta; |
| + double log_base = ExpBucketBase(ExperimentMetrics::kMaxSessionLength, |
| + ExperimentMetrics::kSessionLengthBucketBits); |
| + metrics_.session_length_bucket = LogFloor( |
| + 1 + std::min(time_delta, base::TimeDelta::FromMinutes( |
| + ExperimentMetrics::kMaxSessionLength)) |
| + .InMinutes(), |
| + log_base); |
| +} |
| + |
| +void Experiment::SetActionDelay(base::TimeDelta time_delta) { |
| + DCHECK(!metrics_.InTerminalState()); |
| + DCHECK(!metrics_.InInitialState()); |
| + action_delay_ = time_delta; |
| + double log_base = ExpBucketBase(ExperimentMetrics::kMaxActionDelay, |
| + ExperimentMetrics::kActionDelayBucketBits); |
| + metrics_.action_delay_bucket = |
| + LogFloor(1 + std::min(time_delta, base::TimeDelta::FromSeconds( |
| + ExperimentMetrics::kMaxActionDelay)) |
| + .InSeconds(), |
| + log_base); |
| +} |
| + |
| +} // namespace installer |