File: user_activity_ukm_logger_helpers.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 3,415 bytes parent folder | download | duplicates (3)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_POWER_ML_USER_ACTIVITY_UKM_LOGGER_HELPERS_H_
#define CHROME_BROWSER_ASH_POWER_ML_USER_ACTIVITY_UKM_LOGGER_HELPERS_H_

#include <array>
#include <map>
#include <string>

#include "base/check_op.h"
#include "base/logging.h"
#include "chrome/browser/ash/power/ml/user_activity_event.pb.h"

namespace ash {
namespace power {
namespace ml {

// Metrics below are bucketized.
inline constexpr char kBatteryPercent[] = "BatteryPercent";
inline constexpr char kEventLogDuration[] = "EventLogDuration";
inline constexpr char kKeyEventsInLastHour[] = "KeyEventsInLastHour";
inline constexpr char kLastActivityTime[] = "LastActivityTime";
inline constexpr char kLastUserActivityTime[] = "LastUserActivityTime";
inline constexpr char kMouseEventsInLastHour[] = "MouseEventsInLastHour";
inline constexpr char kRecentVideoPlayingTime[] = "RecentVideoPlayingTime";
inline constexpr char kTimeSinceLastVideoEnded[] = "TimeSinceLastVideoEnded";
inline constexpr char kTouchEventsInLastHour[] = "TouchEventsInLastHour";

// TODO(jiameng): both Bucket and Bucketize are meant for user activity logging,
// but it's currently used by adaptive brightness. Need to refactor by either
// moving these two items to a more common lib or having a helper file for
// adaptive brightness as the two projects are likely to diverge.

// Both |boundary_end| and |rounding| must be positive.
struct Bucket {
  int boundary_end;
  int rounding;
};

// Bucketize |original_value| using given |buckets|, which is an array of
// Bucket and must be sorted in ascending order of |boundary_end|.
// |original_value| must be non-negative. An example of |buckets| is
// {{60, 1}, {300, 10}, {600, 20}}. This function rounds |original_value| down
// to the nearest |bucket.rounding|, where |bucket| is the first entry in
// |buckets| with |bucket.boundary_end| > |original_value|.
// If |original_value| is greater than all |boundary_end|, the function
// returns the largest |boundary_end|. Using the above |buckets| example, the
// function will return 30 if |original_value| = 30, and 290 if
// |original_value| = 299.
template <size_t N>
int Bucketize(int original_value, const std::array<Bucket, N>& buckets) {
  DCHECK_GE(original_value, 0);
  DCHECK(!buckets.empty());
  for (const auto& bucket : buckets) {
    if (original_value < bucket.boundary_end) {
      return bucket.rounding * (original_value / bucket.rounding);
    }
  }
  return buckets.back().boundary_end;
}

class UserActivityUkmLoggerBucketizer {
 public:
  UserActivityUkmLoggerBucketizer() = delete;
  UserActivityUkmLoggerBucketizer(const UserActivityUkmLoggerBucketizer&) =
      delete;
  UserActivityUkmLoggerBucketizer& operator=(
      const UserActivityUkmLoggerBucketizer&) = delete;

  // Bucketizes features if they are present. Returns a
  // feature->bucketized_value map.
  static std::map<std::string, int> BucketizeUserActivityEventFeatures(
      const UserActivityEvent::Features& features);

  // Bucketizes features and also EventLogDuration.
  static std::map<std::string, int> BucketizeUserActivityEventData(
      const UserActivityEvent& event);
};

}  // namespace ml
}  // namespace power
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_POWER_ML_USER_ACTIVITY_UKM_LOGGER_HELPERS_H_