TenantAtlas/app/Filament/Widgets/Operations/OperationsKpiHeader.php

129 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Widgets\Operations;
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\OpsUx\ActiveRuns;
use Carbon\CarbonInterval;
use Filament\Facades\Filament;
use Filament\Widgets\Widget;
use Illuminate\Support\Collection;
class OperationsKpiHeader extends Widget
{
protected static bool $isLazy = false;
protected string $view = 'filament.widgets.operations.operations-kpi-header';
protected int|string|array $columnSpan = 'full';
/**
* @return array<string, mixed>
*/
protected function getViewData(): array
{
$tenant = Filament::getTenant();
if (! $tenant instanceof Tenant) {
return [
'pollingInterval' => null,
'totalRuns30Days' => 0,
'activeRuns' => 0,
'failedOrPartial7Days' => 0,
'avgDuration7Days' => '—',
];
}
$tenantId = (int) $tenant->getKey();
$totalRuns30Days = (int) OperationRun::query()
->where('tenant_id', $tenantId)
->where('created_at', '>=', now()->subDays(30))
->count();
$activeRuns = (int) OperationRun::query()
->where('tenant_id', $tenantId)
->whereIn('status', [
OperationRunStatus::Queued->value,
OperationRunStatus::Running->value,
])
->count();
$failedOrPartial7Days = (int) OperationRun::query()
->where('tenant_id', $tenantId)
->where('status', OperationRunStatus::Completed->value)
->whereIn('outcome', [
OperationRunOutcome::Failed->value,
OperationRunOutcome::PartiallySucceeded->value,
])
->where('completed_at', '>=', now()->subDays(7))
->count();
/** @var Collection<int, OperationRun> $recentCompletedRuns */
$recentCompletedRuns = OperationRun::query()
->where('tenant_id', $tenantId)
->where('status', OperationRunStatus::Completed->value)
->whereNotNull('started_at')
->whereNotNull('completed_at')
->where('completed_at', '>=', now()->subDays(7))
->latest('id')
->limit(200)
->get(['started_at', 'completed_at']);
$durations = $recentCompletedRuns
->map(function (OperationRun $run): ?int {
if (! $run->started_at || ! $run->completed_at) {
return null;
}
$seconds = $run->completed_at->diffInSeconds($run->started_at);
if (is_int($seconds)) {
return $seconds;
}
return (int) round((float) $seconds);
})
->filter(fn (?int $seconds): bool => is_int($seconds) && $seconds > 0)
->values();
$avgDuration7Days = '—';
if ($durations->isNotEmpty()) {
$avgDurationSeconds = (int) round($durations->avg() ?? 0);
$avgDuration7Days = self::formatDurationSeconds($avgDurationSeconds);
}
return [
'pollingInterval' => ActiveRuns::existForTenant($tenant) ? '10s' : null,
'totalRuns30Days' => $totalRuns30Days,
'activeRuns' => $activeRuns,
'failedOrPartial7Days' => $failedOrPartial7Days,
'avgDuration7Days' => $avgDuration7Days,
];
}
private static function formatDurationSeconds(int $seconds): string
{
if ($seconds <= 0) {
return '—';
}
if ($seconds < 60) {
return $seconds.'s';
}
$interval = CarbonInterval::seconds($seconds)->cascade();
if ($seconds < 3600) {
return sprintf('%dm %ds', $interval->minutes, $interval->seconds);
}
return sprintf('%dh %dm', $interval->hours, $interval->minutes);
}
}