Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Provides aggregation of feature usage by tracking impressions and clicks | |
| 6 // over 1-week and 28-day intervals. | |
| 7 // Used by Contextual Search to record impressions of the Bar and CTR of | |
| 8 // panel opens to use as signals for Tap triggering. | |
| 9 | |
| 10 #ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | |
| 11 #define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | |
| 12 | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/gtest_prod_util.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "components/contextual_search/browser/native_int_storage.h" | |
| 18 | |
| 19 namespace contextual_search { | |
| 20 | |
| 21 // Usage: Create a CTRAggregator and start recording impressions or reading | |
| 22 // aggregated data. Get data from the previous week or previous 4-week period | |
| 23 // that ended with the previous week. | |
| 24 // A new week starts at an arbitrary time based on seconds since the Epoch. | |
| 25 // The data from the previous week and previous 28-day period are guaranteed to | |
| 26 // be complete only if the HasPrevious method returns true. If one of the data | |
| 27 // accessors is called when the data is not complete the Impressions will return | |
| 28 // data for only the partial period, and the CTR method will return NAN. | |
| 29 class CTRAggregator { | |
| 30 public: | |
| 31 CTRAggregator(NativeIntStorage& storage); | |
| 32 ~CTRAggregator(); | |
| 33 | |
| 34 // Record an impression. Records a click if |did_click| is true. | |
| 35 void RecordImpression(bool did_click); | |
| 36 | |
| 37 // Returns whether we have the previous week's data for this user. | |
| 38 bool HasPreviousWeekData(); | |
| 39 | |
| 40 // Gets the number of impressions from the previous week. | |
| 41 int GetPreviousWeekImpressions(); | |
| 42 | |
| 43 // Gets the CTR from the previous week. | |
| 44 // Returns NAN if there is no previous week's data for this user. | |
| 45 float GetPreviousWeekCTR(); | |
| 46 | |
| 47 // Returns whether we have data from a 28 day period ending in the previous | |
| 48 // week. | |
| 49 bool HasPrevious28DayData(); | |
| 50 | |
| 51 // Gets the number of impressions from a 28 day period ending in the previous | |
| 52 // week. | |
| 53 int GetPrevious28DayImpressions(); | |
| 54 | |
| 55 // Gets the CTR from a 28 day period ending in the previous week. | |
| 56 // Returns NAN if there is no data for that period for this user. | |
| 57 float GetPrevious28DayCTR(); | |
| 58 | |
| 59 private: | |
| 60 // This implementation uses a fixed number of bins to store integer impression | |
| 61 // and click data for the most recent N weeks, where N = 5 (in order to keep 4 | |
| 62 // complete weeks). Another bin keeps track of the current week being | |
| 63 // written. Yet another bin records when data was first stored or accessed so | |
| 64 // we can know when a time period has complete data. | |
| 65 friend class CTRAggregatorTest; | |
| 66 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SimpleOperationTest); | |
| 67 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, MultiWeekTest); | |
| 68 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipOneWeekTest); | |
| 69 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipThreeWeekTest); | |
| 70 FRIEND_TEST_ALL_PREFIXES(CTRAggregatorTest, SkipFourWeekTest); | |
| 71 | |
| 72 // Constructor for testing; sets the week. | |
| 73 CTRAggregator(NativeIntStorage& storage, int week_number); | |
| 74 // For testing, increment the current week number by |weeks|. | |
| 75 void IncrementWeek(int weeks); | |
| 76 | |
| 77 // Ensure that the week number of the first week ever used is recorded. | |
| 78 void EnsureFirstWeekRecorded(); | |
| 79 // Updates storage of skipped weeks when advancing to a new current week. | |
| 80 void UpdateStorageToCurrentWeek(); | |
| 81 | |
| 82 // Returns the index of the storage bin to use for the given week | |
| 83 // |which_week|. | |
| 84 int GetStorageIndex(int which_week); | |
| 85 // Returns the string key of the storage bin for the given week |which_week|. | |
| 86 std::string GetWeekKey(int which_week); | |
| 87 // Returns the string key for the "clicks" storage bin for the given week | |
| 88 // |which_week|. | |
| 89 std::string GetWeekClicksKey(int which_week); | |
| 90 // Returns the string key for the "impressions" storage bin for the given week | |
| 91 // |which_week|. | |
| 92 std::string GetWeekImpressionsKey(int which_week); | |
| 93 // Increments the value in the storage bin with the given |key|. | |
| 94 void Increment(std::string key); | |
|
Theresa
2016/08/26 23:32:08
nit: I would put the GetPreviousWeekClicks() and g
Donn Denman
2016/08/29 22:58:06
Done.
| |
| 95 | |
| 96 // Gets the number of impressions from the previous week. | |
| 97 int GetPreviousWeekClicks(); | |
| 98 // Gets the number of impressions from a 28 day period ending in the previous | |
| 99 // week. | |
| 100 int GetPrevious28DayClicks(); | |
| 101 | |
| 102 // Cached storage accessors: | |
| 103 // Reads and returns an int keyed by |storage_key|. | |
| 104 int ReadInt(std::string storage_key); | |
| 105 // Writes the given int |value| keyed by |storage_key|. | |
| 106 void WriteInt(std::string storage_key, int value); | |
| 107 | |
| 108 // Persistently stores the CTR data. | |
| 109 NativeIntStorage& storage_; | |
| 110 int week_number_; | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(CTRAggregator); | |
| 113 }; | |
| 114 | |
| 115 } // namespace contextual_search | |
| 116 | |
| 117 #endif // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_CTR_AGGREGATOR_H_ | |
| OLD | NEW |