Skip to content

Commit

Permalink
fix(admin): catch some potential exceptions during initialisation (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenrikur authored Mar 2, 2024
1 parent b15b32d commit 9f76b41
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 80 deletions.
18 changes: 11 additions & 7 deletions app/Filament/Widgets/ApplicationStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class ApplicationStats extends BaseWidget
{
protected function getStats(): array
{
$data = Cache::remember('dd-admin-application-stats', 60, fn() => Application::query()->toBase()->select(DB::raw('type, COUNT(*) as count'))->whereNull('canceled_at')->whereNull('waiting_at')->groupBy('type')->get());
return [
Stat::make('Total Applications (active)', fn() => $data->sum('count')),
Stat::make('Total Dealers (active)', fn() => $data->firstWhere('type', 'dealer')?->count ?? 0),
Stat::make('Total Shares (active)', fn() => $data->firstWhere('type', 'share')?->count ?? 0),
Stat::make('Total Assistants (active)', fn() => $data->firstWhere('type', 'assistant')?->count ?? 0),
];
try {
$data = Cache::remember('dd-admin-application-stats', 60, fn() => Application::query()->toBase()->select(DB::raw('type, COUNT(*) as count'))->whereNull('canceled_at')->whereNull('waiting_at')->groupBy('type')->get());
return [
Stat::make('Total Applications (active)', fn() => $data->sum('count')),
Stat::make('Total Dealers (active)', fn() => $data->firstWhere('type', 'dealer')?->count ?? 0),
Stat::make('Total Shares (active)', fn() => $data->firstWhere('type', 'share')?->count ?? 0),
Stat::make('Total Assistants (active)', fn() => $data->firstWhere('type', 'assistant')?->count ?? 0),
];
} catch (\Throwable $th) {
return [];
}
}
}
58 changes: 31 additions & 27 deletions app/Filament/Widgets/ApplicationStatusChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,40 @@ protected function getType(): string

protected function getData(): array
{
$status = Cache::remember('dd-admin-application-status', 60, fn () => [
ApplicationStatus::Canceled->displayName() => ApplicationStatus::Canceled->orWhere(Application::query())->count(),
ApplicationStatus::Open->displayName() => ApplicationStatus::Open->orWhere(Application::query())->count(),
ApplicationStatus::Waiting->displayName() => ApplicationStatus::Waiting->orWhere(Application::query())->count(),
ApplicationStatus::TableAssigned->displayName() => ApplicationStatus::TableAssigned->orWhere(Application::query())->count(),
ApplicationStatus::TableOffered->displayName() => ApplicationStatus::TableOffered->orWhere(Application::query())->count(),
ApplicationStatus::TableAccepted->displayName() => ApplicationStatus::TableAccepted->orWhere(Application::query())->count(),
ApplicationStatus::CheckedIn->displayName() => ApplicationStatus::CheckedIn->orWhere(Application::query())->count(),
ApplicationStatus::CheckedOut->displayName() => ApplicationStatus::CheckedOut->orWhere(Application::query())->count(),
]);
try {
$status = Cache::remember('dd-admin-application-status', 60, fn () => [
ApplicationStatus::Canceled->displayName() => ApplicationStatus::Canceled->orWhere(Application::query())->count(),
ApplicationStatus::Open->displayName() => ApplicationStatus::Open->orWhere(Application::query())->count(),
ApplicationStatus::Waiting->displayName() => ApplicationStatus::Waiting->orWhere(Application::query())->count(),
ApplicationStatus::TableAssigned->displayName() => ApplicationStatus::TableAssigned->orWhere(Application::query())->count(),
ApplicationStatus::TableOffered->displayName() => ApplicationStatus::TableOffered->orWhere(Application::query())->count(),
ApplicationStatus::TableAccepted->displayName() => ApplicationStatus::TableAccepted->orWhere(Application::query())->count(),
ApplicationStatus::CheckedIn->displayName() => ApplicationStatus::CheckedIn->orWhere(Application::query())->count(),
ApplicationStatus::CheckedOut->displayName() => ApplicationStatus::CheckedOut->orWhere(Application::query())->count(),
]);

return [
'datasets' => [
[
'label' => 'Total Applications',
'data' => array_values($status),
'backgroundColor' => [
'rgb(250, 80, 80)',
'rgb(0, 200, 255)',
'rgb(255, 200, 80)',
'rgb(250, 100, 200)',
'rgb(150, 100, 255)',
'rgb(100, 255, 100)',
'rgb(0, 180, 0)',
'rgb(120, 120, 120)',
return [
'datasets' => [
[
'label' => 'Total Applications',
'data' => array_values($status),
'backgroundColor' => [
'rgb(250, 80, 80)',
'rgb(0, 200, 255)',
'rgb(255, 200, 80)',
'rgb(250, 100, 200)',
'rgb(150, 100, 255)',
'rgb(100, 255, 100)',
'rgb(0, 180, 0)',
'rgb(120, 120, 120)',
],
],
],
],
'labels' => array_keys($status),
];
'labels' => array_keys($status),
];
} catch (\Throwable $th) {
return [];
}
}

protected function getOptions(): array
Expand Down
16 changes: 10 additions & 6 deletions app/Filament/Widgets/ApplicationTablesAssignedChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class ApplicationTablesAssignedChart extends AbstractApplicationTablesChart

protected function retrieveData(): array
{
return Cache::remember('dd-admin-application-tables-assigned', 60, function(): array {
$tableTypeCount = Application::query()->toBase()->join('table_types', 'table_type_assigned', '=', 'table_types.id')->select(DB::raw('table_types.id as id, COUNT(*) as count'))->where('type', '=', 'dealer')->whereNull('canceled_at')->whereNull('waiting_at')->groupBy('table_types.id')->get()->pluck('count', 'id');
return TableType::all()->mapWithKeys(function(TableType $tableType) use ($tableTypeCount) {
return [ $tableType->name => $tableTypeCount[$tableType->id] ?? 0];
})->toArray();
});
try {
return Cache::remember('dd-admin-application-tables-assigned', 60, function(): array {
$tableTypeCount = Application::query()->toBase()->join('table_types', 'table_type_assigned', '=', 'table_types.id')->select(DB::raw('table_types.id as id, COUNT(*) as count'))->where('type', '=', 'dealer')->whereNull('canceled_at')->whereNull('waiting_at')->groupBy('table_types.id')->get()->pluck('count', 'id');
return TableType::all()->mapWithKeys(function(TableType $tableType) use ($tableTypeCount) {
return [ $tableType->name => $tableTypeCount[$tableType->id] ?? 0];
})->toArray();
});
} catch (\Throwable $th) {
return [];
}
}
}
16 changes: 10 additions & 6 deletions app/Filament/Widgets/ApplicationTablesRequestedChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class ApplicationTablesRequestedChart extends AbstractApplicationTablesChart

protected function retrieveData(): array
{
return Cache::remember('dd-admin-application-tables-requested', 60, function(): array {
$tableTypeCount = Application::query()->toBase()->join('table_types', 'table_type_requested', '=', 'table_types.id')->select(DB::raw('table_types.id as id, COUNT(*) as count'))->where('type', '=', 'dealer')->whereNull('canceled_at')->whereNull('waiting_at')->groupBy('table_types.id')->get()->pluck('count', 'id');
return TableType::all()->mapWithKeys(function(TableType $tableType) use ($tableTypeCount) {
return [ $tableType->name => $tableTypeCount[$tableType->id] ?? 0];
})->toArray();
});
try {
return Cache::remember('dd-admin-application-tables-requested', 60, function(): array {
$tableTypeCount = Application::query()->toBase()->join('table_types', 'table_type_requested', '=', 'table_types.id')->select(DB::raw('table_types.id as id, COUNT(*) as count'))->where('type', '=', 'dealer')->whereNull('canceled_at')->whereNull('waiting_at')->groupBy('table_types.id')->get()->pluck('count', 'id');
return TableType::all()->mapWithKeys(function(TableType $tableType) use ($tableTypeCount) {
return [ $tableType->name => $tableTypeCount[$tableType->id] ?? 0];
})->toArray();
});
} catch (\Throwable $th) {
return [];
}
}
}
70 changes: 37 additions & 33 deletions app/Filament/Widgets/RegistrationStatusChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,45 @@ protected function getType(): string

protected function getData(): array
{
$data = Cache::remember('dd-admin-application-totals', 10 * 60, function (): array {
$registrations = RegSysClientController::getAllRegs('id');
return Application::with('user')->whereNull('canceled_at')->whereNull('waiting_at')->get()
->map(fn (Application $application): string => $registrations[$application->user?->reg_id]['status'] ?? 'unknown')
->reduce(function (array $statusCount, string $status) {
$statusCount[$status] += 1;
return $statusCount;
}, [
'new' => 0,
'approved' => 0,
'partially paid' => 0,
'paid' => 0,
'checked in' => 0,
'unknown' => 0,
'cancelled' => 0,
]);
});
return [
'datasets' => [
[
'label' => 'Total Applications',
'data' => array_values($data),
'backgroundColor' => [
'rgb(255, 200, 80)',
'rgb(0, 200, 255)',
'rgb(250, 100, 200)',
'rgb(100, 255, 100)',
'rgb(0, 180, 0)',
'rgb(120, 120, 120)',
'rgb(250, 80, 80)',
try {
$data = Cache::remember('dd-admin-application-totals', 10 * 60, function (): array {
$registrations = RegSysClientController::getAllRegs('id');
return Application::with('user')->whereNull('canceled_at')->whereNull('waiting_at')->get()
->map(fn (Application $application): string => $registrations[$application->user?->reg_id]['status'] ?? 'unknown')
->reduce(function (array $statusCount, string $status) {
$statusCount[$status] += 1;
return $statusCount;
}, [
'new' => 0,
'approved' => 0,
'partially paid' => 0,
'paid' => 0,
'checked in' => 0,
'unknown' => 0,
'cancelled' => 0,
]);
});
return [
'datasets' => [
[
'label' => 'Total Applications',
'data' => array_values($data),
'backgroundColor' => [
'rgb(255, 200, 80)',
'rgb(0, 200, 255)',
'rgb(250, 100, 200)',
'rgb(100, 255, 100)',
'rgb(0, 180, 0)',
'rgb(120, 120, 120)',
'rgb(250, 80, 80)',
],
],
],
],
'labels' => array_keys($data),
];
'labels' => array_keys($data),
];
} catch (\Throwable $th) {
return [];
}
}

protected function getOptions(): array
Expand Down
5 changes: 4 additions & 1 deletion resources/views/filament/widgets/dashboard-info.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@php
$user = filament()->auth()->user();
$user = null;
try {
$user = filament()->auth()->user();
} catch (\Throwable $th) {}
@endphp

<x-filament-widgets::widget class="fi-account-widget">
Expand Down

0 comments on commit 9f76b41

Please sign in to comment.