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.70.x) #3651

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions browser/brave_stats_updater_params.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions browser/brave_stats_updater_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
19 changes: 18 additions & 1 deletion browser/brave_stats_updater_util.cc
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
5 changes: 4 additions & 1 deletion browser/brave_stats_updater_util.h
Original file line number Diff line number Diff line change
@@ -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/. */

Expand All @@ -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_
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brave-core",
"version": "0.70.111",
"version": "0.70.112",
"description": "Brave Core is a set of changes, APIs, and scripts used for customizing Chromium to make Brave.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion patches/chrome-VERSION.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ index 5346a11c9cfb9dcc0dcbe1d9a1e016f51459817d..98da432c0a08c8441dc922c2ed4e9568
-BUILD=3865
-PATCH=90
+BUILD=70
+PATCH=111
+PATCH=112