File: active_status.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 4,394 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_REPORT_DEVICE_METRICS_CHURN_ACTIVE_STATUS_H_
#define CHROMEOS_ASH_COMPONENTS_REPORT_DEVICE_METRICS_CHURN_ACTIVE_STATUS_H_

#include <bitset>
#include <optional>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "chromeos/ash/components/report/prefs/fresnel_pref_names.h"
#include "chromeos/ash/components/report/proto/fresnel_service.pb.h"

class PrefService;

namespace ash {

namespace system {
class StatisticsProvider;
}  // namespace system

namespace report::device_metrics {

// Manage information about the number of active months and the months since
// the device's inception, using specific bit sizes for each.
// Provide the methods for calculating churn analysis metadata.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_REPORT) ActiveStatus {
 public:
  static constexpr int kInceptionYear = 2000;
  static constexpr int kMonthCountBitSize = 10;
  static constexpr int kActiveMonthsBitSize = 18;
  static constexpr int kActiveStatusBitSize =
      kMonthCountBitSize + kActiveMonthsBitSize;

  // Calculate monthly and yearly churn for the 3 observation windows by
  // checking active status at bits 1-3 (monthly) and bits 13-15 (yearly).
  static constexpr int kMonthlyChurnOffset = 1;
  static constexpr int kYearlyChurnOffset = 13;

  // Track number of months from inception date in first 10 bits active status.
  static constexpr char kActiveStatusInceptionDate[] =
      "2000-01-01 00:00:00 GMT";

  ActiveStatus() = delete;
  explicit ActiveStatus(PrefService* local_state);
  ActiveStatus(const ActiveStatus&) = delete;
  ActiveStatus& operator=(const ActiveStatus&) = delete;
  ~ActiveStatus() = default;

  // Accessor methods for the active status value stored in local state pref.
  // This pref is used as the source of truth for this class.
  int GetValue() const;
  void SetValue(int val);

  // Returns a copy of the 28 bit updated active status value which reflects
  // the current month is also active.
  std::optional<int> CalculateNewValue(base::Time ts) const;

  // The inception month and the month count since inception are utilized to
  // generate a fresh timestamp that signifies the presently active month.
  std::optional<base::Time> GetCurrentActiveMonthTimestamp() const;

  // Return necessary information used to send the churn cohort request.
  std::optional<ChurnCohortMetadata> CalculateCohortMetadata(
      base::Time active_ts) const;

  // Return necessary information used to send the churn observation request,
  // based on the local state last ping prefs.
  // |period| is a value between [0,2].
  // |period| = 0 indicates 3-month period starting current month.
  // |period| = 1 indicates 3-month period starting last month.
  // |period| = 2 indicates 3-month period starting two months ago.
  std::optional<ChurnObservationMetadata> CalculateObservationMetadata(
      base::Time active_ts,
      int period) const;

 private:
  // Grant friend access for comprehensive testing of private/protected members.
  friend class ActiveStatusTest;

  // Return |kActiveStatusInceptionDate| as a GMT timestamp.
  std::optional<base::Time> GetInceptionMonthTimestamp() const;

  // Return the int representation of the known months since inception, based
  // on the active status value in local state pref.
  int GetMonthsSinceInception() const;

  // Return the int representation of the known active month bits, based on the
  // active status value in local state pref.
  int GetActiveMonthBits() const;

  // Check if device was active during a specific month in the past 18 months.
  // |month_idx| is a value within the past 18 months, [0,17].
  bool IsDeviceActiveInMonth(int month_idx) const;

  // Helper to check if |active_ts| aligns with the first active month.
  std::optional<bool> IsFirstActiveInCohort(base::Time active_ts) const;

  // Used to read/write the latest active status value.
  const raw_ptr<PrefService> local_state_;

  // Singleton lives throughout class lifetime.
  const raw_ptr<system::StatisticsProvider> statistics_provider_;
};

}  // namespace report::device_metrics
}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_REPORT_DEVICE_METRICS_CHURN_ACTIVE_STATUS_H_