File: ctr_aggregator.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (118 lines) | stat: -rw-r--r-- 4,878 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
// Copyright 2016 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.

// Provides aggregation of feature usage by tracking impressions and clicks
// over 1-week and 28-day intervals.  Impressions are views of some UX and
// clicks are any measured interaction with that UX, yielding CTR -- Click
// Through Rate.
// Used by Contextual Search to record impressions of the Bar and CTR of
// panel opens to use as signals for Tap triggering.

#ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_
#define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "components/contextual_search/browser/weekly_activity_storage.h"

namespace contextual_search {

// Number of weeks of data needed for 28 days.
const int kNumWeeksNeededFor28DayData = 4;

// Usage: Create a CtrAggregator and start recording impressions or reading
// aggregated data.  Get data from the previous week or previous 4-week period
// that ended with the previous week.
// A new week starts at an arbitrary time based on seconds since the Epoch.
// The data from the previous week and previous 28-day period are guaranteed to
// be complete only if the HasPrevious method returns true.  If one of the data
// accessors is called when the data is not complete invalid data may be
// returned.
class CtrAggregator {
 public:
  // Constructs a CtrAggregator using the given |storage| mechanism.
  // Data is stored by |storage| typically on persistent device-local storage.
  // A callback through the storage interface may occur at construction time,
  // so the |storage| must be fully initialized when this constructor is
  // called.
  CtrAggregator(WeeklyActivityStorage& storage);
  ~CtrAggregator();

  // Records an impression.  Records a click if |did_click| is true.
  void RecordImpression(bool did_click);

  // Returns the number for the current week. Useful for checking when the
  // current week changes.
  int GetCurrentWeekNumber();

  // Returns whether we have the previous week's data for this user.
  bool HasPreviousWeekData();

  // Gets the number of impressions from the previous week.
  // Callers must check if there is previous week's data for this user, or
  // invalid data may be returned.
  int GetPreviousWeekImpressions();

  // Gets the CTR from the previous week.
  // Callers must check if there is previous week's data for this user, or
  // invalid data may be returned.
  float GetPreviousWeekCtr();

  // Returns whether we have data from a 28 day period ending in the previous
  // week.
  bool HasPrevious28DayData();

  // Gets the number of impressions from a 28 day period ending in the previous
  // week.
  // Callers must check if there is previous 28 day data for this user, or
  // invalid data may be returned.
  int GetPrevious28DayImpressions();

  // Gets the CTR from a 28 day period ending in the previous week.
  // Callers must check if there is previous 28 day data for this user, or
  // invalid data may be returned.
  float GetPrevious28DayCtr();

 private:
  // This implementation uses a fixed number of bins to store integer impression
  // and click data for the most recent N weeks, where N = 5 (in order to keep 4
  // complete weeks).  Another bin keeps track of the current week being
  // written.  Yet another bin records when data was first stored or accessed so
  // we can know when a time period has complete data.
  friend class CtrAggregatorTest;
  FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SimpleOperationTest);
  FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, MultiWeekTest);
  FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SkipOneWeekTest);
  FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SkipThreeWeeksTest);
  FRIEND_TEST_ALL_PREFIXES(CtrAggregatorTest, SkipFourWeeksTest);

  // Constructs an instance for testing; sets the week.
  CtrAggregator(WeeklyActivityStorage& storage, int week_number);
  // For testing, increments the current week number by |weeks|.
  void IncrementWeek(int weeks);

  // Gets the number of clicks from the previous week.
  // Callers must check if there is previous week's data for this user, or
  // invalid data may be returned.
  int GetPreviousWeekClicks();
  // Gets the number of clicks from a 28 day period ending in the previous
  // week.
  // Callers must check if there is previous 28 day data for this user, or
  // invalid data may be returned.
  int GetPrevious28DayClicks();

  // Stores the weekly activity data.
  WeeklyActivityStorage& storage_;

  // The current week number, expressed as the number of weeks since Epoch.
  int week_number_;

  DISALLOW_COPY_AND_ASSIGN(CtrAggregator);
};

}  // namespace contextual_search

#endif  // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_