,hosts:list} */ public static function listing(): array { $hosts = CloudHost::orderBy('id')->get(); if ($hosts->isEmpty()) { return [ 'summary' => self::emptySummary(), 'regions' => [], 'hosts' => [], ]; } $hostIds = $hosts->pluck('id')->map( static fn ($id): int => (int) $id )->all(); $memberships = CloudHostResourcePoolMembership::whereIn( 'host_id', $hostIds )->orderBy('host_id')->orderBy('resource_pool_id')->orderBy('id')->get(); $membershipsByHost = []; foreach ($memberships as $membership) { $membershipsByHost[(int) $membership->host_id][] = $membership; } $selectedByHost = []; $poolIds = []; foreach ($hosts as $host) { $selected = CloudHostPoolMembershipService::selected( $membershipsByHost[(int) $host->id] ?? [] ); $selectedByHost[(int) $host->id] = $selected; if ($selected !== null) { $poolIds[] = (int) $selected->resource_pool_id; } } $pools = self::keyById($poolIds === [] ? [] : CloudResourcePool::whereIn('id', array_values(array_unique( $poolIds )))->orderBy('sort')->orderBy('id')->get()); $lineIds = []; foreach ($pools as $pool) { $lineIds[] = (int) $pool->line_id; } $lines = self::keyById($lineIds === [] ? [] : CloudNetworkLine::whereIn('id', array_values(array_unique( $lineIds )))->orderBy('sort')->orderBy('id')->get()); $regionIds = []; foreach ($lines as $line) { $regionIds[] = (int) $line->region_id; } $regions = self::keyById($regionIds === [] ? [] : CloudRegion::whereIn('id', array_values(array_unique( $regionIds )))->orderBy('sort')->orderBy('id')->get()); $policies = self::keyByHost(CloudHostCapacityPolicy::whereIn( 'host_id', $hostIds )->get()); $agents = self::keyByHost(CloudHostAgent::whereIn( 'host_id', $hostIds )->get()); $storage = self::keyByHost(CloudHostStorageConfiguration::whereIn( 'host_id', $hostIds )->get()); $usage = CloudPanelUsageService::forHosts($hostIds); $ipStats = self::ipStats($hostIds); $flat = []; $groups = []; foreach ($hosts as $host) { $hostId = (int) $host->id; $membership = $selectedByHost[$hostId] ?? null; $pool = $membership === null ? null : ($pools[(int) $membership->resource_pool_id] ?? null); $line = $pool === null ? null : ($lines[(int) $pool->line_id] ?? null); $region = $line === null ? null : ($regions[(int) $line->region_id] ?? null); $lineIp = $line === null ? self::emptyIpStats() : ($ipStats[$hostId][(int) $line->id] ?? self::emptyIpStats()); $row = self::hostDto( $host, $membership, $pool, $line, $region, $policies[$hostId] ?? null, $agents[$hostId] ?? null, $storage[$hostId] ?? null, $usage[$hostId] ?? CloudPanelUsageService::emptyUsage(), $lineIp ); $flat[] = $row; $key = $region === null ? 'unclassified' : 'region:' . $region->id; if (!isset($groups[$key])) { $groups[$key] = self::regionDto($region, $host); } $groups[$key]['hosts'][] = $row; self::addHostToGroup($groups[$key], $row); } $groupRows = array_values($groups); usort($groupRows, static function (array $left, array $right): int { if ($left['id'] === null) { return $right['id'] === null ? 0 : 1; } if ($right['id'] === null) { return -1; } return [$left['sort'], $left['id']] <=> [$right['sort'], $right['id']]; }); foreach ($groupRows as &$group) { usort($group['hosts'], static fn (array $left, array $right): int => [$left['name'], $left['id']] <=> [$right['name'], $right['id']] ); } unset($group); return [ 'summary' => self::summary($flat, $groupRows), 'regions' => $groupRows, 'hosts' => $flat, ]; } public static function all(): array { return self::listing()['hosts']; } public static function one(CloudHost $host): array { $memberships = CloudHostResourcePoolMembership::where( 'host_id', (int) $host->id )->orderBy('resource_pool_id')->orderBy('id')->get(); return self::basicDto($host, $memberships); } private static function basicDto(CloudHost $host, iterable $memberships): array { $selected = CloudHostPoolMembershipService::selected($memberships); $state = (string) ($selected?->scheduler_state ?? 'disabled'); $dto = $host->toArray(); $dto['resource_pool_id'] = $selected === null ? null : (int) $selected->resource_pool_id; $dto['scheduler_state'] = in_array($state, self::SCHEDULER_STATES, true) ? $state : 'disabled'; return $dto; } private static function hostDto(CloudHost $host, $membership, $pool, $line, $region, $policy, ?CloudHostAgent $agent, ?CloudHostStorageConfiguration $storage, array $usage, array $ip): array { $base = self::basicDto($host, $membership === null ? [] : [$membership]); $scheduler = (string) $base['scheduler_state']; $selectorReady = CloudNetworkSelector::isValid( (string) $host->default_network ); $storageReady = $storage !== null && $storage->isApplied(); $agentReady = $agent !== null && $agent->isReady(); $capacity = self::capacity($policy, $usage, $ip); $capacityConfigured = $policy !== null && $capacity['cpu']['configured'] > 0 && $capacity['memory']['configured'] > 0 && $capacity['disk']['configured'] > 0 && $capacity['cpu']['safety'] < $capacity['cpu']['configured'] && $capacity['memory']['safety'] < $capacity['memory']['configured'] && $capacity['disk']['safety'] < $capacity['disk']['configured']; $maxInstances = (int) $capacity['instances']['max']; $instanceCount = (int) $capacity['instances']['allocated'] + (int) $capacity['instances']['reserved']; $poolPathReady = $pool !== null && (string) $pool->status === 'active' && $line !== null && (string) $line->status === 'active' && $region !== null && (string) $region->status === 'active'; $placementReady = (bool) $host->enabled && $scheduler === 'active' && $poolPathReady && $selectorReady && $agentReady && $storageReady && $capacityConfigured && (int) $capacity['unknown_instances'] === 0 && ($maxInstances <= 0 || $instanceCount < $maxInstances) && $capacity['cpu']['free'] > 0 && $capacity['memory']['free'] > 0 && $capacity['disk']['free'] > 0 && $capacity['ip']['available'] > 0; $attention = self::attention( $host, $membership, $pool, $line, $region, $scheduler, $selectorReady, $agentReady, $storageReady, $capacityConfigured, $capacity ); $country = CloudCountryCatalog::option((string) ($region?->country_code ?? $host->country_code)); return $base + [ 'location' => [ 'physical_label' => (string) $host->region, 'country_code' => $country['code'], 'country_name' => $country['name'], 'country_flag' => $country['flag'], ], 'placement' => [ 'region_id' => $region === null ? null : (int) $region->id, 'region_code' => $region === null ? '' : (string) $region->code, 'region_name' => $region === null ? '未分类节点' : (string) $region->name, 'region_status' => $region === null ? 'missing' : (string) $region->status, 'line_id' => $line === null ? null : (int) $line->id, 'line_name' => $line === null ? '' : (string) $line->name, 'line_status' => $line === null ? 'missing' : (string) $line->status, 'pool_id' => $pool === null ? null : (int) $pool->id, 'pool_name' => $pool === null ? '' : (string) $pool->name, 'pool_status' => $pool === null ? 'missing' : (string) $pool->status, ], 'agent' => [ 'state' => self::agentState($agent), 'ready' => $agentReady, 'enabled' => $agent !== null && (bool) $agent->enabled, 'secret_configured' => $agent !== null && trim((string) $agent->sealed_api_secret) !== '', 'revision' => $agent === null ? 0 : (int) $agent->revision, 'tested_revision' => $agent === null ? 0 : (int) $agent->tested_revision, 'last_test_status' => $agent === null ? 'never' : (string) $agent->last_test_status, 'last_error_code' => $agent === null ? '' : (string) $agent->last_error_code, 'version' => $agent === null ? '' : (string) $agent->last_agent_version, 'last_tested_at' => $agent === null ? 0 : (int) $agent->last_tested_at, 'updated_at' => $agent === null ? 0 : (int) $agent->updated_at, ], 'readiness' => [ 'network_selector_ready' => $selectorReady, 'storage_ready' => $storageReady, 'capacity_configured' => $capacityConfigured, 'placement_ready' => $placementReady, ], 'capacity' => $capacity, 'attention' => $attention, 'needs_attention' => $attention !== [], ]; } private static function agentState(?CloudHostAgent $agent): string { if ($agent === null) { return 'unconfigured'; } if (!(bool) $agent->enabled) { return 'disabled'; } if ($agent->isReady()) { return 'ready'; } if ((string) $agent->last_test_status === 'failed') { return 'failed'; } return 'pending'; } private static function capacity($policy, array $usage, array $ip): array { $result = []; foreach ([ 'cpu' => ['configured_cpu_cores', 'safety_cpu_cores'], 'memory' => ['configured_memory_mib', 'safety_memory_mib'], 'disk' => ['configured_disk_gib', 'safety_disk_gib'], ] as $key => [$configuredField, $safetyField]) { $configured = max(0, (int) ($policy?->{$configuredField} ?? 0)); $safety = max(0, (int) ($policy?->{$safetyField} ?? 0)); $allocated = max(0, (int) ($usage[$key]['allocated'] ?? 0)); $reserved = max(0, (int) ($usage[$key]['reserved'] ?? 0)); $allocatable = max(0, $configured - $safety); $free = max(0, $allocatable - $allocated - $reserved); $result[$key] = compact( 'configured', 'safety', 'allocatable', 'allocated', 'reserved', 'free' ); } $result['instances'] = [ 'allocated' => max(0, (int) ($usage['instances']['allocated'] ?? 0)), 'reserved' => max(0, (int) ($usage['instances']['reserved'] ?? 0)), 'max' => max(0, (int) ($policy?->max_instances ?? 0)), ]; $result['ip'] = $ip; $result['unknown_instances'] = max(0, (int) ($usage['unknown'] ?? 0)); return $result; } private static function attention(CloudHost $host, $membership, $pool, $line, $region, string $scheduler, bool $selectorReady, bool $agentReady, bool $storageReady, bool $capacityConfigured, array $capacity): array { $attention = []; if ($membership === null || $pool === null || $line === null || $region === null) { $attention[] = 'placement_unclassified'; } if (!$selectorReady) { $attention[] = 'network_selector_invalid'; } if (!$agentReady) { $attention[] = 'agent_not_ready'; } if (!$storageReady) { $attention[] = 'storage_not_ready'; } if ((int) $capacity['unknown_instances'] > 0) { $attention[] = 'instance_capacity_unknown'; } if ($scheduler === 'active') { if (!(bool) $host->enabled) { $attention[] = 'host_service_disabled'; } if ($pool !== null && (string) $pool->status !== 'active') { $attention[] = 'pool_inactive'; } if ($line !== null && (string) $line->status !== 'active') { $attention[] = 'line_inactive'; } if ($region !== null && (string) $region->status !== 'active') { $attention[] = 'region_inactive'; } if (!$capacityConfigured) { $attention[] = 'capacity_not_configured'; } if ((int) $capacity['ip']['available'] <= 0) { $attention[] = 'public_ip_unavailable'; } } return array_values(array_unique($attention)); } private static function ipStats(array $hostIds): array { $result = []; if ($hostIds === []) { return $result; } $rows = CloudPublicIp::query() ->join('cloud_public_ip_pools as public_pool', 'public_pool.id', '=', 'cloud_public_ips.public_ip_pool_id') ->join('cloud_host_network_attachments as network_attachment', 'network_attachment.id', '=', 'public_pool.network_attachment_id') ->join('cloud_hosts as selected_host', 'selected_host.id', '=', 'cloud_public_ips.host_id') ->whereIn('cloud_public_ips.host_id', $hostIds) ->whereColumn('public_pool.host_id', 'cloud_public_ips.host_id') ->whereColumn('network_attachment.host_id', 'cloud_public_ips.host_id') ->whereColumn('network_attachment.network_selector', 'selected_host.default_network') ->groupBy('cloud_public_ips.host_id', 'network_attachment.line_id') ->selectRaw( 'cloud_public_ips.host_id, network_attachment.line_id, ' . 'COUNT(*) AS total_count, ' . "SUM(CASE WHEN cloud_public_ips.state = 'assigned' " . 'THEN 1 ELSE 0 END) AS allocated_count, ' . "SUM(CASE WHEN cloud_public_ips.state IN " . "('reserved','changing','uncertain') THEN 1 ELSE 0 END) " . 'AS reserved_count, ' . "SUM(CASE WHEN cloud_public_ips.state = 'disabled' " . 'THEN 1 ELSE 0 END) AS disabled_count, ' . "SUM(CASE WHEN cloud_public_ips.state = 'available' " . 'AND cloud_public_ips.reservation_id IS NULL ' . 'AND cloud_public_ips.instance_id IS NULL ' . 'AND cloud_public_ips.operation_id IS NULL ' . "AND network_attachment.state = 'active' " . "AND network_attachment.sync_state = 'applied' " . 'AND network_attachment.observed_revision >= ' . 'network_attachment.desired_revision ' . "AND public_pool.state = 'active' " . "AND public_pool.sync_state = 'applied' " . 'AND public_pool.observed_revision >= public_pool.desired_revision ' . 'THEN 1 ELSE 0 END) AS available_count' )->get(); foreach ($rows as $row) { $result[(int) $row->host_id][(int) $row->line_id] = [ 'total' => (int) $row->total_count, 'allocated' => (int) $row->allocated_count, 'reserved' => (int) $row->reserved_count, 'available' => (int) $row->available_count, 'disabled' => (int) $row->disabled_count, ]; } return $result; } private static function emptyIpStats(): array { return [ 'total' => 0, 'allocated' => 0, 'reserved' => 0, 'available' => 0, 'disabled' => 0, ]; } private static function regionDto($region, CloudHost $host): array { $country = CloudCountryCatalog::option((string) ($region?->country_code ?? $host->country_code)); return [ 'id' => $region === null ? null : (int) $region->id, 'code' => $region === null ? 'unclassified' : (string) $region->code, 'name' => $region === null ? '未分类节点' : (string) $region->name, 'status' => $region === null ? 'missing' : (string) $region->status, 'sort' => $region === null ? PHP_INT_MAX : (int) $region->sort, 'country_code' => $country['code'], 'country_name' => $country['name'], 'country_flag' => $country['flag'], 'summary' => self::emptyGroupSummary(), 'capacity' => self::emptyAggregateCapacity(), 'hosts' => [], ]; } private static function addHostToGroup(array &$group, array $host): void { $summary = &$group['summary']; $summary['hosts_total']++; $summary['enabled_hosts'] += $host['enabled'] ? 1 : 0; $scheduler = (string) $host['scheduler_state']; $summary['scheduler_' . $scheduler]++; $summary['agent_ready_hosts'] += $host['agent']['ready'] ? 1 : 0; $summary['placement_ready_hosts'] += $host['readiness']['placement_ready'] ? 1 : 0; $summary['attention_hosts'] += $host['needs_attention'] ? 1 : 0; self::addCapacity($group['capacity'], $host['capacity']); } private static function summary(array $hosts, array $regions): array { $summary = self::emptySummary(); $summary['regions_total'] = count(array_filter( $regions, static fn (array $region): bool => $region['id'] !== null )); foreach ($hosts as $host) { $summary['hosts_total']++; $summary['enabled_hosts'] += $host['enabled'] ? 1 : 0; $summary['scheduler_active'] += $host['scheduler_state'] === 'active' ? 1 : 0; $summary['scheduler_draining'] += $host['scheduler_state'] === 'draining' ? 1 : 0; $summary['scheduler_disabled'] += $host['scheduler_state'] === 'disabled' ? 1 : 0; $summary['agent_ready_hosts'] += $host['agent']['ready'] ? 1 : 0; $summary['placement_ready_hosts'] += $host['readiness']['placement_ready'] ? 1 : 0; $summary['attention_hosts'] += $host['needs_attention'] ? 1 : 0; self::addCapacity($summary['capacity'], $host['capacity']); } return $summary; } private static function emptySummary(): array { return self::emptyGroupSummary() + [ 'regions_total' => 0, 'capacity' => self::emptyAggregateCapacity(), ]; } private static function emptyGroupSummary(): array { return [ 'hosts_total' => 0, 'enabled_hosts' => 0, 'scheduler_active' => 0, 'scheduler_draining' => 0, 'scheduler_disabled' => 0, 'agent_ready_hosts' => 0, 'placement_ready_hosts' => 0, 'attention_hosts' => 0, ]; } private static function emptyAggregateCapacity(): array { return [ 'cpu' => self::emptyResourceCapacity(), 'memory' => self::emptyResourceCapacity(), 'disk' => self::emptyResourceCapacity(), 'instances' => ['allocated' => 0, 'reserved' => 0, 'max' => 0], 'ip' => self::emptyIpStats(), ]; } private static function emptyResourceCapacity(): array { return [ 'configured' => 0, 'safety' => 0, 'allocatable' => 0, 'allocated' => 0, 'reserved' => 0, 'free' => 0, ]; } private static function addCapacity(array &$target, array $source): void { foreach (['cpu', 'memory', 'disk'] as $resource) { foreach (array_keys(self::emptyResourceCapacity()) as $field) { $target[$resource][$field] += (int) ($source[$resource][$field] ?? 0); } } foreach (['allocated', 'reserved', 'max'] as $field) { $target['instances'][$field] += (int) ($source['instances'][$field] ?? 0); } foreach (array_keys(self::emptyIpStats()) as $field) { $target['ip'][$field] += (int) ($source['ip'][$field] ?? 0); } } private static function keyById(iterable $rows): array { $result = []; foreach ($rows as $row) { $result[(int) $row->id] = $row; } return $result; } private static function keyByHost(iterable $rows): array { $result = []; foreach ($rows as $row) { $result[(int) $row->host_id] = $row; } return $result; } }