92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Dashboard;
|
|
|
|
use App\Models\BaselineTenantAssignment;
|
|
use App\Models\Finding;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Widgets\Widget;
|
|
|
|
class BaselineCompareNow extends Widget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected string $view = 'filament.widgets.dashboard.baseline-compare-now';
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function getViewData(): array
|
|
{
|
|
$tenant = Filament::getTenant();
|
|
|
|
$empty = [
|
|
'hasAssignment' => false,
|
|
'profileName' => null,
|
|
'findingsCount' => 0,
|
|
'highCount' => 0,
|
|
'mediumCount' => 0,
|
|
'lowCount' => 0,
|
|
'lastComparedAt' => null,
|
|
'landingUrl' => null,
|
|
];
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return $empty;
|
|
}
|
|
|
|
$assignment = BaselineTenantAssignment::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->with('baselineProfile')
|
|
->first();
|
|
|
|
if (! $assignment instanceof BaselineTenantAssignment || $assignment->baselineProfile === null) {
|
|
return $empty;
|
|
}
|
|
|
|
$profile = $assignment->baselineProfile;
|
|
$scopeKey = 'baseline_profile:'.$profile->getKey();
|
|
|
|
$findingsQuery = Finding::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->where('source', 'baseline.compare')
|
|
->where('scope_key', $scopeKey)
|
|
->where('status', Finding::STATUS_NEW);
|
|
|
|
$findingsCount = (int) (clone $findingsQuery)->count();
|
|
$highCount = (int) (clone $findingsQuery)
|
|
->where('severity', Finding::SEVERITY_HIGH)
|
|
->count();
|
|
$mediumCount = (int) (clone $findingsQuery)
|
|
->where('severity', Finding::SEVERITY_MEDIUM)
|
|
->count();
|
|
$lowCount = (int) (clone $findingsQuery)
|
|
->where('severity', Finding::SEVERITY_LOW)
|
|
->count();
|
|
|
|
$latestRun = OperationRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('type', 'baseline_compare')
|
|
->where('context->baseline_profile_id', (string) $profile->getKey())
|
|
->whereNotNull('completed_at')
|
|
->latest('completed_at')
|
|
->first();
|
|
|
|
return [
|
|
'hasAssignment' => true,
|
|
'profileName' => (string) $profile->name,
|
|
'findingsCount' => $findingsCount,
|
|
'highCount' => $highCount,
|
|
'mediumCount' => $mediumCount,
|
|
'lowCount' => $lowCount,
|
|
'lastComparedAt' => $latestRun?->finished_at?->diffForHumans(),
|
|
'landingUrl' => \App\Filament\Pages\BaselineCompareLanding::getUrl(tenant: $tenant),
|
|
];
|
|
}
|
|
}
|