Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weekly stats ping (uplift to 0.71.x) #3650

Merged
merged 1 commit into from Oct 10, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Merge pull request #3648 from brave/incorrect-weekly-pings

Fix weekly stats pings
  • Loading branch information
emerick committed Oct 9, 2019
commit 942dc343fdb10812335f0fdd30cbcbc301f09fd1
@@ -112,38 +112,7 @@ int BraveStatsUpdaterParams::GetCurrentMonth() const {
}

int BraveStatsUpdaterParams::GetCurrentISOWeekNumber() const {
base::Time now = GetCurrentTimeNow();
base::Time::Exploded now_exploded;
now.LocalExplode(&now_exploded);
now_exploded.hour = 0;
now_exploded.minute = 0;
now_exploded.second = 0;
now_exploded.millisecond = 0;
now_exploded.day_of_month =
now_exploded.day_of_month + 3 - ((now_exploded.day_of_week + 6) % 7);

base::Time now_adjusted;
if (!base::Time::FromLocalExploded(now_exploded, &now_adjusted))
return 0;

base::Time::Exploded jan4_exploded = {0};
jan4_exploded.year = now_exploded.year;
jan4_exploded.month = 1;
jan4_exploded.day_of_week = 0;
jan4_exploded.day_of_month = 4;
jan4_exploded.hour = 0;
jan4_exploded.minute = 0;
jan4_exploded.second = 0;
jan4_exploded.millisecond = 0;

base::Time jan4_time;
if (!base::Time::FromLocalExploded(jan4_exploded, &jan4_time))
return 0;

return 1 + std::round(
((now_adjusted.ToJsTime() - jan4_time.ToJsTime()) / 86400000 -
3 + (jan4_exploded.day_of_week + 6) % 7) /
7);
return GetIsoWeekNumber(GetCurrentTimeNow());
}

base::Time BraveStatsUpdaterParams::GetCurrentTimeNow() const {
@@ -7,6 +7,7 @@

#include "base/time/time.h"
#include "brave/browser/brave_stats_updater_params.h"
#include "brave/browser/brave_stats_updater_util.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
#include "chrome/browser/browser_process.h"
@@ -285,3 +286,32 @@ TEST_F(BraveStatsUpdaterTest, HasCorrectWeekOfInstallation) {
"2019-03-25");
}
}

TEST_F(BraveStatsUpdaterTest, GetIsoWeekNumber) {
base::Time::Exploded exploded;
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
exploded.day_of_week = 1;
exploded.day_of_month = 29;
exploded.month = 7;
exploded.year = 2019;

base::Time time;
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &time));
EXPECT_EQ(brave::GetIsoWeekNumber(time), 31);

exploded.day_of_month = 30;
exploded.month = 9;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &time));
EXPECT_EQ(brave::GetIsoWeekNumber(time), 40);

exploded.day_of_month = 1;
exploded.month = 9;
exploded.day_of_week = 0;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &time));
EXPECT_EQ(brave::GetIsoWeekNumber(time), 35);
}
@@ -1,9 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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 <ctime>

#include "brave/browser/brave_stats_updater_util.h"

#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"

namespace brave {
@@ -15,4 +19,17 @@ std::string GetDateAsYMD(const base::Time& time) {
exploded.day_of_month);
}

int GetIsoWeekNumber(const base::Time& time) {
char buffer[24];
time_t rawtime = time.ToTimeT();
struct tm* timeinfo = std::localtime(&rawtime);
strftime(buffer, 24, "%V", timeinfo);

int week_number = 0;
if (!base::StringToInt(buffer, &week_number))
return 0;

return week_number;
}

} // namespace brave
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/. */

@@ -13,6 +14,8 @@ namespace brave {

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

int GetIsoWeekNumber(const base::Time& time);

} // namespace brave

#endif // BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.