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.
385 lines
16 KiB
PHP
385 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudServicePeriod;
|
|
use App\Models\CloudTrafficPool;
|
|
use App\Models\CloudTrafficUsageSettlementBucket;
|
|
|
|
/** Canonical accounting-freshness projection shared by user and admin views. */
|
|
final class CloudTrafficPoolFreshness
|
|
{
|
|
public const DELAY_SECONDS = 300;
|
|
public const ATTENTION_SECONDS = 3600;
|
|
|
|
/** @return array<string,int|string|null> */
|
|
public static function snapshot(
|
|
int $userId,
|
|
int $trafficPoolGroupId,
|
|
?int $now = null
|
|
): array {
|
|
$key = $userId . ':' . $trafficPoolGroupId;
|
|
return self::snapshots([[
|
|
'user_id' => $userId,
|
|
'traffic_pool_group_id' => $trafficPoolGroupId,
|
|
]], $now)[$key] ?? self::empty('projection_pending');
|
|
}
|
|
|
|
/** @return array<string,array<string,int|string|null>> */
|
|
public static function snapshots(
|
|
array $identities,
|
|
?int $now = null,
|
|
?array $accountingSnapshots = null
|
|
): array {
|
|
return CloudTrafficConsistentRead::run(static fn (): array =>
|
|
self::readSnapshots($identities, $now ?? time(), $accountingSnapshots));
|
|
}
|
|
|
|
private static function readSnapshots(array $identities, int $now, ?array $accountingSnapshots): array
|
|
{
|
|
$pairs = [];
|
|
foreach ($identities as $identity) {
|
|
$userId = (int) ($identity['user_id'] ?? 0);
|
|
$groupId = (int) ($identity['traffic_pool_group_id'] ?? 0);
|
|
if ($userId > 0 && $groupId > 0) {
|
|
$pairs[$userId . ':' . $groupId] = [
|
|
'user_id' => $userId,
|
|
'traffic_pool_group_id' => $groupId,
|
|
];
|
|
}
|
|
}
|
|
if ($pairs === []) {
|
|
return [];
|
|
}
|
|
$accountingSnapshots ??= CloudTrafficPoolAccounting::snapshots(
|
|
array_values($pairs),
|
|
$now
|
|
);
|
|
$snapshotByKey = [];
|
|
$poolIds = [];
|
|
foreach ($pairs as $key => $pair) {
|
|
$snapshot = $accountingSnapshots[$key] ?? null;
|
|
if (!is_array($snapshot)
|
|
|| ($snapshot['pool_id'] ?? null) === null) {
|
|
continue;
|
|
}
|
|
$snapshotByKey[$key] = $snapshot;
|
|
$poolIds[] = (int) $snapshot['pool_id'];
|
|
}
|
|
|
|
$factsByKey = [];
|
|
if ($snapshotByKey !== []) {
|
|
$facts = CloudServicePeriod::query()
|
|
->leftJoin('cloud_traffic_entitlements as matching_entitlement', static function ($join): void {
|
|
$join->on('matching_entitlement.service_period_id', '=', 'cloud_service_periods.id')
|
|
->on('matching_entitlement.user_id', '=', 'cloud_service_periods.user_id')
|
|
->on('matching_entitlement.instance_id', '=', 'cloud_service_periods.instance_id')
|
|
->on('matching_entitlement.starts_at', '=', 'cloud_service_periods.starts_at')
|
|
->on('matching_entitlement.ends_at', '=', 'cloud_service_periods.ends_at')
|
|
->on('matching_entitlement.granted_bytes', '=', 'cloud_service_periods.traffic_quota_bytes')
|
|
->where('matching_entitlement.quota_mode', '=', 'finite')
|
|
->whereIn('matching_entitlement.state', ['active', 'exhausted']);
|
|
})
|
|
->leftJoin('cloud_instances as freshness_instance',
|
|
'freshness_instance.id', '=',
|
|
'cloud_service_periods.instance_id')
|
|
->leftJoin(
|
|
'cloud_traffic_instance_cursors as freshness_cursor',
|
|
'freshness_cursor.instance_id', '=',
|
|
'cloud_service_periods.instance_id'
|
|
)->leftJoin(
|
|
'cloud_traffic_scan_failures as freshness_period_failure',
|
|
static function ($join): void {
|
|
$join->on(
|
|
'freshness_period_failure.subject_id', '=',
|
|
'cloud_service_periods.id'
|
|
)->where(
|
|
'freshness_period_failure.scan_kind', '=', 'period'
|
|
);
|
|
}
|
|
)->leftJoin(
|
|
'cloud_traffic_scan_failures as freshness_instance_failure',
|
|
static function ($join): void {
|
|
$join->on(
|
|
'freshness_instance_failure.subject_id', '=',
|
|
'cloud_service_periods.instance_id'
|
|
)->where(
|
|
'freshness_instance_failure.scan_kind', '=',
|
|
'instance'
|
|
);
|
|
}
|
|
)->where(function ($query) use ($pairs): void {
|
|
foreach ($pairs as $pair) {
|
|
$query->orWhere(function ($branch) use ($pair): void {
|
|
$branch->where(
|
|
'cloud_service_periods.user_id',
|
|
$pair['user_id']
|
|
)->where(
|
|
'cloud_service_periods.traffic_pool_group_id',
|
|
$pair['traffic_pool_group_id']);
|
|
});
|
|
}
|
|
})->where('cloud_service_periods.traffic_quota_mode', 'finite')
|
|
->whereIn('cloud_service_periods.status',
|
|
['active', 'exhausted'])
|
|
->where('cloud_service_periods.starts_at', '<=', $now)
|
|
->where('cloud_service_periods.ends_at', '>', $now)
|
|
->orderBy('cloud_service_periods.id')->get([
|
|
'cloud_service_periods.id as period_id',
|
|
'matching_entitlement.pool_id as matched_pool_id',
|
|
'cloud_service_periods.instance_id',
|
|
'cloud_service_periods.user_id as period_user_id',
|
|
'cloud_service_periods.traffic_pool_group_id as group_id',
|
|
'freshness_instance.user_id as instance_user_id',
|
|
'freshness_instance.status as instance_status',
|
|
'freshness_instance.lifetime_download_bytes',
|
|
'freshness_instance.lifetime_upload_bytes',
|
|
'freshness_instance.traffic_synced_at',
|
|
'freshness_cursor.instance_id as cursor_instance_id',
|
|
'freshness_cursor.user_id as cursor_user_id',
|
|
'freshness_cursor.pool_id as cursor_pool_id',
|
|
'freshness_cursor.accounted_download_bytes',
|
|
'freshness_cursor.accounted_upload_bytes',
|
|
'freshness_cursor.last_observed_at',
|
|
'freshness_cursor.state as cursor_state',
|
|
'freshness_period_failure.id as period_failure_id',
|
|
'freshness_instance_failure.id as instance_failure_id',
|
|
]);
|
|
foreach ($facts as $fact) {
|
|
$key = (int) $fact->period_user_id . ':'
|
|
. (int) $fact->group_id;
|
|
if (!isset($snapshotByKey[$key])) {
|
|
continue;
|
|
}
|
|
$factsByKey[$key][] = $fact;
|
|
}
|
|
}
|
|
$dueByPool = [];
|
|
if ($poolIds !== []) {
|
|
foreach (CloudTrafficUsageSettlementBucket::whereIn(
|
|
'pool_id', $poolIds
|
|
)->where('state', 'open')->where('seal_at', '<=', $now)
|
|
->selectRaw(
|
|
'pool_id, COUNT(*) AS aggregate, MIN(seal_at) AS oldest'
|
|
)->groupBy('pool_id')->get() as $row) {
|
|
$dueByPool[(int) $row->pool_id] = [
|
|
'count' => (int) $row->aggregate,
|
|
'oldest' => (int) $row->oldest,
|
|
];
|
|
}
|
|
}
|
|
|
|
$result = [];
|
|
foreach ($pairs as $key => $pair) {
|
|
$snapshot = $snapshotByKey[$key] ?? null;
|
|
if ($snapshot === null) {
|
|
$result[$key] = self::empty('projection_pending');
|
|
continue;
|
|
}
|
|
$status = 'current';
|
|
$code = 'current';
|
|
$unsettled = [];
|
|
$stale = [];
|
|
$latestSample = 0;
|
|
$pendingEntitlement = false;
|
|
if ((string) $snapshot['state'] === 'attention') {
|
|
$status = 'attention';
|
|
$code = 'pool_attention';
|
|
}
|
|
foreach ($factsByKey[$key] ?? [] as $fact) {
|
|
$instanceId = (int) $fact->instance_id;
|
|
if ((int) $fact->matched_pool_id !== (int) $snapshot['pool_id']) {
|
|
$pendingEntitlement = true;
|
|
if ($status === 'current') {
|
|
$status = 'delayed';
|
|
$code = 'projection_pending';
|
|
}
|
|
}
|
|
if ($fact->instance_status !== null && CloudTrafficAccountingEvidence::expectsSample($fact->instance_status)
|
|
&& (int) $fact->instance_user_id === $pair['user_id']) {
|
|
$latestSample = max($latestSample, (int) $fact->traffic_synced_at);
|
|
}
|
|
if ($fact->cursor_instance_id === null
|
|
|| (int) $fact->instance_user_id !== $pair['user_id']
|
|
|| (int) $fact->cursor_pool_id
|
|
!== (int) $snapshot['pool_id']
|
|
|| (int) $fact->cursor_user_id !== $pair['user_id']) {
|
|
$unsettled[$instanceId] = true;
|
|
$status = 'attention';
|
|
$code = 'cursor_missing';
|
|
continue;
|
|
}
|
|
$expectsSample = CloudTrafficAccountingEvidence::expectsSample((string) $fact->instance_status);
|
|
$counters = CloudTrafficAccountingEvidence::counters(
|
|
(int) $fact->lifetime_download_bytes, (int) $fact->lifetime_upload_bytes,
|
|
(int) $fact->accounted_download_bytes, (int) $fact->accounted_upload_bytes
|
|
);
|
|
if ($fact->cursor_state !== 'ready' || $counters === 'regression') {
|
|
$unsettled[$instanceId] = true;
|
|
$status = 'attention';
|
|
$code = 'cursor_attention';
|
|
continue;
|
|
}
|
|
if ($counters === 'pending') {
|
|
$unsettled[$instanceId] = true;
|
|
if ($status !== 'attention') {
|
|
$status = 'delayed';
|
|
$code = 'settlement_delayed';
|
|
}
|
|
}
|
|
if ($expectsSample && (int) $fact->traffic_synced_at < $now - self::DELAY_SECONDS) {
|
|
$stale[$instanceId] = true;
|
|
if ($status !== 'attention') {
|
|
$status = 'delayed';
|
|
$code = 'sample_stale';
|
|
}
|
|
}
|
|
if ($fact->period_failure_id !== null
|
|
|| $fact->instance_failure_id !== null) {
|
|
$status = 'attention';
|
|
$code = 'accounting_failure';
|
|
}
|
|
}
|
|
$due = $dueByPool[(int) $snapshot['pool_id']] ?? [
|
|
'count' => 0,
|
|
'oldest' => $now,
|
|
];
|
|
$dueLag = $due['count'] > 0
|
|
? max(0, $now - (int) $due['oldest']) : 0;
|
|
if ($dueLag > self::ATTENTION_SECONDS) {
|
|
$status = 'attention';
|
|
$code = 'settlement_overdue';
|
|
} elseif ($dueLag > self::DELAY_SECONDS
|
|
&& $status === 'current') {
|
|
$status = 'delayed';
|
|
$code = 'settlement_delayed';
|
|
}
|
|
$result[$key] = [
|
|
'status' => $status,
|
|
'last_sample_at' => $latestSample ?: null,
|
|
'projection_pending' => $pendingEntitlement,
|
|
'unsettled_instance_count' => count($unsettled),
|
|
'stale_sample_count' => count($stale),
|
|
'overdue_bucket_count' => (int) $due['count'],
|
|
'safe_code' => $code,
|
|
];
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/** @return array<string,int|string|null> */
|
|
public static function forGroup(
|
|
int $trafficPoolGroupId,
|
|
?int $now = null
|
|
): array {
|
|
return self::forGroups([$trafficPoolGroupId], $now)[
|
|
$trafficPoolGroupId
|
|
] ?? self::empty('no_pools') + ['pool_count' => 0];
|
|
}
|
|
|
|
/** @return array<int,array<string,int|string|null>> */
|
|
public static function forGroups(
|
|
array $trafficPoolGroupIds,
|
|
?int $now = null
|
|
): array {
|
|
return CloudTrafficConsistentRead::run(static fn (): array => self::groupSnapshots($trafficPoolGroupIds, $now ?? time()));
|
|
}
|
|
|
|
private static function groupSnapshots(array $trafficPoolGroupIds, int $now): array
|
|
{
|
|
$groupIds = array_values(array_unique(array_filter(array_map(
|
|
'intval',
|
|
$trafficPoolGroupIds
|
|
), static fn (int $id): bool => $id > 0)));
|
|
if ($groupIds === []) {
|
|
return [];
|
|
}
|
|
$identities = CloudTrafficPool::whereIn(
|
|
'traffic_pool_group_id', $groupIds
|
|
)->orderBy('id')->get(['user_id', 'traffic_pool_group_id'])->map(
|
|
static fn (CloudTrafficPool $pool): array => [
|
|
'user_id' => (int) $pool->user_id,
|
|
'traffic_pool_group_id' =>
|
|
(int) $pool->traffic_pool_group_id,
|
|
]
|
|
)->all();
|
|
$materialized = array_count_values(array_column($identities, 'traffic_pool_group_id'));
|
|
$current = CloudServicePeriod::whereIn('traffic_pool_group_id', $groupIds)
|
|
->where('traffic_quota_mode', 'finite')->whereIn('status', ['active', 'exhausted'])
|
|
->where('starts_at', '<=', $now)->where('ends_at', '>', $now)
|
|
->distinct()->get(['user_id', 'traffic_pool_group_id']);
|
|
foreach ($current as $period) {
|
|
$identities[] = $period->only(['user_id', 'traffic_pool_group_id']);
|
|
}
|
|
$groupByKey = [];
|
|
foreach ($identities as $identity) {
|
|
$groupByKey[(int) $identity['user_id'] . ':'
|
|
. (int) $identity['traffic_pool_group_id']] =
|
|
(int) $identity['traffic_pool_group_id'];
|
|
}
|
|
$snapshots = self::snapshots($identities, $now);
|
|
$byGroup = [];
|
|
foreach ($snapshots as $key => $snapshot) {
|
|
$groupId = (int) ($groupByKey[$key] ?? 0);
|
|
if ($groupId > 0) {
|
|
$byGroup[$groupId][] = $snapshot;
|
|
}
|
|
}
|
|
$result = [];
|
|
foreach ($groupIds as $groupId) {
|
|
$result[$groupId] = self::aggregate(
|
|
$byGroup[$groupId] ?? []
|
|
);
|
|
$result[$groupId]['pool_count'] = $materialized[$groupId] ?? 0;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/** @return array<string,int|string|null> */
|
|
private static function aggregate(array $snapshots): array
|
|
{
|
|
if ($snapshots === []) {
|
|
return self::empty('no_pools') + ['pool_count' => 0];
|
|
}
|
|
$rank = ['current' => 0, 'delayed' => 1, 'attention' => 2];
|
|
$status = 'current';
|
|
$latestSample = 0;
|
|
$unsettled = 0;
|
|
$stale = 0;
|
|
$overdue = 0;
|
|
foreach ($snapshots as $snapshot) {
|
|
if ($rank[$snapshot['status']] > $rank[$status]) {
|
|
$status = $snapshot['status'];
|
|
}
|
|
$latestSample = max($latestSample, (int) $snapshot['last_sample_at']);
|
|
$unsettled += (int) $snapshot['unsettled_instance_count'];
|
|
$stale += (int) $snapshot['stale_sample_count'];
|
|
$overdue += (int) $snapshot['overdue_bucket_count'];
|
|
}
|
|
return [
|
|
'status' => $status,
|
|
'last_sample_at' => $latestSample ?: null,
|
|
'unsettled_instance_count' => $unsettled,
|
|
'stale_sample_count' => $stale,
|
|
'overdue_bucket_count' => $overdue,
|
|
'safe_code' => $status === 'current'
|
|
? 'current' : 'group_' . $status,
|
|
'pool_count' => count($snapshots),
|
|
];
|
|
}
|
|
|
|
/** @return array<string,int|string|null> */
|
|
private static function empty(string $code): array
|
|
{
|
|
return [
|
|
'status' => $code === 'no_pools' ? 'current' : 'delayed',
|
|
'last_sample_at' => null,
|
|
'projection_pending' => $code === 'projection_pending',
|
|
'unsettled_instance_count' => 0,
|
|
'stale_sample_count' => 0,
|
|
'overdue_bucket_count' => 0,
|
|
'safe_code' => $code,
|
|
];
|
|
}
|
|
}
|