Skip to content

Commit

Permalink
[Feature] Database has time zone setting (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen authored Dec 13, 2023
1 parent b71beb1 commit 8fc5e2a
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 21 deletions.
29 changes: 16 additions & 13 deletions app/Filament/Pages/Settings/GeneralPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,21 @@ public function form(Form $form): Form
Forms\Components\TextInput::make('site_name')
->maxLength(50)
->required()
->columnSpan(['md' => 2]),
->columnSpanFull(),
Forms\Components\Toggle::make('public_dashboard_enabled')
->label('Public dashboard'),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),

Forms\Components\Section::make('Time Zone Settings')
->schema([
Forms\Components\Select::make('timezone')
->label('Time zone')
->hint(new HtmlString('&#x1f517;<a href="https://docs.speedtest-tracker.dev/" target="_blank" rel="nofollow">Docs</a>'))
->options(TimeZoneHelper::list())
->searchable()
->required(),
Expand All @@ -61,6 +73,9 @@ public function form(Form $form): Form
->placeholder('M j, Y G:i:s')
->maxLength(25)
->required(),
Forms\Components\Toggle::make('db_has_timezone')
->label('Database has time zone')
->helperText(new HtmlString('Enable if your database <strong>has</strong> a time zone already set.')),
])
->compact()
->columns([
Expand Down Expand Up @@ -113,18 +128,6 @@ public function form(Form $form): Form
'default' => 1,
'md' => 2,
]),

Forms\Components\Section::make('Public Dashboard Settings')
->schema([
Forms\Components\Toggle::make('public_dashboard_enabled')
->label('Enable')
->columnSpan(2),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
])
->columnSpan('full'),
]);
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Exports\ResultsSelectedBulkExport;
use App\Filament\Resources\ResultResource\Pages;
use App\Helpers\TimeZoneHelper;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Carbon\Carbon;
Expand Down Expand Up @@ -142,7 +143,7 @@ public static function table(Table $table): Table
TextColumn::make('created_at')
->label('Created')
->dateTime($settings->time_format ?? 'M j, Y G:i:s')
->timezone($settings->timezone ?? 'UTC')
->timezone(TimeZoneHelper::displayTimeZone($settings))
->sortable(),
])
->filters([
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Widgets/RecentJitterChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Widgets;

use App\Helpers\TimeZoneHelper;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Filament\Widgets\ChartWidget;
Expand Down Expand Up @@ -77,7 +78,7 @@ protected function getData(): array
'tension' => 0.4,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
];
}

Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Widgets/RecentPingChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Widgets;

use App\Helpers\TimeZoneHelper;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Filament\Widgets\ChartWidget;
Expand Down Expand Up @@ -59,7 +60,7 @@ protected function getData(): array
'tension' => 0.4,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
];
}

Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Widgets/RecentSpeedChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Widgets;

use App\Helpers\TimeZoneHelper;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Filament\Widgets\ChartWidget;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected function getData(): array
'tension' => 0.4,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')),
'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')),
];
}

Expand Down
15 changes: 15 additions & 0 deletions app/Helpers/TimeZoneHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

namespace App\Helpers;

use App\Settings\GeneralSettings;
use DateTimeZone;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

class TimeZoneHelper
{
/**
* Returns the display timezone
*/
public static function displayTimeZone(GeneralSettings $settings): string
{
// Don't translate to local time if the database already returns it.
if ($settings->db_has_timezone) {
return 'UTC';
}

return $settings->timezone
?? 'UTC';
}

/**
* Returns a collection of time zones with their offset from UTC.
*/
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ public function __invoke(Request $request)
return view('get-started');
}

/**
* This jank needs to happen because some people like
* to watch the world burn by setting a time zone
* in their database instances.
*/
if ($settings->db_has_timezone) {
date_default_timezone_set($settings->timezone ?? 'UTC');
}

$diff = $latestResult->created_at->diffForHumans();

return view('dashboard', [
'diff' => $diff,
'latestResult' => $latestResult,
]);
}
Expand Down
2 changes: 2 additions & 0 deletions app/Settings/GeneralSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class GeneralSettings extends Settings

public string $timezone;

public bool $db_has_timezone;

public bool $public_dashboard_enabled;

public static function group(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('general.db_has_timezone', false);
}
};
6 changes: 3 additions & 3 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
@livewire(\App\Filament\Widgets\StatsOverviewWidget::class)
</div>

@if ($latestResult)
@isset($latestResult)
<div class="text-sm font-semibold leading-6 text-center col-span-full sm:text-base">
Latest result: {{ $latestResult?->created_at->diffForHumans() }}
Latest result: <time datetime="{{ $latestResult->created_at }}">{{ $diff }}</time>
</div>
@endif
@endisset

<div class="col-span-full">
@livewire(\App\Filament\Widgets\RecentSpeedChartWidget::class)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/debug/timezone.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<ul role="list" class="divide-y divide-gray-200">
<li class="px-4 py-4 sm:px-6">
<p class="text-sm font-medium text-gray-900">Latest result ran at</p>
<p class="text-sm text-gray-500 truncate">{{ $latest->created_at->timezone($settings['timezone'] ?? 'UTC')->format('M. jS, Y h:i:s') }}</p>
<p class="text-sm text-gray-500 truncate">{{ $latest->created_at->timezone($settings['db_has_timezone'] ? null : $settings['timezone'] ?? 'UTC')->format('M. jS, Y h:i:s') }}</p>
</li>

<li class="px-4 py-4 sm:px-6">
Expand Down

0 comments on commit 8fc5e2a

Please sign in to comment.