Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: components/precache/core/precache_statistics_table.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Addressed comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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 #include "components/precache/core/precache_statistics_table.h"
6
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9 #include "sql/connection.h"
10 #include "sql/statement.h"
11
12 using sql::Statement;
13
14 namespace {
15
16 // Returns the number of 24 hour intervals that have passed between time 0 and
17 // |time_on_day|, rounded down.
18 int64 GetKey(const base::Time& time_on_day) {
19 return (time_on_day - base::Time()).InDays();
20 }
21
22 // Returns the time of the beginning of the 24 hour interval specified by |key|.
23 base::Time FromKey(int64 key) {
24 return base::Time() + base::TimeDelta::FromDays(key);
25 }
26
27 } // namespace
28
29 namespace precache {
30
31 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics()
32 : downloaded_precache_motivated_bytes(0),
33 downloaded_non_precache_bytes(0),
34 downloaded_non_precache_bytes_cellular(0),
35 saved_bytes(0),
36 saved_bytes_cellular(0) {}
37
38 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics(
39 int64 downloaded_precache_motivated_bytes,
40 int64 downloaded_non_precache_bytes,
41 int64 downloaded_non_precache_bytes_cellular, int64 saved_bytes,
42 int64 saved_bytes_cellular)
43 : downloaded_precache_motivated_bytes(downloaded_precache_motivated_bytes),
44 downloaded_non_precache_bytes(downloaded_non_precache_bytes),
45 downloaded_non_precache_bytes_cellular(
46 downloaded_non_precache_bytes_cellular),
47 saved_bytes(saved_bytes),
48 saved_bytes_cellular(saved_bytes_cellular) {}
49
50 bool PrecacheStatisticsTable::PrecacheStatistics::operator==(
51 const PrecacheStatistics& other) const {
52 return downloaded_precache_motivated_bytes ==
53 other.downloaded_precache_motivated_bytes &&
54 downloaded_non_precache_bytes == other.downloaded_non_precache_bytes &&
55 downloaded_non_precache_bytes_cellular ==
56 other.downloaded_non_precache_bytes_cellular &&
57 saved_bytes == other.saved_bytes &&
58 saved_bytes_cellular == other.saved_bytes_cellular;
59 }
60
61 PrecacheStatisticsTable::PrecacheStatisticsTable() : db_(NULL) {}
62
63 PrecacheStatisticsTable::~PrecacheStatisticsTable() {}
64
65 void PrecacheStatisticsTable::Init(sql::Connection* db) {
66 DCHECK(!db_); // Init must only be called once.
67 DCHECK(db); // The database connection must be non-NULL.
68 db_ = db;
69 CreateTableIfNonExistent();
70 }
71
72 void PrecacheStatisticsTable::IncreaseDailyStats(
73 const base::Time& time_on_day, const PrecacheStatistics& delta) {
74 // Create a row for that day if there is no existing one.
75 Statement insert_statement(db_->GetCachedStatement(
76 SQL_FROM_HERE,
77 "INSERT OR IGNORE INTO precache_statistics (date) VALUES(?)"));
78
79 insert_statement.BindInt64(0, GetKey(time_on_day));
80 insert_statement.Run();
81
82 // Increase the recorded statistics for that day.
83 Statement update_statement(db_->GetCachedStatement(
84 SQL_FROM_HERE, "UPDATE precache_statistics SET "
85 "downloaded_precache_motivated_bytes = "
86 "downloaded_precache_motivated_bytes + ?, "
87 "downloaded_non_precache_bytes = "
88 "downloaded_non_precache_bytes + ?, "
89 "downloaded_non_precache_bytes_cellular = "
90 "downloaded_non_precache_bytes_cellular + ?, "
91 "saved_bytes = saved_bytes + ?, "
92 "saved_bytes_cellular = saved_bytes_cellular + ? "
93 "WHERE date = ?"));
94
95 update_statement.BindInt64(0, delta.downloaded_precache_motivated_bytes);
96 update_statement.BindInt64(1, delta.downloaded_non_precache_bytes);
97 update_statement.BindInt64(2, delta.downloaded_non_precache_bytes_cellular);
98 update_statement.BindInt64(3, delta.saved_bytes);
99 update_statement.BindInt64(4, delta.saved_bytes_cellular);
100 update_statement.BindInt64(5, GetKey(time_on_day));
101
102 update_statement.Run();
103 }
104
105 void PrecacheStatisticsTable::GetOldStats(const base::Time& current_time,
106 PrecacheStatisticsMap* stats_map) {
107 stats_map->clear();
108
109 Statement statement(db_->GetCachedStatement(
110 SQL_FROM_HERE, "SELECT date, "
111 "downloaded_precache_motivated_bytes, "
112 "downloaded_non_precache_bytes, "
113 "downloaded_non_precache_bytes_cellular, "
114 "saved_bytes, "
115 "saved_bytes_cellular "
116 "FROM precache_statistics WHERE date < ?"));
117 statement.BindInt64(0, GetKey(current_time));
118
119 while (statement.Step()) {
120 base::Time date = FromKey(statement.ColumnInt64(0));
121 PrecacheStatistics stats;
122
123 stats.downloaded_precache_motivated_bytes = statement.ColumnInt64(1);
124 stats.downloaded_non_precache_bytes = statement.ColumnInt64(2);
125 stats.downloaded_non_precache_bytes_cellular = statement.ColumnInt64(3);
126 stats.saved_bytes = statement.ColumnInt64(4);
127 stats.saved_bytes_cellular = statement.ColumnInt64(5);
128
129 (*stats_map)[date] = stats;
130 }
131 }
132
133 void PrecacheStatisticsTable::DeleteOldStats(const base::Time& current_time) {
134 Statement statement(db_->GetCachedStatement(
135 SQL_FROM_HERE, "DELETE FROM precache_statistics WHERE date < ?"));
136
137 statement.BindInt64(0, GetKey(current_time));
138 statement.Run();
139 }
140
141 void PrecacheStatisticsTable::CreateTableIfNonExistent() {
142 ignore_result(db_->Execute(
143 "CREATE TABLE IF NOT EXISTS precache_statistics ("
144 "date INTEGER PRIMARY KEY, "
145 "downloaded_precache_motivated_bytes INTEGER DEFAULT 0, "
146 "downloaded_non_precache_bytes INTEGER DEFAULT 0, "
147 "downloaded_non_precache_bytes_cellular INTEGER DEFAULT 0, "
148 "saved_bytes INTEGER DEFAULT 0, "
149 "saved_bytes_cellular INTEGER DEFAULT 0)"));
Scott Hess - ex-Googler 2013/11/27 01:32:33 If this fails, the caller should really fail Init(
sclittle 2013/12/02 21:12:52 Removed the statistics table.
150 }
151
152 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698