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

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: Reduced CL down to just the PrecacheDatabase Created 7 years, 1 month 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 const char kStatisticsTableName[] = "precache_statistics";
17
18 // Rounds |time_on_day| down to midnight of the start of the day of
19 // |time_on_day|, and returns the internal value.
20 int64 GetKey(const base::Time& time_on_day) {
21 return time_on_day.LocalMidnight().ToInternalValue();
davidben 2013/11/06 23:50:53 What happens if the user changes time zones? Seems
sclittle 2013/11/09 01:07:58 Good point about time zones. If we switch time zon
davidben 2013/11/11 19:06:26 Ah, so if you rely on UMA's aggregation, you'll we
22 }
23
24 } // namespace
25
26 namespace precache {
27
28 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics()
29 : downloaded_precache_motivated_bytes(0),
30 downloaded_non_precache_bytes(0),
31 downloaded_non_precache_bytes_cellular(0),
32 saved_bytes(0),
33 saved_bytes_cellular(0) {}
34
35 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics(
36 int64 downloaded_precache_motivated_bytes,
37 int64 downloaded_non_precache_bytes,
38 int64 downloaded_non_precache_bytes_cellular, int64 saved_bytes,
39 int64 saved_bytes_cellular)
40 : downloaded_precache_motivated_bytes(downloaded_precache_motivated_bytes),
41 downloaded_non_precache_bytes(downloaded_non_precache_bytes),
42 downloaded_non_precache_bytes_cellular(
43 downloaded_non_precache_bytes_cellular),
44 saved_bytes(saved_bytes),
45 saved_bytes_cellular(saved_bytes_cellular) {}
46
47 bool PrecacheStatisticsTable::PrecacheStatistics::operator==(
48 const PrecacheStatistics& other) const {
49 return downloaded_precache_motivated_bytes ==
50 other.downloaded_precache_motivated_bytes &&
51 downloaded_non_precache_bytes == other.downloaded_non_precache_bytes &&
52 downloaded_non_precache_bytes_cellular ==
53 other.downloaded_non_precache_bytes_cellular &&
54 saved_bytes == other.saved_bytes &&
55 saved_bytes_cellular == other.saved_bytes_cellular;
56 }
57
58 PrecacheStatisticsTable::PrecacheStatisticsTable() : db_(NULL) {}
59
60 PrecacheStatisticsTable::~PrecacheStatisticsTable() {}
61
62 void PrecacheStatisticsTable::Init(sql::Connection* db) {
63 DCHECK(!db_); // Init must only be called once.
64 DCHECK(db); // The database connection must be non-NULL.
65 db_ = db;
66 CreateTableIfNonExistent();
67 }
68
69 void PrecacheStatisticsTable::IncreaseDailyStats(
70 const base::Time& time_on_day, const PrecacheStatistics& delta) {
71 DCHECK(db_);
72
73 // Create a row for that day if there is no existing one.
74 Statement insert_statement(db_->GetCachedStatement(
75 SQL_FROM_HERE,
76 base::StringPrintf("INSERT OR IGNORE INTO %s (date) VALUES(?)",
77 kStatisticsTableName).c_str()));
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,
85 base::StringPrintf(
86 "UPDATE %s SET "
87 "downloaded_precache_motivated_bytes = "
88 "downloaded_precache_motivated_bytes + ?, "
89 "downloaded_non_precache_bytes = "
90 "downloaded_non_precache_bytes + ?, "
91 "downloaded_non_precache_bytes_cellular = "
92 "downloaded_non_precache_bytes_cellular + ?, "
93 "saved_bytes = saved_bytes + ?, "
94 "saved_bytes_cellular = saved_bytes_cellular + ? "
95 "WHERE date = ?", kStatisticsTableName).c_str()));
96
97 update_statement.BindInt64(0, delta.downloaded_precache_motivated_bytes);
98 update_statement.BindInt64(1, delta.downloaded_non_precache_bytes);
99 update_statement.BindInt64(2, delta.downloaded_non_precache_bytes_cellular);
100 update_statement.BindInt64(3, delta.saved_bytes);
101 update_statement.BindInt64(4, delta.saved_bytes_cellular);
102 update_statement.BindInt64(5, GetKey(time_on_day));
103
104 update_statement.Run();
105 }
106
107 void PrecacheStatisticsTable::GetAllStatsUntil(
108 const base::Time& end_date, PrecacheStatisticsMap* stats_map) {
109 DCHECK(db_);
110 DCHECK(stats_map); // |stats_map| must not be NULL.
111 stats_map->clear();
112
113 Statement statement(db_->GetCachedStatement(
114 SQL_FROM_HERE, base::StringPrintf(
115 "SELECT date, "
116 "downloaded_precache_motivated_bytes, "
117 "downloaded_non_precache_bytes, "
118 "downloaded_non_precache_bytes_cellular, "
119 "saved_bytes, "
120 "saved_bytes_cellular "
121 "FROM %s WHERE date <= ?",
122 kStatisticsTableName).c_str()));
123 statement.BindInt64(0, GetKey(end_date));
124
125 while (statement.Step()) {
126 base::Time date = base::Time::FromInternalValue(statement.ColumnInt64(0));
127 PrecacheStatistics stats;
128
129 stats.downloaded_precache_motivated_bytes = statement.ColumnInt64(1);
130 stats.downloaded_non_precache_bytes = statement.ColumnInt64(2);
131 stats.downloaded_non_precache_bytes_cellular = statement.ColumnInt64(3);
132 stats.saved_bytes = statement.ColumnInt64(4);
133 stats.saved_bytes_cellular = statement.ColumnInt64(5);
134
135 (*stats_map)[date] = stats;
136 }
137 }
138
139 void PrecacheStatisticsTable::DeleteAllStatsUntil(const base::Time& end_date) {
140 DCHECK(db_);
141
142 Statement statement(db_->GetCachedStatement(
143 SQL_FROM_HERE, base::StringPrintf("DELETE FROM %s WHERE date <= ?",
144 kStatisticsTableName).c_str()));
145
146 statement.BindInt64(0, GetKey(end_date));
147 statement.Run();
148 }
149
150 void PrecacheStatisticsTable::CreateTableIfNonExistent() {
151 DCHECK(db_);
152 if (db_->DoesTableExist(kStatisticsTableName))
153 return;
154
155 if (!db_->Execute(base::StringPrintf(
156 "CREATE TABLE %s (date INTEGER, "
157 "downloaded_precache_motivated_bytes INTEGER DEFAULT 0, "
158 "downloaded_non_precache_bytes INTEGER DEFAULT 0, "
159 "downloaded_non_precache_bytes_cellular INTEGER DEFAULT 0, "
160 "saved_bytes INTEGER DEFAULT 0, "
161 "saved_bytes_cellular INTEGER DEFAULT 0, "
162 "PRIMARY KEY(date))",
163 kStatisticsTableName).c_str())) {
164 DLOG(WARNING) << "Could not create precache statistics table in database.";
165 }
166 }
167
168 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698