Chromium Code Reviews| 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_database.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/metrics/histogram.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "components/precache/core/precache_statistics_table.h" | |
| 12 #include "components/precache/core/precache_url_table.h" | |
| 13 #include "sql/connection.h" | |
| 14 #include "url/gurl.h" | |
| 15 | |
| 16 namespace precache { | |
| 17 | |
| 18 namespace { | |
|
bengr
2013/10/23 19:03:36
Again, any reason your anonymous namespace is decl
sclittle
2013/10/24 22:11:38
Moved anon namespace out of precache namespace.
| |
| 19 | |
| 20 // The number of days old that an entry in the precache URL table can be before | |
| 21 // it is considered "old" and is removed from the table. | |
| 22 const int64 kPrecacheHistoryExpiryPeriodDays = 60; | |
| 23 | |
| 24 int GetPercentageForUMA(int64 numerator, int64 denomenator) { | |
|
bengr
2013/10/23 19:03:36
Does a function like this really not exist in chro
sclittle
2013/10/24 22:11:38
On second thought, it doesn't really make sense to
| |
| 25 if (denomenator == 0) { | |
| 26 return 0; | |
| 27 } | |
| 28 return numerator * 100.0 / denomenator; | |
| 29 } | |
| 30 | |
| 31 void RecordSingleDayPrecacheUMA( | |
| 32 const PrecacheStatisticsTable::PrecacheStatistics& stats) { | |
| 33 UMA_HISTOGRAM_COUNTS("Precache.DailyPrecachedKB", | |
|
bengr
2013/10/23 19:03:36
You also need to describe these UMA in histograms.
sclittle
2013/10/24 22:11:38
Done.
| |
| 34 stats.precached_bytes / 1000); | |
|
bengr
2013/10/23 19:03:36
A KB is 1024 B. Why not report bytes instead? Also
sclittle
2013/10/24 22:11:38
Changed to report bytes with max_range at 1 billio
| |
| 35 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedKB", | |
| 36 stats.downloaded_bytes / 1000); | |
| 37 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedKB.Cellular", | |
| 38 stats.downloaded_bytes_cellular / 1000); | |
| 39 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB", stats.saved_bytes / 1000); | |
| 40 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB.Cellular", | |
| 41 stats.saved_bytes_cellular / 1000); | |
| 42 | |
| 43 UMA_HISTOGRAM_PERCENTAGE( | |
| 44 "Precache.DailySavingsPercentage", | |
| 45 GetPercentageForUMA(stats.saved_bytes, | |
| 46 stats.saved_bytes + stats.downloaded_bytes)); | |
| 47 UMA_HISTOGRAM_PERCENTAGE( | |
| 48 "Precache.DailySavingsPercentage.Cellular", | |
| 49 GetPercentageForUMA( | |
| 50 stats.saved_bytes_cellular, | |
| 51 stats.saved_bytes_cellular + stats.downloaded_bytes_cellular)); | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 PrecacheDatabase::PrecacheDatabase() | |
| 57 : precache_url_table_(new PrecacheURLTable()), | |
| 58 precache_statistics_table_(new PrecacheStatisticsTable()) { | |
| 59 DetachFromThread(); | |
| 60 } | |
| 61 | |
| 62 PrecacheDatabase::~PrecacheDatabase() { | |
| 63 // Since the PrecacheDatabase is refcounted, it will only be deleted if there | |
| 64 // are no references remaining to it, meaning that it is not in use. Thus, it | |
| 65 // is save to delete it, regardless of what thread we are on. | |
|
bengr
2013/10/23 19:03:36
is safe
sclittle
2013/10/24 22:11:38
Done.
| |
| 66 DetachFromThread(); | |
| 67 } | |
| 68 | |
| 69 void PrecacheDatabase::Init(sql::Connection* db) { | |
| 70 DCHECK(CalledOnValidThread()); | |
| 71 | |
| 72 DCHECK(!db_); // Init must only be called once. | |
| 73 DCHECK(db); // The database connection must be non-NULL. | |
| 74 | |
| 75 db_.reset(db); | |
| 76 precache_url_table_->Init(db_.get()); | |
| 77 precache_statistics_table_->Init(db_.get()); | |
| 78 } | |
| 79 | |
| 80 void PrecacheDatabase::FlushOldStats(base::Time flush_end_date) { | |
| 81 DCHECK(CalledOnValidThread()); | |
| 82 DCHECK(db_); | |
| 83 | |
| 84 // Delete old precache history that has expired. | |
| 85 precache_url_table_->DeleteAllPrecachedBetween( | |
| 86 base::Time::FromInternalValue(0), | |
| 87 flush_end_date - | |
| 88 base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); | |
| 89 | |
| 90 std::map<base::Time, PrecacheStatisticsTable::PrecacheStatistics> stats_map; | |
| 91 precache_statistics_table_->GetAllStatsBetween( | |
| 92 base::Time::FromInternalValue(0), flush_end_date, &stats_map); | |
| 93 | |
| 94 for (std::map<base::Time, | |
| 95 PrecacheStatisticsTable::PrecacheStatistics>::const_iterator | |
| 96 it = stats_map.begin(); | |
| 97 it != stats_map.end(); ++it) { | |
| 98 RecordSingleDayPrecacheUMA(it->second); | |
|
bengr
2013/10/23 19:03:36
I don't think we should record UMA for days where
sclittle
2013/10/24 22:11:38
There won't be any rows in the statistics table fo
| |
| 99 } | |
| 100 | |
| 101 precache_statistics_table_->DeleteAllStatsBetween( | |
| 102 base::Time::FromInternalValue(0), flush_end_date); | |
| 103 } | |
| 104 | |
| 105 void PrecacheDatabase::RecordURLFetched(GURL url, base::Time fetch_time, | |
| 106 int64 size, bool was_cached, | |
| 107 bool is_precaching, bool is_cellular) { | |
| 108 DCHECK(CalledOnValidThread()); | |
| 109 DCHECK(db_); | |
| 110 | |
| 111 if (is_precaching) { | |
| 112 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 113 // If a precache fetch came from the cache, and the user browsed the | |
| 114 // resource recently, then the precache fetch did nothing, so ignore it. | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 if (!was_cached) { | |
| 119 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 120 stats.precached_bytes = size; | |
| 121 precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats); | |
| 122 } | |
| 123 | |
| 124 precache_url_table_->AddURL(url, fetch_time); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 if (!was_cached) { | |
| 129 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 130 stats.downloaded_bytes = size; | |
| 131 if (is_cellular) { | |
| 132 stats.downloaded_bytes_cellular = size; | |
| 133 } | |
| 134 precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats); | |
| 135 | |
| 136 // Since the fetch was over the network, delete any record of it having been | |
| 137 // prefetched from the URL table. If it had been precached, the resource | |
| 138 // must have expired. | |
| 139 precache_url_table_->DeleteURL(url); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 if (was_cached && precache_url_table_->HasURL(url)) { | |
| 144 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 145 stats.saved_bytes = size; | |
| 146 if (is_cellular) { | |
| 147 stats.saved_bytes_cellular = size; | |
| 148 } | |
| 149 precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats); | |
| 150 | |
| 151 precache_url_table_->DeleteURL(url); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 } // namespace precache | |
| OLD | NEW |