Skip to content

Commit

Permalink
Check every 5 minutes if we need to ping the update server for today
Browse files Browse the repository at this point in the history
We were previously pinging the update server once an hour. However,
that approach will undercount in the following scenario: a user's
laptop sends a ping, the user closes the laptop, and the next day the
user runs Brave for less than one hour.
  • Loading branch information
emerick committed Dec 3, 2018
1 parent d3d7e3a commit 1a412ea
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
2 changes: 2 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ source_set("browser_process") {
"brave_stats_updater.h",
"brave_stats_updater_params.cc",
"brave_stats_updater_params.h",
"brave_stats_updater_util.cc",
"brave_stats_updater_util.h",
"brave_tab_helpers.cc",
"brave_tab_helpers.h",
"brave_rewards/donations_dialog.cc",
Expand Down
20 changes: 12 additions & 8 deletions browser/brave_stats_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/sys_info.h"
#include "brave/browser/brave_stats_updater_params.h"
#include "brave/browser/brave_stats_updater_util.h"
#include "brave/browser/version_info.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/browser_process.h"
Expand All @@ -25,8 +26,9 @@
// Ping the update server shortly after startup (units are seconds).
const int kUpdateServerStartupPingDelay = 3;

// Ping the update server once an hour (units are seconds).
const int kUpdateServerPeriodicPingFrequency = 60 * 60;
// Every five minutes, check if we need to ping the update server for
// today (units are seconds).
const int kUpdateServerPeriodicPingFrequency = 5 * 60;

namespace {

Expand Down Expand Up @@ -112,7 +114,7 @@ void BraveStatsUpdater::Start() {
server_ping_periodic_timer_->Start(
FROM_HERE,
base::TimeDelta::FromSeconds(kUpdateServerPeriodicPingFrequency), this,
&BraveStatsUpdater::OnServerPingPeriodicTimerFired);
&BraveStatsUpdater::OnServerPingTimerFired);
DCHECK(server_ping_periodic_timer_->IsRunning());
}

Expand Down Expand Up @@ -155,11 +157,13 @@ void BraveStatsUpdater::OnSimpleLoaderComplete(
VLOG(1) << "Brave stats ping, url: " << final_url.spec();
}

void BraveStatsUpdater::OnServerPingStartupTimerFired() {
SendServerPing();
}
void BraveStatsUpdater::OnServerPingTimerFired() {
// If we already pinged the stats server today, then we're done.
std::string today_ymd = brave::GetDateAsYMD(base::Time::Now());
std::string last_check_ymd = pref_service_->GetString(kLastCheckYMD);
if (base::CompareCaseInsensitiveASCII(today_ymd, last_check_ymd) == 0)
return;

void BraveStatsUpdater::OnServerPingPeriodicTimerFired() {
SendServerPing();
}

Expand All @@ -170,7 +174,7 @@ void BraveStatsUpdater::OnReferralCheckedForPromoCodeFileChanged() {
void BraveStatsUpdater::StartServerPingStartupTimer() {
server_ping_startup_timer_->Start(
FROM_HERE, base::TimeDelta::FromSeconds(kUpdateServerStartupPingDelay),
this, &BraveStatsUpdater::OnServerPingStartupTimerFired);
this, &BraveStatsUpdater::OnServerPingTimerFired);
DCHECK(server_ping_startup_timer_->IsRunning());
}

Expand Down
7 changes: 2 additions & 5 deletions browser/brave_stats_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ class BraveStatsUpdater {
std::unique_ptr<brave::BraveStatsUpdaterParams> stats_updater_params,
scoped_refptr<net::HttpResponseHeaders> headers);

// Invoked when server ping startup timer fires.
void OnServerPingStartupTimerFired();

// Invoked when server ping periodic timer fires.
void OnServerPingPeriodicTimerFired();
// Invoked when server ping timer fires.
void OnServerPingTimerFired();

// Invoked when the specified referral preference changes.
void OnReferralCheckedForPromoCodeFileChanged();
Expand Down
13 changes: 3 additions & 10 deletions browser/brave_stats_updater_params.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "brave/browser/brave_stats_updater_params.h"

#include "base/strings/stringprintf.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "brave/browser/brave_stats_updater_util.h"
#include "brave/common/pref_names.h"
#include "components/prefs/pref_service.h"

Expand Down Expand Up @@ -83,15 +83,8 @@ std::string BraveStatsUpdaterParams::BooleanToString(bool bool_value) const {
return bool_value ? "true" : "false";
}

std::string BraveStatsUpdaterParams::GetDateAsYMD(const base::Time& time) const {
base::Time::Exploded exploded;
time.LocalExplode(&exploded);
return base::StringPrintf("%d-%02d-%02d", exploded.year, exploded.month,
exploded.day_of_month);
}

std::string BraveStatsUpdaterParams::GetCurrentDateAsYMD() const {
return GetDateAsYMD(g_current_time);
return brave::GetDateAsYMD(g_current_time);
}

std::string BraveStatsUpdaterParams::GetLastMondayAsYMD() const {
Expand All @@ -104,7 +97,7 @@ std::string BraveStatsUpdaterParams::GetLastMondayAsYMD() const {
now.ToJsTime() -
((exploded.day_of_week - 1) * base::Time::kMillisecondsPerDay));

return GetDateAsYMD(last_monday);
return brave::GetDateAsYMD(last_monday);
}

int BraveStatsUpdaterParams::GetCurrentMonth() const {
Expand Down
18 changes: 18 additions & 0 deletions browser/brave_stats_updater_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_stats_updater_util.h"

#include "base/strings/stringprintf.h"

namespace brave {

std::string GetDateAsYMD(const base::Time& time) {
base::Time::Exploded exploded;
time.LocalExplode(&exploded);
return base::StringPrintf("%d-%02d-%02d", exploded.year, exploded.month,
exploded.day_of_month);
}

} // namespace brave
18 changes: 18 additions & 0 deletions browser/brave_stats_updater_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_
#define BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_

#include <string>

#include "base/time/time.h"

namespace brave {

std::string GetDateAsYMD(const base::Time& time);

} // namespace brave

#endif // BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_

0 comments on commit 1a412ea

Please sign in to comment.