-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build initial dashboard page with ViewStats widget
- Loading branch information
1 parent
52136bd
commit 21aeac6
Showing
8 changed files
with
318 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
enum PresentationFilter: string | ||
{ | ||
use Traits\EnumToArray; | ||
|
||
case INSTRUCTIONS = 'instructions'; | ||
case ADHOC = 'adhoc'; | ||
|
||
/** | ||
* Returns the user-friendly label | ||
*/ | ||
public function label(): string | ||
{ | ||
return match ($this) { | ||
self::INSTRUCTIONS => 'Instructions', | ||
self::ADHOC => 'Adhoc', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Enums\Traits; | ||
|
||
// Inspired From: https://stackoverflow.com/a/71680007/3482221 | ||
|
||
trait EnumToArray | ||
{ | ||
/** | ||
* The array of keys for the enum. | ||
* | ||
* @return mixed[] - These will be a string array. But laravel's toArray | ||
* doesn't specify that, so we have to use mixed. | ||
*/ | ||
public static function names(): array | ||
{ | ||
return collect(self::cases()) | ||
->pluck('name') | ||
->toArray(); | ||
} | ||
|
||
/** | ||
* The array of values for the enum. | ||
* | ||
* @return mixed[] | ||
*/ | ||
public static function values(): array | ||
{ | ||
return collect(self::cases()) | ||
->pluck('value') | ||
->toArray(); | ||
} | ||
|
||
/** | ||
* The array of values for the enum. | ||
* | ||
* @return array<mixed, string> | ||
*/ | ||
public static function array(): array | ||
{ | ||
return collect(self::cases()) | ||
->reduce(function ($carry, $row) { | ||
$carry[$row->value] = $row->label(); | ||
|
||
return $carry; | ||
}, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\Filament\Pages; | ||
|
||
use App\Enums\PresentationFilter; | ||
use App\Models\Presentation; | ||
use Filament\Forms\Components\DatePicker; | ||
use Filament\Forms\Components\Section; | ||
use Filament\Forms\Components\Select; | ||
use Filament\Forms\Form; | ||
use Filament\Forms\Get; | ||
use Filament\Pages\Dashboard as BaseDashboard; | ||
use Filament\Pages\Dashboard\Concerns\HasFiltersForm; | ||
|
||
class Dashboard extends BaseDashboard | ||
{ | ||
use HasFiltersForm; | ||
|
||
public function getColumns(): int|string|array | ||
{ | ||
return 2; | ||
} | ||
|
||
public function filtersForm(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
Section::make() | ||
->schema([ | ||
Select::make('presentation_id') | ||
->label('Presentation') | ||
->searchable() | ||
->options( | ||
auth()->user()->isAdministrator() | ||
? PresentationFilter::array() | ||
: [] | ||
)->getSearchResultsUsing(function (string $search) { | ||
return Presentation::forUser() | ||
->where('title', 'ilike', "%{$search}%") | ||
->limit(20) | ||
->pluck('title', 'id') | ||
->toArray(); | ||
})->getOptionLabelUsing(function ($value): ?string { | ||
return Presentation::forUser() | ||
->find(intval($value))?->title; | ||
}), | ||
DatePicker::make('start_date') | ||
->label('Start Date') | ||
->native(false) | ||
->maxDate(fn (Get $get): ?string => $get('end_date') ?? now()->subDay()) | ||
->hintIcon('heroicon-o-information-circle', tooltip: 'Only affects "Date Range" stats') | ||
->default(now()->subDays(8)), | ||
DatePicker::make('end_date') | ||
->label('End Date') | ||
->native(false) | ||
->minDate(fn (Get $get): ?string => $get('start_date')) | ||
->default(now()->subDay()) | ||
->hintIcon('heroicon-o-information-circle', tooltip: 'Only affects "Date Range" stats') | ||
->maxDate(now()->subDay()), | ||
]) | ||
->columns(3), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Filament\Widgets; | ||
|
||
use App\Models\AggregateView; | ||
use App\Models\DailyView; | ||
use Filament\Widgets\Concerns\InteractsWithPageFilters; | ||
use Filament\Widgets\StatsOverviewWidget as BaseWidget; | ||
use Filament\Widgets\StatsOverviewWidget\Stat; | ||
|
||
class ViewStats extends BaseWidget | ||
{ | ||
use InteractsWithPageFilters; | ||
|
||
protected static ?string $pollingInterval = null; | ||
|
||
protected function getStats(): array | ||
{ | ||
return [ | ||
$this->aggregateViews(withinRange: true), | ||
$this->dailyViews(), | ||
$this->aggregateViews(), | ||
]; | ||
} | ||
|
||
private function dailyViews(): Stat | ||
{ | ||
$views = DailyView::getForStat( | ||
presentationId: $this->filters['presentation_id'] | ||
); | ||
|
||
$totalviews = $views->count(); | ||
$uniqueviews = $views->unique(function (DailyView $item) { | ||
return $item->presentation_id.$item->session_id; | ||
})->count(); | ||
|
||
$percentUniqueviews = $totalviews == 0 | ||
? 0 | ||
: round(($uniqueviews / $totalviews) * 100); | ||
|
||
return Stat::make('Total Views Today', $totalviews) | ||
->description($percentUniqueviews."% ($uniqueviews) Unique Views"); | ||
} | ||
|
||
private function aggregateViews(bool $withinRange = false): Stat | ||
{ | ||
$views = AggregateView::getForStat( | ||
presentationId: $this->filters['presentation_id'], | ||
startDate: $withinRange ? $this->filters['start_date'] : null, | ||
endDate: $withinRange ? $this->filters['end_date'] : null, | ||
); | ||
|
||
$totalviews = $views->sum('total_count'); | ||
$uniqueviews = $views->sum('unique_count'); | ||
|
||
$percentUniqueviews = $totalviews == 0 | ||
? 0 | ||
: round(($uniqueviews / $totalviews) * 100); | ||
|
||
$heading = $withinRange ? 'Total Views in Date Range' : 'Total Lifetime Views'; | ||
|
||
return Stat::make($heading, $totalviews) | ||
->description($percentUniqueviews."% ($uniqueviews) Unique Views"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters