You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudStorageArtifact;
|
|
|
|
/**
|
|
* Read-only projection of immutable-object replica coverage.
|
|
*
|
|
* Coverage is derived from verified ready replicas instead of operation rows.
|
|
* This keeps the administrator overview accurate while a copy operation is
|
|
* retrying, reconciling or waiting for a replacement backend.
|
|
*/
|
|
final class CloudStorageProtectionSummaryService
|
|
{
|
|
/** @return array<string,int> */
|
|
public static function summary(?int $now = null): array
|
|
{
|
|
$now ??= time();
|
|
$rows = CloudStorageArtifact::query()
|
|
->from('cloud_storage_artifacts as artifacts')
|
|
->leftJoin('cloud_storage_replicas as replicas', static function ($join): void {
|
|
$join->on('replicas.artifact_id', '=', 'artifacts.id')
|
|
->where('replicas.state', '=', 'ready');
|
|
})
|
|
->whereNotIn('artifacts.state', [
|
|
'retiring', 'delete_attention', 'deleted',
|
|
])
|
|
->groupBy(
|
|
'artifacts.id',
|
|
'artifacts.desired_copies',
|
|
'artifacts.required_copies',
|
|
'artifacts.created_at'
|
|
)
|
|
->get([
|
|
'artifacts.id',
|
|
'artifacts.desired_copies',
|
|
'artifacts.required_copies',
|
|
'artifacts.created_at',
|
|
CloudStorageArtifact::query()->getQuery()->raw(
|
|
'COUNT(replicas.id) AS ready_copies'
|
|
),
|
|
]);
|
|
|
|
$protected = 0;
|
|
$degraded = 0;
|
|
$backlog = 0;
|
|
$oldestCreatedAt = null;
|
|
foreach ($rows as $row) {
|
|
$ready = (int) ($row->ready_copies ?? 0);
|
|
$desired = (int) $row->desired_copies;
|
|
$required = (int) $row->required_copies;
|
|
if ($ready >= $desired) {
|
|
$protected++;
|
|
continue;
|
|
}
|
|
|
|
$backlog++;
|
|
if ($ready >= $required) {
|
|
$degraded++;
|
|
}
|
|
$createdAt = (int) $row->created_at;
|
|
if ($oldestCreatedAt === null || $createdAt < $oldestCreatedAt) {
|
|
$oldestCreatedAt = $createdAt;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'protected_artifact_count' => $protected,
|
|
'degraded_artifact_count' => $degraded,
|
|
'replication_backlog_count' => $backlog,
|
|
'oldest_replication_age_seconds' => $oldestCreatedAt === null
|
|
? 0
|
|
: max(0, $now - $oldestCreatedAt),
|
|
];
|
|
}
|
|
}
|