whereIn('state', ['held', 'uncertain', 'committed']) ->get(['state', 'instance_id', 'cpu_cores', 'memory_mib', 'disk_gib']); $coveredInstances = []; foreach ($reservationRows as $row) { $state = (string) $row->state; $bucket = $state === 'committed' ? 'allocated' : 'reserved'; $usage['instances'][$bucket]++; $usage['cpu'][$bucket] += max(0, (int) $row->cpu_cores); $usage['memory'][$bucket] += max(0, (int) $row->memory_mib); $usage['disk'][$bucket] += max(0, (int) $row->disk_gib); if ($row->instance_id !== null) { $coveredInstances[(int) $row->instance_id] = true; } } $instances = CloudInstance::where('host_id', $hostId) ->where('status', '<>', 'deleted') ->get(['id', 'spec_json']); foreach ($instances as $instance) { if (isset($coveredInstances[(int) $instance->id])) { continue; } $spec = self::spec((string) $instance->spec_json); if ($spec === null) { $usage['unknown']++; continue; } $usage['instances']['allocated']++; $usage['cpu']['allocated'] += $spec['cpu']; $usage['memory']['allocated'] += $spec['memory']; $usage['disk']['allocated'] += $spec['disk']; } $usage['cpu_sum'] = $usage['cpu']['allocated'] + $usage['cpu']['reserved']; $usage['memory_sum'] = $usage['memory']['allocated'] + $usage['memory']['reserved']; $usage['disk_sum'] = $usage['disk']['allocated'] + $usage['disk']['reserved']; $usage['instance_count'] = $usage['instances']['allocated'] + $usage['instances']['reserved']; return $usage; } public static function forHosts(array $hostIds): array { $hostIds = array_values(array_unique(array_filter( array_map('intval', $hostIds), static fn (int $id): bool => $id > 0 ))); $result = []; foreach ($hostIds as $hostId) { $result[$hostId] = self::forHost($hostId); } return $result; } public static function emptyUsage(): array { return [ 'cpu' => ['allocated' => 0, 'reserved' => 0], 'memory' => ['allocated' => 0, 'reserved' => 0], 'disk' => ['allocated' => 0, 'reserved' => 0], 'instances' => ['allocated' => 0, 'reserved' => 0], 'unknown' => 0, ]; } private static function spec(string $encoded): ?array { try { $value = json_decode($encoded, true, 32, JSON_THROW_ON_ERROR); } catch (\Throwable $error) { return null; } if (!is_array($value) || array_is_list($value)) { return null; } $cpu = self::positiveNumber($value['vcpu'] ?? $value['cpu_cores'] ?? null); $memory = self::positiveNumber($value['memory_mib'] ?? $value['ram_mib'] ?? null); $disk = self::positiveNumber($value['disk_gib'] ?? null); if ($disk === null && is_array($value['disks'] ?? null)) { $sum = 0.0; foreach ($value['disks'] as $row) { if (!is_array($row)) { return null; } $capacity = self::positiveNumber($row['capacity_gib'] ?? $row['size_gib'] ?? null); if ($capacity === null) { return null; } $sum += $capacity; } $disk = $sum > 0 ? $sum : null; } return $cpu === null || $memory === null || $disk === null ? null : [ 'cpu' => (int) ceil($cpu), 'memory' => (int) ceil($memory), 'disk' => (int) ceil($disk), ]; } private static function positiveNumber($value): ?float { if (is_bool($value) || (!is_int($value) && !is_float($value) && !(is_string($value) && is_numeric($value)))) { return null; } $number = (float) $value; return is_finite($number) && $number > 0 ? $number : null; } }