Skip to content

Commit

Permalink
Format date option added to graph widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
schmeits committed Jun 24, 2024
1 parent 3da314d commit 3d2a676
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions resources/lang/en/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@
'chart_pageviews' => [
'heading' => 'Pageviews (last 7 days)',
'dataset_label' => 'Pageviews',
'date_format' => 'm-d-Y',
],
'chart_sessions' => [
'heading' => 'Sessions (last 7 days)',
'dataset_label' => 'Sessions',
'date_format' => 'm-d-Y',
],
],
];
2 changes: 2 additions & 0 deletions resources/lang/nl/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@
'chart_pageviews' => [
'heading' => 'Pagina views (laatste 7 dagen)',
'dataset_label' => 'Pagina views',
'date_format' => 'd-m-Y',
],
'chart_sessions' => [
'heading' => 'Sessies (laatste 7 dagen)',
'dataset_label' => 'Sessies',
'date_format' => 'd-m-Y',
],
],
];
12 changes: 11 additions & 1 deletion src/FilamentUmami.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Schmeits\FilamentUmami;

use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Exception;
use Illuminate\Support\Facades\Lang;
use Schmeits\FilamentUmami\Concerns\Filter;
Expand Down Expand Up @@ -174,7 +175,16 @@ public function metrics(Filter $filter, UmamiMetricTypes $type): array

public function pageViewsAndSessions(): array
{
return $this->client->getPageViewsAndSessions();
$result = $this->client->getPageViewsAndSessions();

return [
'pageviews' => collect(CarbonPeriod::create(now()->subDays(6), now())->toArray())
->mapWithKeys(fn ($val) => [$val->format('Y-m-d') => 0])
->merge(collect($result['pageviews'])->pluck('y', 'x')->toArray())->toArray(),
'sessions' => collect(CarbonPeriod::create(now()->subDays(6), now())->toArray())
->mapWithKeys(fn ($val) => [$val->format('Y-m-d') => 0])
->merge(collect($result['pageviews'])->pluck('y', 'x')->toArray())->toArray(),
];
}

protected function transformUmamiMetricResult(array $metrics, string $defaultEmptyValue = ''): array
Expand Down
9 changes: 6 additions & 3 deletions src/Widgets/UmamiWidgetGraphPageViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ protected function getData(): array
{
$results = FilamentUmami::pageViewsAndSessions();

$pageviews = collect($results['pageviews'] ?? [])->reverse();
$pageviews = collect($results['pageviews'] ?? []);

return [
'datasets' => [
[
'label' => trans('filament-umami-widgets::translations.widget.chart_pageviews.dataset_label'),
'data' => $pageviews->pluck('y')->toArray(),
'data' => $pageviews->values()->toArray(),
],
],
'labels' => $pageviews->pluck('x')->map(fn ($item) => Carbon::make($item)->format('d-m-Y'))->toArray(),
'labels' => $pageviews
->keys()
->map(fn ($item) => Carbon::make($item)->format(trans('filament-umami-widgets::translations.widget.chart_pageviews.date_format')))
->toArray(),
];
}

Expand Down
9 changes: 6 additions & 3 deletions src/Widgets/UmamiWidgetGraphSessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ protected function getData(): array
{
$results = FilamentUmami::pageViewsAndSessions();

$pageviews = collect($results['sessions'] ?? [])->reverse();
$pageviews = collect($results['sessions'] ?? []);

return [
'datasets' => [
[
'label' => trans('filament-umami-widgets::translations.widget.chart_sessions.dataset_label'),
'data' => $pageviews->pluck('y')->toArray(),
'data' => $pageviews->values()->toArray(),
],
],
'labels' => $pageviews->pluck('x')->map(fn ($item) => Carbon::make($item)->format('d-m-Y'))->toArray(),
'labels' => $pageviews
->keys()
->map(fn ($item) => Carbon::make($item)->format(trans('filament-umami-widgets::translations.widget.chart_sessions.date_format')))
->toArray(),
];
}

Expand Down

0 comments on commit 3d2a676

Please sign in to comment.