| 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 "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 // Returns the number of 24 hour intervals that have passed between time 0 and |
| 19 // |time_on_day|, rounded down. |
| 20 int64 GetKey(const base::Time& time_on_day) { |
| 21 return (time_on_day - base::Time()).InDays(); |
| 22 } |
| 23 |
| 24 // Returns the time of the beginning of the 24 hour interval specified by |key|. |
| 25 base::Time FromKey(int64 key) { |
| 26 return base::Time() + base::TimeDelta::FromDays(key); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 namespace precache { |
| 32 |
| 33 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics() |
| 34 : downloaded_precache_motivated_bytes(0), |
| 35 downloaded_non_precache_bytes(0), |
| 36 downloaded_non_precache_bytes_cellular(0), |
| 37 saved_bytes(0), |
| 38 saved_bytes_cellular(0) {} |
| 39 |
| 40 PrecacheStatisticsTable::PrecacheStatistics::PrecacheStatistics( |
| 41 int64 downloaded_precache_motivated_bytes, |
| 42 int64 downloaded_non_precache_bytes, |
| 43 int64 downloaded_non_precache_bytes_cellular, int64 saved_bytes, |
| 44 int64 saved_bytes_cellular) |
| 45 : downloaded_precache_motivated_bytes(downloaded_precache_motivated_bytes), |
| 46 downloaded_non_precache_bytes(downloaded_non_precache_bytes), |
| 47 downloaded_non_precache_bytes_cellular( |
| 48 downloaded_non_precache_bytes_cellular), |
| 49 saved_bytes(saved_bytes), |
| 50 saved_bytes_cellular(saved_bytes_cellular) {} |
| 51 |
| 52 bool PrecacheStatisticsTable::PrecacheStatistics::operator==( |
| 53 const PrecacheStatistics& other) const { |
| 54 return downloaded_precache_motivated_bytes == |
| 55 other.downloaded_precache_motivated_bytes && |
| 56 downloaded_non_precache_bytes == other.downloaded_non_precache_bytes && |
| 57 downloaded_non_precache_bytes_cellular == |
| 58 other.downloaded_non_precache_bytes_cellular && |
| 59 saved_bytes == other.saved_bytes && |
| 60 saved_bytes_cellular == other.saved_bytes_cellular; |
| 61 } |
| 62 |
| 63 PrecacheStatisticsTable::PrecacheStatisticsTable() : db_(NULL) {} |
| 64 |
| 65 PrecacheStatisticsTable::~PrecacheStatisticsTable() {} |
| 66 |
| 67 void PrecacheStatisticsTable::Init(sql::Connection* db) { |
| 68 DCHECK(!db_); // Init must only be called once. |
| 69 DCHECK(db); // The database connection must be non-NULL. |
| 70 db_ = db; |
| 71 CreateTableIfNonExistent(); |
| 72 } |
| 73 |
| 74 void PrecacheStatisticsTable::IncreaseDailyStats( |
| 75 const base::Time& time_on_day, const PrecacheStatistics& delta) { |
| 76 DCHECK(db_); |
| 77 |
| 78 // Create a row for that day if there is no existing one. |
| 79 Statement insert_statement(db_->GetCachedStatement( |
| 80 SQL_FROM_HERE, |
| 81 base::StringPrintf("INSERT OR IGNORE INTO %s (date) VALUES(?)", |
| 82 kStatisticsTableName).c_str())); |
| 83 |
| 84 insert_statement.BindInt64(0, GetKey(time_on_day)); |
| 85 insert_statement.Run(); |
| 86 |
| 87 // Increase the recorded statistics for that day. |
| 88 Statement update_statement(db_->GetCachedStatement( |
| 89 SQL_FROM_HERE, |
| 90 base::StringPrintf( |
| 91 "UPDATE %s SET " |
| 92 "downloaded_precache_motivated_bytes = " |
| 93 "downloaded_precache_motivated_bytes + ?, " |
| 94 "downloaded_non_precache_bytes = " |
| 95 "downloaded_non_precache_bytes + ?, " |
| 96 "downloaded_non_precache_bytes_cellular = " |
| 97 "downloaded_non_precache_bytes_cellular + ?, " |
| 98 "saved_bytes = saved_bytes + ?, " |
| 99 "saved_bytes_cellular = saved_bytes_cellular + ? " |
| 100 "WHERE date = ?", kStatisticsTableName).c_str())); |
| 101 |
| 102 update_statement.BindInt64(0, delta.downloaded_precache_motivated_bytes); |
| 103 update_statement.BindInt64(1, delta.downloaded_non_precache_bytes); |
| 104 update_statement.BindInt64(2, delta.downloaded_non_precache_bytes_cellular); |
| 105 update_statement.BindInt64(3, delta.saved_bytes); |
| 106 update_statement.BindInt64(4, delta.saved_bytes_cellular); |
| 107 update_statement.BindInt64(5, GetKey(time_on_day)); |
| 108 |
| 109 update_statement.Run(); |
| 110 } |
| 111 |
| 112 void PrecacheStatisticsTable::GetOldStats(const base::Time& current_time, |
| 113 PrecacheStatisticsMap* stats_map) { |
| 114 DCHECK(db_); |
| 115 DCHECK(stats_map); // |stats_map| must not be NULL. |
| 116 stats_map->clear(); |
| 117 |
| 118 Statement statement(db_->GetCachedStatement( |
| 119 SQL_FROM_HERE, base::StringPrintf( |
| 120 "SELECT date, " |
| 121 "downloaded_precache_motivated_bytes, " |
| 122 "downloaded_non_precache_bytes, " |
| 123 "downloaded_non_precache_bytes_cellular, " |
| 124 "saved_bytes, " |
| 125 "saved_bytes_cellular " |
| 126 "FROM %s WHERE date < ?", |
| 127 kStatisticsTableName).c_str())); |
| 128 statement.BindInt64(0, GetKey(current_time)); |
| 129 |
| 130 while (statement.Step()) { |
| 131 base::Time date = FromKey(statement.ColumnInt64(0)); |
| 132 PrecacheStatistics stats; |
| 133 |
| 134 stats.downloaded_precache_motivated_bytes = statement.ColumnInt64(1); |
| 135 stats.downloaded_non_precache_bytes = statement.ColumnInt64(2); |
| 136 stats.downloaded_non_precache_bytes_cellular = statement.ColumnInt64(3); |
| 137 stats.saved_bytes = statement.ColumnInt64(4); |
| 138 stats.saved_bytes_cellular = statement.ColumnInt64(5); |
| 139 |
| 140 (*stats_map)[date] = stats; |
| 141 } |
| 142 } |
| 143 |
| 144 void PrecacheStatisticsTable::DeleteOldStats(const base::Time& current_time) { |
| 145 DCHECK(db_); |
| 146 |
| 147 Statement statement(db_->GetCachedStatement( |
| 148 SQL_FROM_HERE, base::StringPrintf("DELETE FROM %s WHERE date < ?", |
| 149 kStatisticsTableName).c_str())); |
| 150 |
| 151 statement.BindInt64(0, GetKey(current_time)); |
| 152 statement.Run(); |
| 153 } |
| 154 |
| 155 void PrecacheStatisticsTable::CreateTableIfNonExistent() { |
| 156 DCHECK(db_); |
| 157 if (db_->DoesTableExist(kStatisticsTableName)) |
| 158 return; |
| 159 |
| 160 if (!db_->Execute(base::StringPrintf( |
| 161 "CREATE TABLE %s (date INTEGER, " |
| 162 "downloaded_precache_motivated_bytes INTEGER DEFAULT 0, " |
| 163 "downloaded_non_precache_bytes INTEGER DEFAULT 0, " |
| 164 "downloaded_non_precache_bytes_cellular INTEGER DEFAULT 0, " |
| 165 "saved_bytes INTEGER DEFAULT 0, " |
| 166 "saved_bytes_cellular INTEGER DEFAULT 0, " |
| 167 "PRIMARY KEY(date))", |
| 168 kStatisticsTableName).c_str())) { |
| 169 DLOG(WARNING) << "Could not create precache statistics table in database."; |
| 170 } |
| 171 } |
| 172 |
| 173 } // namespace precache |
| OLD | NEW |