|
|
|
|
@ -31,7 +31,12 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
?int $now = null,
|
|
|
|
|
?array $accountingSnapshots = null
|
|
|
|
|
): array {
|
|
|
|
|
$now ??= time();
|
|
|
|
|
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);
|
|
|
|
|
@ -65,6 +70,16 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
$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')
|
|
|
|
|
@ -111,6 +126,7 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
->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',
|
|
|
|
|
@ -165,12 +181,25 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
$unsettled = [];
|
|
|
|
|
$stale = [];
|
|
|
|
|
$confirmed = [];
|
|
|
|
|
$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
|
|
|
|
|
@ -240,6 +269,8 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
$result[$key] = [
|
|
|
|
|
'status' => $status,
|
|
|
|
|
'last_confirmed_at' => $lastConfirmed,
|
|
|
|
|
'last_sample_at' => $latestSample ?: null,
|
|
|
|
|
'projection_pending' => $pendingEntitlement,
|
|
|
|
|
'unsettled_instance_count' => count($unsettled),
|
|
|
|
|
'stale_cursor_count' => count($stale),
|
|
|
|
|
'overdue_bucket_count' => (int) $due['count'],
|
|
|
|
|
@ -264,7 +295,11 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
array $trafficPoolGroupIds,
|
|
|
|
|
?int $now = null
|
|
|
|
|
): array {
|
|
|
|
|
$now ??= time();
|
|
|
|
|
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
|
|
|
|
|
@ -281,6 +316,14 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
(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'] . ':'
|
|
|
|
|
@ -300,6 +343,7 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
$result[$groupId] = self::aggregate(
|
|
|
|
|
$byGroup[$groupId] ?? []
|
|
|
|
|
);
|
|
|
|
|
$result[$groupId]['pool_count'] = $materialized[$groupId] ?? 0;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
@ -313,6 +357,7 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
$rank = ['current' => 0, 'delayed' => 1, 'attention' => 2];
|
|
|
|
|
$status = 'current';
|
|
|
|
|
$lastConfirmed = [];
|
|
|
|
|
$latestSample = 0;
|
|
|
|
|
$unsettled = 0;
|
|
|
|
|
$stale = 0;
|
|
|
|
|
$overdue = 0;
|
|
|
|
|
@ -320,6 +365,7 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
if ($rank[$snapshot['status']] > $rank[$status]) {
|
|
|
|
|
$status = $snapshot['status'];
|
|
|
|
|
}
|
|
|
|
|
$latestSample = max($latestSample, (int) $snapshot['last_sample_at']);
|
|
|
|
|
if ($snapshot['last_confirmed_at'] !== null) {
|
|
|
|
|
$lastConfirmed[] = (int) $snapshot['last_confirmed_at'];
|
|
|
|
|
}
|
|
|
|
|
@ -329,6 +375,7 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'status' => $status,
|
|
|
|
|
'last_sample_at' => $latestSample ?: null,
|
|
|
|
|
'last_confirmed_at' => count($lastConfirmed) === count($snapshots)
|
|
|
|
|
? min($lastConfirmed) : null,
|
|
|
|
|
'unsettled_instance_count' => $unsettled,
|
|
|
|
|
@ -346,6 +393,8 @@ final class CloudTrafficPoolFreshness
|
|
|
|
|
return [
|
|
|
|
|
'status' => $code === 'no_pools' ? 'current' : 'delayed',
|
|
|
|
|
'last_confirmed_at' => null,
|
|
|
|
|
'last_sample_at' => null,
|
|
|
|
|
'projection_pending' => $code === 'projection_pending',
|
|
|
|
|
'unsettled_instance_count' => 0,
|
|
|
|
|
'stale_cursor_count' => 0,
|
|
|
|
|
'overdue_bucket_count' => 0,
|
|
|
|
|
|