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..d5903375758c72047108df3dbf38ef433d8a0a3a 100644 |
--- a/chrome/browser/budget_service/budget_database.cc |
+++ b/chrome/browser/budget_service/budget_database.cc |
@@ -4,7 +4,7 @@ |
#include "chrome/browser/budget_service/budget_database.h" |
-#include "base/containers/adapters.h" |
+#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 +25,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. |
+constexpr double kBudgetDurationInHours = 240; |
} // namespace |
@@ -55,9 +55,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 +84,14 @@ bool BudgetDatabase::IsCached(const GURL& origin) const { |
return budget_map_.find(origin.spec()) != budget_map_.end(); |
} |
+double BudgetDatabase::GetBudget(const GURL& origin) { |
+ double total = 0.0; |
Peter Beverloo
2016/09/05 14:39:03
nit: s/0.0/0/
harkness
2016/09/06 13:28:39
Done.
|
+ 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 +124,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)); |
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)); |
} |
Peter Beverloo
2016/09/05 14:39:03
Can we DCHECK_EQ(0, total)?
harkness
2016/09/06 13:28:39
Done.
|
- // 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 +173,10 @@ void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
return; |
} |
+ // Get the current SES score, to generate UMA.. |
Peter Beverloo
2016/09/05 14:39:03
nit: .. -> .
harkness
2016/09/06 13:28:39
Done.
|
+ 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 +184,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); |
} |
// Walk the chunks and remove enough budget to cover the needed amount. |
@@ -294,14 +316,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(); |