1
0
mirror of https://github.com/flarum/core.git synced 2025-08-22 16:14:00 +02:00

Add "today" period with hourly breakdown, and fix timezone issues

This fix ensures that before aggregating daily/hourly statistics, dates
are converted into the local timezone ("flarum-statistics.timezone" in
the settings table).
This commit is contained in:
Toby Zerner
2017-12-11 19:01:38 +10:30
parent 8860def12e
commit 6857d32d22
3 changed files with 106 additions and 41 deletions

View File

@@ -12,20 +12,41 @@
namespace Flarum\Statistics\Listener;
use DateTime;
use DateTimeZone;
use Flarum\Core\Discussion;
use Flarum\Core\Post;
use Flarum\Core\User;
use Flarum\Event\ConfigureWebApp;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Events\Dispatcher;
class AddStatisticsData
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureWebApp::class, [$this, 'addStatisticsData']);
}
/**
* @param ConfigureWebApp $event
*/
public function addStatisticsData(ConfigureWebApp $event)
{
$event->view->setVariable('statistics', $this->getStatistics());
@@ -49,11 +70,33 @@ class AddStatisticsData
private function getDailyCounts(Builder $query, $column)
{
// Calculate the offset between the server timezone (which is used for
// dates stored in the database) and the user's timezone (set via the
// settings table). We will use this to adjust dates before aggregating
// daily/hourly statistics.
$offset = $this->getTimezoneOffset();
return $query
->selectRaw('DATE('.$column.') as date')
->selectRaw(
'UNIX_TIMESTAMP(
DATE_FORMAT(
@date := DATE_ADD('.$column.', INTERVAL ? SECOND), -- correct for timezone
IF(@date > ?, \'%Y-%m-%d %H:00:00\', \'%Y-%m-%d\') -- if within the last 48 hours, group by hour
)
) as period',
[$offset, new DateTime('-48 hours')]
)
->selectRaw('COUNT(id) as count')
->where($column, '>', new DateTime('-24 months'))
->groupBy('date')
->lists('count', 'date');
->groupBy('period')
->lists('count', 'period');
}
private function getTimezoneOffset()
{
$dataTimezone = new DateTimeZone(date_default_timezone_get());
$displayTimezone = new DateTimeZone($this->settings->get('flarum-statistics.timezone', date_default_timezone_get()));
return $displayTimezone->getOffset(new DateTime('now', $dataTimezone));
}
}