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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/precache/core/precache_statistics_table.cc
diff --git a/components/precache/core/precache_statistics_table.cc b/components/precache/core/precache_statistics_table.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aaf98ad6ed18ebe592bfadf81dc5e7c842c06347
--- /dev/null
+++ b/components/precache/core/precache_statistics_table.cc
@@ -0,0 +1,168 @@
+// Copyright 2013 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.
+
+#include "components/precache/core/precache_statistics_table.h"
+
+#include "base/logging.h"
+#include "base/strings/stringprintf.h"
+#include "sql/connection.h"
+#include "sql/statement.h"
+
+using sql::Statement;
+
+namespace {
+
+const char kStatisticsTableName[] = "precache_statistics";
+
+// Rounds |time_on_day| down to midnight of the start of the day of
+// |time_on_day|, and returns the internal value.
+int64 GetKey(const base::Time& time_on_day) {
+ 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
+}
+
+} // namespace
+
+namespace precache {
+
+PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics()
+ : downloaded_precache_motivated_bytes(0),
+ downloaded_non_precache_bytes(0),
+ downloaded_non_precache_bytes_cellular(0),
+ saved_bytes(0),
+ saved_bytes_cellular(0) {}
+
+PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics(
+ int64 downloaded_precache_motivated_bytes,
+ int64 downloaded_non_precache_bytes,
+ int64 downloaded_non_precache_bytes_cellular, int64 saved_bytes,
+ int64 saved_bytes_cellular)
+ : downloaded_precache_motivated_bytes(downloaded_precache_motivated_bytes),
+ downloaded_non_precache_bytes(downloaded_non_precache_bytes),
+ downloaded_non_precache_bytes_cellular(
+ downloaded_non_precache_bytes_cellular),
+ saved_bytes(saved_bytes),
+ saved_bytes_cellular(saved_bytes_cellular) {}
+
+bool PrecacheStatisticsTable::PrecacheStatistics::operator==(
+ const PrecacheStatistics& other) const {
+ return downloaded_precache_motivated_bytes ==
+ other.downloaded_precache_motivated_bytes &&
+ downloaded_non_precache_bytes == other.downloaded_non_precache_bytes &&
+ downloaded_non_precache_bytes_cellular ==
+ other.downloaded_non_precache_bytes_cellular &&
+ saved_bytes == other.saved_bytes &&
+ saved_bytes_cellular == other.saved_bytes_cellular;
+}
+
+PrecacheStatisticsTable::PrecacheStatisticsTable() : db_(NULL) {}
+
+PrecacheStatisticsTable::~PrecacheStatisticsTable() {}
+
+void PrecacheStatisticsTable::Init(sql::Connection* db) {
+ DCHECK(!db_); // Init must only be called once.
+ DCHECK(db); // The database connection must be non-NULL.
+ db_ = db;
+ CreateTableIfNonExistent();
+}
+
+void PrecacheStatisticsTable::IncreaseDailyStats(
+ const base::Time& time_on_day, const PrecacheStatistics& delta) {
+ DCHECK(db_);
+
+ // Create a row for that day if there is no existing one.
+ Statement insert_statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf("INSERT OR IGNORE INTO %s (date) VALUES(?)",
+ kStatisticsTableName).c_str()));
+
+ insert_statement.BindInt64(0, GetKey(time_on_day));
+ insert_statement.Run();
+
+ // Increase the recorded statistics for that day.
+ Statement update_statement(db_->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf(
+ "UPDATE %s SET "
+ "downloaded_precache_motivated_bytes = "
+ "downloaded_precache_motivated_bytes + ?, "
+ "downloaded_non_precache_bytes = "
+ "downloaded_non_precache_bytes + ?, "
+ "downloaded_non_precache_bytes_cellular = "
+ "downloaded_non_precache_bytes_cellular + ?, "
+ "saved_bytes = saved_bytes + ?, "
+ "saved_bytes_cellular = saved_bytes_cellular + ? "
+ "WHERE date = ?", kStatisticsTableName).c_str()));
+
+ update_statement.BindInt64(0, delta.downloaded_precache_motivated_bytes);
+ update_statement.BindInt64(1, delta.downloaded_non_precache_bytes);
+ update_statement.BindInt64(2, delta.downloaded_non_precache_bytes_cellular);
+ update_statement.BindInt64(3, delta.saved_bytes);
+ update_statement.BindInt64(4, delta.saved_bytes_cellular);
+ update_statement.BindInt64(5, GetKey(time_on_day));
+
+ update_statement.Run();
+}
+
+void PrecacheStatisticsTable::GetAllStatsUntil(
+ const base::Time& end_date, PrecacheStatisticsMap* stats_map) {
+ DCHECK(db_);
+ DCHECK(stats_map); // |stats_map| must not be NULL.
+ stats_map->clear();
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE, base::StringPrintf(
+ "SELECT date, "
+ "downloaded_precache_motivated_bytes, "
+ "downloaded_non_precache_bytes, "
+ "downloaded_non_precache_bytes_cellular, "
+ "saved_bytes, "
+ "saved_bytes_cellular "
+ "FROM %s WHERE date <= ?",
+ kStatisticsTableName).c_str()));
+ statement.BindInt64(0, GetKey(end_date));
+
+ while (statement.Step()) {
+ base::Time date = base::Time::FromInternalValue(statement.ColumnInt64(0));
+ PrecacheStatistics stats;
+
+ stats.downloaded_precache_motivated_bytes = statement.ColumnInt64(1);
+ stats.downloaded_non_precache_bytes = statement.ColumnInt64(2);
+ stats.downloaded_non_precache_bytes_cellular = statement.ColumnInt64(3);
+ stats.saved_bytes = statement.ColumnInt64(4);
+ stats.saved_bytes_cellular = statement.ColumnInt64(5);
+
+ (*stats_map)[date] = stats;
+ }
+}
+
+void PrecacheStatisticsTable::DeleteAllStatsUntil(const base::Time& end_date) {
+ DCHECK(db_);
+
+ Statement statement(db_->GetCachedStatement(
+ SQL_FROM_HERE, base::StringPrintf("DELETE FROM %s WHERE date <= ?",
+ kStatisticsTableName).c_str()));
+
+ statement.BindInt64(0, GetKey(end_date));
+ statement.Run();
+}
+
+void PrecacheStatisticsTable::CreateTableIfNonExistent() {
+ DCHECK(db_);
+ if (db_->DoesTableExist(kStatisticsTableName))
+ return;
+
+ if (!db_->Execute(base::StringPrintf(
+ "CREATE TABLE %s (date INTEGER, "
+ "downloaded_precache_motivated_bytes INTEGER DEFAULT 0, "
+ "downloaded_non_precache_bytes INTEGER DEFAULT 0, "
+ "downloaded_non_precache_bytes_cellular INTEGER DEFAULT 0, "
+ "saved_bytes INTEGER DEFAULT 0, "
+ "saved_bytes_cellular INTEGER DEFAULT 0, "
+ "PRIMARY KEY(date))",
+ kStatisticsTableName).c_str())) {
+ DLOG(WARNING) << "Could not create precache statistics table in database.";
+ }
+}
+
+} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698