Chromium Code Reviews| Index: chrome/browser/budget_service/budget_database.cc |
| diff --git a/chrome/browser/budget_service/budget_database.cc b/chrome/browser/budget_service/budget_database.cc |
| index 9ec26da61754ebd1f66e8319ee2e62a8bbee339f..86388c983b24013c9d611429cfda82115239dc53 100644 |
| --- a/chrome/browser/budget_service/budget_database.cc |
| +++ b/chrome/browser/budget_service/budget_database.cc |
| @@ -5,6 +5,7 @@ |
| #include "chrome/browser/budget_service/budget_database.h" |
| #include "base/containers/adapters.h" |
|
Peter Beverloo
2016/08/26 14:56:43
nit: unused
harkness
2016/08/31 13:15:35
Done.
|
| +#include "base/metrics/histogram_macros.h" |
| #include "base/time/clock.h" |
| #include "base/time/default_clock.h" |
| #include "chrome/browser/budget_service/budget.pb.h" |
| @@ -25,8 +26,8 @@ namespace { |
| const char kDatabaseUMAName[] = "BudgetManager"; |
| // The default amount of time during which a budget will be valid. |
| -// This is 3 days = 72 hours. |
| -constexpr double kBudgetDurationInHours = 72; |
| +// This is 10 days = 240 hours. |
|
harkness
2016/08/25 19:51:19
I went ahead and kept it at 10 days. We can collec
Peter Beverloo
2016/08/26 14:56:43
Acknowledged.
|
| +constexpr double kBudgetDurationInHours = 240; |
| } // namespace |
| @@ -55,9 +56,8 @@ BudgetDatabase::BudgetDatabase( |
| BudgetDatabase::~BudgetDatabase() {} |
| -void BudgetDatabase::GetBudgetDetails( |
| - const GURL& origin, |
| - const GetBudgetDetailsCallback& callback) { |
| +void BudgetDatabase::GetBudgetDetails(const GURL& origin, |
| + const GetBudgetCallback& callback) { |
| DCHECK_EQ(origin.GetOrigin(), origin); |
| SyncCache(origin, |
| @@ -85,6 +85,14 @@ bool BudgetDatabase::IsCached(const GURL& origin) const { |
| return budget_map_.find(origin.spec()) != budget_map_.end(); |
| } |
| +double BudgetDatabase::GetBudget(const GURL& origin) { |
|
harkness
2016/08/25 19:51:19
We need this because the data structure for mojo i
|
| + double total = 0.0; |
| + BudgetInfo& info = budget_map_[origin.spec()]; |
| + for (const BudgetChunk& chunk : info.chunks) |
| + total += chunk.amount; |
| + return total; |
| +} |
| + |
| void BudgetDatabase::AddToCache( |
| const GURL& origin, |
| const AddToCacheCallback& callback, |
| @@ -117,36 +125,44 @@ void BudgetDatabase::AddToCache( |
| callback.Run(success); |
| } |
| -void BudgetDatabase::GetBudgetAfterSync( |
| - const GURL& origin, |
| - const GetBudgetDetailsCallback& callback, |
| - bool success) { |
| +void BudgetDatabase::GetBudgetAfterSync(const GURL& origin, |
| + const GetBudgetCallback& callback, |
| + bool success) { |
| + mojo::Array<blink::mojom::BudgetStatePtr> predictions; |
| + |
| // If the database wasn't able to read the information, return the |
| - // failure and an empty BudgetPrediction. |
| + // failure and an empty predictions array. |
| if (!success) { |
| - callback.Run(success, BudgetPrediction()); |
| + // TODO(harkness): Add a status enum to this call. |
| + callback.Run(std::move(predictions)); |
|
harkness
2016/08/25 19:51:19
I'll add this when I add Reserve to the mojo servi
|
| return; |
| } |
| // Now, build up the BudgetExpection. This is different from the format |
| // in which the cache stores the data. The cache stores chunks of budget and |
| - // when that budget expires. The BudgetPrediction describes a set of times |
| + // when that budget expires. The mojo array describes a set of times |
| // and the budget at those times. |
| - BudgetPrediction prediction; |
| - double total = 0; |
| + double total = GetBudget(origin); |
| - // Starting with the chunks that expire the farthest in the future, build up |
| - // the budget predictions for those future times. |
| + // Always add one entry at the front of the list for the total budget now. |
| + blink::mojom::BudgetStatePtr prediction(blink::mojom::BudgetState::New()); |
| + prediction->budget_at = total; |
| + prediction->time = clock_->Now().ToDoubleT(); |
| + predictions.push_back(std::move(prediction)); |
| + |
| + // Starting with the soonest expiring chunks, add entries for the |
| + // expiration times going forward. |
| const BudgetChunks& chunks = budget_map_[origin.spec()].chunks; |
| - for (const auto& chunk : base::Reversed(chunks)) { |
| - prediction.emplace_front(total, chunk.expiration); |
| - total += chunk.amount; |
| + for (const auto& chunk : chunks) { |
| + blink::mojom::BudgetStatePtr prediction(blink::mojom::BudgetState::New()); |
| + total -= chunk.amount; |
| + prediction->budget_at = total; |
| + prediction->time = chunk.expiration.ToDoubleT(); |
| + predictions.push_back(std::move(prediction)); |
| } |
| - // Always add one entry at the front of the list for the total budget now. |
| - prediction.emplace_front(total, clock_->Now()); |
| - |
| - callback.Run(true /* success */, prediction); |
| + // TODO(harkness) Add status enum. |
| + callback.Run(std::move(predictions)); |
| } |
| void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| @@ -158,6 +174,10 @@ void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| return; |
| } |
| + // Get the current SES score, to generate UMA.. |
| + SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| + double score = service->GetScore(origin); |
| + |
| // Walk the list of budget chunks to see if the origin has enough budget. |
| double total = 0; |
| BudgetInfo& info = budget_map_[origin.spec()]; |
| @@ -165,8 +185,11 @@ void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| total += chunk.amount; |
| if (total < amount) { |
| + UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", score); |
| callback.Run(false /* success */); |
| return; |
| + } else if (total < amount * 2) { |
| + UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForLowBudgetOrigin", score); |
|
Peter Beverloo
2016/08/26 14:56:43
We tried to hard to keep these out of here :( Let'
harkness
2016/08/31 13:15:35
Acknowledged.
|
| } |
| // Walk the chunks and remove enough budget to cover the needed amount. |
| @@ -294,14 +317,16 @@ void BudgetDatabase::AddEngagementBudget(const GURL& origin) { |
| base::Time expiration = |
| clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours); |
| budget_map_[origin.spec()].chunks.emplace_back(ratio * score, expiration); |
| + |
| + // Any time we award engagement budget, which is done at most once an hour |
| + // whenever any budget action is taken, record the budget. |
| + double budget = GetBudget(origin); |
| + UMA_HISTOGRAM_COUNTS_100("PushMessaging.BackgroundBudget", budget); |
| } |
| // Cleans up budget in the cache. Relies on the caller eventually writing the |
| // cache back to the database. |
| bool BudgetDatabase::CleanupExpiredBudget(const GURL& origin) { |
| - if (!IsCached(origin)) |
| - return false; |
| - |
| base::Time now = clock_->Now(); |
| BudgetChunks& chunks = budget_map_[origin.spec()].chunks; |
| auto cleanup_iter = chunks.begin(); |