@ -23,25 +23,32 @@ final class CloudAdminOperationsService
// request and no mutation replay can occur here.
CloudWorkerService::expireDueJobs(100);
$filters = self::filters($input);
$query = self::query();
self::applyFilters($query , $filters);
$scope = self::query();
self::applyScopeFilters($scope , $filters);
$lockedSql = self::resourceLockedSql('cloud_jobs');
$summaryRow = (clone $query)->selectRaw(
$attentionSql = self::attentionSql('cloud_jobs');
$summaryRow = (clone $scope)->selectRaw(
"COUNT(*) AS total, "
. "COALESCE(SUM(CASE WHEN cloud_jobs.phase < > 'terminal' THEN 1 ELSE 0 END), 0) AS active, "
. "COALESCE(SUM(CASE WHEN cloud_jobs.phase = 'terminal' AND cloud_jobs.outcome = 'succeeded' THEN 1 ELSE 0 END), 0) AS succeeded, "
. "COALESCE(SUM(CASE WHEN cloud_jobs.phase = 'terminal' AND cloud_jobs.outcome = 'failed' THEN 1 ELSE 0 END), 0) AS failed, "
. 'COALESCE(SUM(CASE WHEN ' . $attentionSql
. ' THEN 1 ELSE 0 END), 0) AS attention, '
. 'COALESCE(SUM(CASE WHEN ' . $lockedSql
. ' THEN 1 ELSE 0 END), 0) AS unresolved_effect'
. ' THEN 1 ELSE 0 END), 0) AS locked '
)->first();
$summary = [];
foreach (['total', 'active', 'succeeded', 'failed',
'unresolved_effect '] as $key) {
foreach (['total', 'active', 'succeeded', 'failed', 'attention',
'locked '] as $key) {
$summary[$key] = max(0, (int) ($summaryRow?->$key ?? 0));
}
$pages = max(1, (int) ceil($summary['total']
$selectedTotal = $summary[$filters['status'] === 'all'
? 'total' : $filters['status']];
$pages = max(1, (int) ceil($selectedTotal
/ $filters['per_page']));
$filters['page'] = min($filters['page'], $pages);
$query = clone $scope;
self::applyStatusFilter($query, $filters['status']);
$records = self::select($query)->orderByDesc('cloud_jobs.id')
->offset(($filters['page'] - 1) * $filters['per_page'])
->limit($filters['per_page'])->get();
@ -59,19 +66,20 @@ final class CloudAdminOperationsService
'region' => (string) $host->region,
];
}
$from = $summary['total'] === 0 ? 0
$from = $selectedTotal === 0 ? 0
: ($filters['page'] - 1) * $filters['per_page'] + 1;
return [
'items' => $items,
'pagination' => [
'page' => $filters['page'],
'per_page' => $filters['per_page'],
'total' => $summary['total'] ,
'total' => $selectedTotal ,
'total_pages' => $pages,
'from' => $from,
'to' => $from === 0 ? 0 : $from + count($items) - 1,
],
'summary' => $summary,
'summary_links' => self::summaryLinks($filters),
'filters' => $filters,
'hosts' => $hosts,
'kinds' => CloudOperationCatalog::kindOptions(),
@ -364,25 +372,22 @@ final class CloudAdminOperationsService
);
}
private static function applyFilters($query, array $filter s): void
private static function applyStatusFilter($query, string $statu s): void
{
if ($filters[' status'] === 'locked') {
if ($status === 'locked') {
$query->whereRaw(self::resourceLockedSql('cloud_jobs'));
} elseif ($filters['status'] === 'attention') {
$query->where(static function ($attention): void {
$attention->where(static function ($failed): void {
$failed->where('cloud_jobs.phase', 'terminal')
->where('cloud_jobs.outcome', 'failed');
})->orWhereRaw(
CloudJobRecoverySql::pendingSql('cloud_jobs')
);
});
} elseif ($filters['status'] === 'active') {
} elseif ($status === 'attention') {
$query->whereRaw(self::attentionSql('cloud_jobs'));
} elseif ($status === 'active') {
$query->where('cloud_jobs.phase', '< >', 'terminal');
} elseif ($filters[' status'] !== 'all') {
} elseif ($status !== 'all') {
$query->where('cloud_jobs.phase', 'terminal')
->where('cloud_jobs.outcome', $filters[' status'] );
->where('cloud_jobs.outcome', $status);
}
}
private static function applyScopeFilters($query, array $filters): void
{
if ($filters['kind'] !== '') {
$query->where('cloud_jobs.kind', $filters['kind']);
}
@ -413,6 +418,41 @@ final class CloudAdminOperationsService
}
}
private static function attentionSql(string $table): string
{
if (preg_match('/^[a-z_][a-z0-9_]*$/D', $table) !== 1) {
throw new \InvalidArgumentException('Invalid task table alias');
}
return '((' . $table . ".phase = 'terminal' AND "
. $table . ".outcome = 'failed') OR "
. CloudJobRecoverySql::pendingSql($table) . ')';
}
/** @return array< string , string > */
private static function summaryLinks(array $filters): array
{
$base = [];
foreach (['q', 'kind', 'host_id'] as $key) {
if ($filters[$key] !== null & & $filters[$key] !== '') {
$base[$key] = (string) $filters[$key];
}
}
if ($filters['per_page'] !== 20) {
$base['per_page'] = (string) $filters['per_page'];
}
$links = [];
foreach (['all', 'active', 'attention', 'failed', 'locked'] as $status) {
$query = $base;
if ($status !== 'all') {
$query = ['status' => $status] + $query;
}
$encoded = http_build_query($query, '', '& ', PHP_QUERY_RFC3986);
$links[$status] = '/admin/operations'
. ($encoded === '' ? '' : '?' . $encoded);
}
return $links;
}
private static function row($job, bool $canManage = false): array
{
$completed = (string) $job->phase === 'terminal';