File: weekly_activity_storage.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 (88 lines) | stat: -rw-r--r-- 3,839 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
// 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.

#ifndef COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_
#define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_

#include <string>
#include <unordered_map>

#include "base/macros.h"

namespace contextual_search {

// An abstract class that stores weekly user interaction data in device-specific
// integer storage. Only a limited storage window is supported, set through the
// constructor. Allows callers to read and write user actions to persistent
// storage on the device by overriding the ReadStorage and WriteStorage calls.
// A user view of some UX is an "Impression", and user interaction is considered
// a "Click" even if the triggering gesture was something else.  Together they
// produce the Click-Through-Rate, or CTR.
class WeeklyActivityStorage {
 public:
  // Constructs an instance that will manage at least |weeks_needed| weeks of
  // data.
  WeeklyActivityStorage(int weeks_needed);
  virtual ~WeeklyActivityStorage();

  // Advances the accessible storage range to end at the given |week_number|.
  // Since only a limited number of storage weeks are supported, advancing to
  // a different week makes data from weeks than the range size inaccessible.
  // This must be called for each week before reading or writing any data
  // for that week.
  // HasData will return true for all the weeks that still have accessible data.
  void AdvanceToWeek(int week_number);

  // Returns the number of clicks for the given week.
  int ReadClicks(int week_number);
  // Writes |value| into the number of clicks for the given |week_number|.
  void WriteClicks(int week_number, int value);

  // Returns the number of impressions for the given week.
  int ReadImpressions(int week_number);
  // Writes |value| into the number of impressions for the given |week_number|.
  void WriteImpressions(int week_number, int value);

  // Returns whether the given |week_number| has data, based on whether
  // InitData has ever been called for that week.
  bool HasData(int week_number);
  // Clears the click and impression counters for the given |week_number|.
  void ClearData(int week_number);

  // Reads and returns the value keyed by |storage_bucket|.
  // If there is no stored value associated with the given bucket then 0 is
  // returned.
  virtual int ReadStorage(std::string storage_bucket) = 0;
  // Overwrites the |value| to the storage bucket keyed by |storage_bucket|,
  // regardless of whether there is an existing value in the given bucket.
  virtual void WriteStorage(std::string storage_bucket, int value) = 0;

 private:
  // Returns the string key of the storage bin for the given week |which_week|.
  std::string GetWeekKey(int which_week);
  // Returns the string key for the "clicks" storage bin for the given week
  // |which_week|.
  std::string GetWeekClicksKey(int which_week);
  // Returns the string key for the "impressions" storage bin for the given week
  // |which_week|.
  std::string GetWeekImpressionsKey(int which_week);

  // Reads and returns the integer keyed by |storage_key|.
  // If there is no value for the given key then 0 is returned.
  int ReadInt(std::string storage_key);
  // Writes the integer |value| to the storage bucket keyed by |storage_key|.
  void WriteInt(std::string storage_key, int value);

  // Ensures that activity data is initialized for the given week |which_week|.
  void EnsureHasActivity(int which_week);

  // The number of weeks of data that this instance needs to support.
  int weeks_needed_;

  DISALLOW_COPY_AND_ASSIGN(WeeklyActivityStorage);
};

}  // namespace contextual_search

#endif  // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_