From 88228df6f272d95bf6605ba3b9ac3bd599b8a26a Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 19 Dec 2017 08:57:55 +1030 Subject: [PATCH] Clean up some variable/method names --- .../statistics/js/admin/dist/extension.js | 6 ++-- .../admin/src/components/StatisticsWidget.js | 6 ++-- .../src/Listener/AddStatisticsData.php | 28 ++++++++----------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/extensions/statistics/js/admin/dist/extension.js b/extensions/statistics/js/admin/dist/extension.js index 32a6db4aa..8f6369f8d 100644 --- a/extensions/statistics/js/admin/dist/extension.js +++ b/extensions/statistics/js/admin/dist/extension.js @@ -41,9 +41,9 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon // reset to the first hour of the day, and then convert back into UTC time. // We'll be working with seconds rather than milliseconds throughout too. var today = new Date(); - today.setTime(today.getTime() + app.data.statistics.utcOffset * 1000); + today.setTime(today.getTime() + app.data.statistics.timezoneOffset * 1000); today.setUTCHours(0, 0, 0, 0); - today.setTime(today.getTime() - app.data.statistics.utcOffset * 1000); + today.setTime(today.getTime() - app.data.statistics.timezoneOffset * 1000); today = today / 1000; this.entities = ['users', 'discussions', 'posts']; @@ -143,7 +143,7 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon return; } - var offset = app.data.statistics.utcOffset; + var offset = app.data.statistics.timezoneOffset; var period = this.periods[this.selectedPeriod]; var periodLength = period.end - period.start; var labels = []; diff --git a/extensions/statistics/js/admin/src/components/StatisticsWidget.js b/extensions/statistics/js/admin/src/components/StatisticsWidget.js index 1c05424a0..63be5598a 100644 --- a/extensions/statistics/js/admin/src/components/StatisticsWidget.js +++ b/extensions/statistics/js/admin/src/components/StatisticsWidget.js @@ -24,9 +24,9 @@ export default class StatisticsWidget extends DashboardWidget { // reset to the first hour of the day, and then convert back into UTC time. // We'll be working with seconds rather than milliseconds throughout too. let today = new Date(); - today.setTime(today.getTime() + app.data.statistics.utcOffset * 1000); + today.setTime(today.getTime() + app.data.statistics.timezoneOffset * 1000); today.setUTCHours(0, 0, 0, 0); - today.setTime(today.getTime() - app.data.statistics.utcOffset * 1000); + today.setTime(today.getTime() - app.data.statistics.timezoneOffset * 1000); today = today / 1000; this.entities = ['users', 'discussions', 'posts']; @@ -99,7 +99,7 @@ export default class StatisticsWidget extends DashboardWidget { return; } - const offset = app.data.statistics.utcOffset; + const offset = app.data.statistics.timezoneOffset; const period = this.periods[this.selectedPeriod]; const periodLength = period.end - period.start; const labels = []; diff --git a/extensions/statistics/src/Listener/AddStatisticsData.php b/extensions/statistics/src/Listener/AddStatisticsData.php index 36253d62e..793f0e7a8 100644 --- a/extensions/statistics/src/Listener/AddStatisticsData.php +++ b/extensions/statistics/src/Listener/AddStatisticsData.php @@ -84,8 +84,8 @@ class AddStatisticsData $results = $query ->selectRaw( 'DATE_FORMAT( - @date := DATE_ADD('.$column.', INTERVAL ? SECOND), -- correct for timezone - IF(@date > ?, \'%Y-%m-%dT%H:00:00\', \'%Y-%m-%dT00:00:00\') -- if within the last 48 hours, group by hour + @date := DATE_ADD('.$column.', INTERVAL ? SECOND), -- convert to user timezone + IF(@date > ?, \'%Y-%m-%d %H:00:00\', \'%Y-%m-%d\') -- if within the last 48 hours, group by hour ) as time_group', [$offset, new DateTime('-48 hours')] ) @@ -94,13 +94,14 @@ class AddStatisticsData ->groupBy('time_group') ->lists('count', 'time_group'); - // Now that we have the aggregated statistics, convert each point in - // time into a UNIX timestamp . - $displayTimezone = $this->getDisplayTimezone(); + // Now that we have the aggregated statistics, convert each time group + // into a UNIX timestamp. + $userTimezone = $this->getUserTimezone(); + $timed = []; - $results->each(function ($count, $time) use (&$timed, $displayTimezone) { - $time = new DateTime($time, $displayTimezone); + $results->each(function ($count, $time) use (&$timed, $userTimezone) { + $time = new DateTime($time, $userTimezone); $timed[$time->getTimestamp()] = $count; }); @@ -109,19 +110,14 @@ class AddStatisticsData private function getTimezoneOffset() { + $now = new DateTime; + $dataTimezone = new DateTimeZone(date_default_timezone_get()); - return $this->getDisplayTimezone()->getOffset(new DateTime('now', $dataTimezone)); + return $this->getUserTimezone()->getOffset($now) - $dataTimezone->getOffset($now); } - private function getUTCOffset() - { - $utcTimezone = new DateTimeZone('UTC'); - - return $this->getDisplayTimezone()->getOffset(new DateTime('now', $utcTimezone)); - } - - private function getDisplayTimezone() + private function getUserTimezone() { return new DateTimeZone($this->settings->get('flarum-statistics.timezone', date_default_timezone_get())); }