| OLD | NEW |
| (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 "base/time/time.h" |
| 10 #include "sql/connection.h" |
| 11 #include "sql/statement.h" |
| 12 |
| 13 using sql::Statement; |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kStatisticsTableName[] = "precache_statistics"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 namespace precache { |
| 22 |
| 23 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics() |
| 24 : precached_bytes(0), |
| 25 downloaded_bytes(0), |
| 26 downloaded_bytes_cellular(0), |
| 27 saved_bytes(0), |
| 28 saved_bytes_cellular(0) {} |
| 29 |
| 30 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics( |
| 31 int64 precached_bytes, int64 downloaded_bytes, |
| 32 int64 downloaded_bytes_cellular, int64 saved_bytes, |
| 33 int64 saved_bytes_cellular) |
| 34 : precached_bytes(precached_bytes), |
| 35 downloaded_bytes(downloaded_bytes), |
| 36 downloaded_bytes_cellular(downloaded_bytes_cellular), |
| 37 saved_bytes(saved_bytes), |
| 38 saved_bytes_cellular(saved_bytes_cellular) {} |
| 39 |
| 40 bool PrecacheStatisticsTable::PrecacheStatistics::operator==( |
| 41 const PrecacheStatistics& other) const { |
| 42 return precached_bytes == other.precached_bytes && |
| 43 downloaded_bytes == other.downloaded_bytes && |
| 44 downloaded_bytes_cellular == other.downloaded_bytes_cellular && |
| 45 saved_bytes == other.saved_bytes && |
| 46 saved_bytes_cellular == other.saved_bytes_cellular; |
| 47 } |
| 48 |
| 49 PrecacheStatisticsTable::PrecacheStatisticsTable() : db_(NULL) {} |
| 50 |
| 51 PrecacheStatisticsTable::~PrecacheStatisticsTable() {} |
| 52 |
| 53 void PrecacheStatisticsTable::Init(sql::Connection* db) { |
| 54 DCHECK(!db_); // Init must only be called once. |
| 55 DCHECK(db); // The database connection must be non-NULL. |
| 56 db_ = db; |
| 57 CreateTableIfNonExistent(); |
| 58 } |
| 59 |
| 60 void PrecacheStatisticsTable::IncreaseDailyStats( |
| 61 const base::Time& time_on_day, const PrecacheStatistics& stats_change) { |
| 62 DCHECK(db_); |
| 63 |
| 64 // Create a row for that day if there is no existing one. |
| 65 Statement insert_statement(db_->GetCachedStatement( |
| 66 SQL_FROM_HERE, |
| 67 base::StringPrintf("INSERT OR IGNORE INTO %s (date) VALUES(?)", |
| 68 kStatisticsTableName).c_str())); |
| 69 |
| 70 insert_statement.BindInt64(0, GetKey(time_on_day)); |
| 71 insert_statement.Run(); |
| 72 |
| 73 // Increase the recorded statistics for that day. |
| 74 Statement update_statement(db_->GetCachedStatement( |
| 75 SQL_FROM_HERE, |
| 76 base::StringPrintf( |
| 77 "UPDATE %s SET precached_bytes = precached_bytes + ?, " |
| 78 "downloaded_bytes = downloaded_bytes + ?, " |
| 79 "downloaded_bytes_cellular = downloaded_bytes_cellular + ?, " |
| 80 "saved_bytes = saved_bytes + ?, " |
| 81 "saved_bytes_cellular = saved_bytes_cellular + ? " |
| 82 "WHERE date = ?", kStatisticsTableName).c_str())); |
| 83 |
| 84 update_statement.BindInt64(0, stats_change.precached_bytes); |
| 85 update_statement.BindInt64(1, stats_change.downloaded_bytes); |
| 86 update_statement.BindInt64(2, stats_change.downloaded_bytes_cellular); |
| 87 update_statement.BindInt64(3, stats_change.saved_bytes); |
| 88 update_statement.BindInt64(4, stats_change.saved_bytes_cellular); |
| 89 update_statement.BindInt64(5, GetKey(time_on_day)); |
| 90 |
| 91 update_statement.Run(); |
| 92 } |
| 93 |
| 94 void PrecacheStatisticsTable::GetAllStatsBetween(const base::Time& start_time, |
| 95 const base::Time& end_time, |
| 96 PrecacheStatisticsMap* map) { |
| 97 DCHECK(db_); |
| 98 |
| 99 Statement statement(db_->GetCachedStatement( |
| 100 SQL_FROM_HERE, |
| 101 base::StringPrintf( |
| 102 "SELECT date, precached_bytes, downloaded_bytes, " |
| 103 "downloaded_bytes_cellular, saved_bytes, saved_bytes_cellular FROM " |
| 104 "%s WHERE date >= ? and date <= ?", |
| 105 kStatisticsTableName).c_str())); |
| 106 |
| 107 statement.BindInt64(0, GetKey(start_time)); |
| 108 statement.BindInt64(1, GetKey(end_time)); |
| 109 |
| 110 while (statement.Step()) { |
| 111 base::Time date = base::Time::FromInternalValue(statement.ColumnInt64(0)); |
| 112 PrecacheStatistics stats; |
| 113 |
| 114 stats.precached_bytes = statement.ColumnInt64(1); |
| 115 stats.downloaded_bytes = statement.ColumnInt64(2); |
| 116 stats.downloaded_bytes_cellular = statement.ColumnInt64(3); |
| 117 stats.saved_bytes = statement.ColumnInt64(4); |
| 118 stats.saved_bytes_cellular = statement.ColumnInt64(5); |
| 119 |
| 120 (*map)[date] = stats; |
| 121 } |
| 122 } |
| 123 |
| 124 void PrecacheStatisticsTable::DeleteAllStatsBetween( |
| 125 const base::Time& delete_begin, const base::Time& delete_end) { |
| 126 DCHECK(db_); |
| 127 |
| 128 Statement statement(db_->GetCachedStatement( |
| 129 SQL_FROM_HERE, |
| 130 base::StringPrintf("DELETE FROM %s WHERE date >= ? AND date <= ?", |
| 131 kStatisticsTableName).c_str())); |
| 132 |
| 133 statement.BindInt64(0, GetKey(delete_begin)); |
| 134 statement.BindInt64(1, GetKey(delete_end)); |
| 135 |
| 136 statement.Run(); |
| 137 } |
| 138 |
| 139 // static |
| 140 int64 PrecacheStatisticsTable::GetKey(const base::Time& time_on_day) { |
| 141 return time_on_day.LocalMidnight().ToInternalValue(); |
| 142 } |
| 143 |
| 144 void PrecacheStatisticsTable::CreateTableIfNonExistent() { |
| 145 DCHECK(db_); |
| 146 if (db_->DoesTableExist(kStatisticsTableName)) |
| 147 return; |
| 148 |
| 149 if (!db_->Execute(base::StringPrintf( |
| 150 "CREATE TABLE %s (date INTEGER, " |
| 151 "precached_bytes INTEGER DEFAULT 0, " |
| 152 "downloaded_bytes INTEGER DEFAULT 0, " |
| 153 "downloaded_bytes_cellular INTEGER DEFAULT 0, " |
| 154 "saved_bytes INTEGER DEFAULT 0, " |
| 155 "saved_bytes_cellular INTEGER DEFAULT 0, " |
| 156 "PRIMARY KEY(date))", |
| 157 kStatisticsTableName).c_str())) { |
| 158 DLOG(WARNING) << "Could not create precache statistics table in database."; |
| 159 } |
| 160 } |
| 161 |
| 162 } // namespace precache |
| OLD | NEW |