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.
virt/web/app/Services/Cloud/CloudPanelUsageService.php

144 lines
5.3 KiB
PHTML

<?php
namespace App\Services\Cloud;
use App\Models\CloudHost;
use App\Models\CloudInstance;
use App\Models\CloudResourceReservation;
/**
* Panel-owned host usage. Reservations cover current provisioning and paid
* instances; older panel rows without a reservation are counted from their
* immutable metadata so retiring inventory cannot create phantom capacity.
*/
final class CloudPanelUsageService
{
/**
* The panel deliberately does not persist the engine's SSH/management
* address in cloud_hosts. Operators must put a management IPv4 in the
* public-pool reserved_ips list, which is materialized into the ledger.
*/
public static function managementAddress(?CloudHost $host): ?string
{
return null;
}
public static function managementAddressFromValue($value): ?string
{
$address = trim((string) ($value ?? ''));
return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false
? null : $address;
}
public static function forHost(int $hostId, $reservationRows = null): array
{
$usage = self::emptyUsage();
$reservationRows ??= CloudResourceReservation::where('host_id', $hostId)
->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;
}
}