From 15a323385cbccde917d6619902f7ca65e23bf79e Mon Sep 17 00:00:00 2001 From: codex Date: Sat, 31 Oct 2026 04:01:22 +0800 Subject: [PATCH] feat(admin): project agent status by region --- .../Cloud/CloudAdminHostProjectionService.php | 7 + .../Cloud/CloudAgentSettingsService.php | 164 +++++++++++++++--- .../CloudAdminHostListProjectionTest.php | 30 ++++ 3 files changed, 180 insertions(+), 21 deletions(-) diff --git a/web/app/Services/Cloud/CloudAdminHostProjectionService.php b/web/app/Services/Cloud/CloudAdminHostProjectionService.php index e9864ace..d5286ed6 100644 --- a/web/app/Services/Cloud/CloudAdminHostProjectionService.php +++ b/web/app/Services/Cloud/CloudAdminHostProjectionService.php @@ -253,6 +253,11 @@ final class CloudAdminHostProjectionService '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 ? '' @@ -261,6 +266,8 @@ final class CloudAdminHostProjectionService : (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, diff --git a/web/app/Services/Cloud/CloudAgentSettingsService.php b/web/app/Services/Cloud/CloudAgentSettingsService.php index b890ddaa..b3e12d1c 100644 --- a/web/app/Services/Cloud/CloudAgentSettingsService.php +++ b/web/app/Services/Cloud/CloudAgentSettingsService.php @@ -42,23 +42,54 @@ final class CloudAgentSettingsService public static function listing(): array { - $hosts = CloudHost::query()->orderBy('id')->get([ - 'id', 'name', 'region', 'country_code', 'enabled', 'default_network', - ]); - $settings = CloudHostAgent::query()->whereIn( - 'host_id', $hosts->pluck('id')->all() - )->get()->keyBy('host_id'); - $storage = CloudHostStorageConfiguration::query()->whereIn( - 'host_id', $hosts->pluck('id')->all() - )->get()->keyBy('host_id'); + $hosts = CloudAdminHostProjectionService::listing(); + $agents = []; + $byHost = []; + foreach ($hosts['hosts'] as $host) { + $agent = self::projectedDto($host); + $agents[] = $agent; + $byHost[(int) $agent['host_id']] = $agent; + } - return ['agents' => $hosts->map(static function ($host) use ( - $settings, $storage - ): array { - $setting = $settings->get((int) $host->id); - return self::dto($host, $setting, - $storage->get((int) $host->id)); - })->all()]; + $regions = []; + foreach ($hosts['regions'] as $region) { + $group = [ + 'id' => $region['id'], + 'code' => $region['code'], + 'name' => $region['name'], + 'status' => $region['status'], + 'sort' => $region['sort'], + 'country_code' => $region['country_code'], + 'country_name' => $region['country_name'], + 'country_flag' => $region['country_flag'], + 'summary' => self::emptyAgentSummary(), + 'agents' => [], + ]; + foreach ($region['hosts'] as $host) { + $hostId = (int) ($host['id'] ?? 0); + if (!isset($byHost[$hostId])) { + continue; + } + $group['agents'][] = $byHost[$hostId]; + self::addAgentToSummary($group['summary'], $byHost[$hostId]); + } + $regions[] = $group; + } + + $summary = self::emptyAgentSummary() + ['regions_total' => 0]; + $summary['regions_total'] = count(array_filter( + $regions, + static fn (array $region): bool => $region['id'] !== null + )); + foreach ($agents as $agent) { + self::addAgentToSummary($summary, $agent); + } + + return [ + 'summary' => $summary, + 'regions' => $regions, + 'agents' => $agents, + ]; } public static function read(int $hostId): array @@ -429,30 +460,121 @@ final class CloudAgentSettingsService $ready = $setting !== null && $setting->isReady() && CloudNetworkSelector::isValid($host->default_network) && $storage !== null && $storage->isApplied(); - return [ + return self::withStatus([ 'host_id' => (int) $host->id, 'host_name' => (string) $host->name, 'region' => (string) $host->region, 'country_code' => (string) $host->country_code, 'host_enabled' => (bool) $host->enabled, 'agent_id' => (string) $host->id, + 'ready' => $ready, + 'storage_configured' => $storage !== null + && $storage->isApplied(), + ] + self::settingFields($setting)); + } + + private static function projectedDto(array $host): array + { + $agent = is_array($host['agent'] ?? null) ? $host['agent'] : []; + $readiness = is_array($host['readiness'] ?? null) + ? $host['readiness'] : []; + $ready = ($agent['ready'] ?? false) === true + && ($readiness['network_selector_ready'] ?? false) === true + && ($readiness['storage_ready'] ?? false) === true; + return self::withStatus([ + 'host_id' => (int) ($host['id'] ?? 0), + 'host_name' => (string) ($host['name'] ?? ''), + 'region' => (string) ($host['region'] ?? ''), + 'country_code' => (string) ($host['country_code'] ?? ''), + 'host_enabled' => ($host['enabled'] ?? false) === true, + 'scheduler_state' => (string) ($host['scheduler_state'] + ?? 'disabled'), + 'placement' => is_array($host['placement'] ?? null) + ? $host['placement'] : [], + 'location' => is_array($host['location'] ?? null) + ? $host['location'] : [], + 'agent_id' => (string) ($host['id'] ?? ''), + 'enabled' => ($agent['enabled'] ?? false) === true, + 'secret_configured' => + ($agent['secret_configured'] ?? false) === true, + 'revision' => (int) ($agent['revision'] ?? 0), + 'tested_revision' => (int) ($agent['tested_revision'] ?? 0), + 'ready' => $ready, + 'storage_configured' => + ($readiness['storage_ready'] ?? false) === true, + 'last_test_status' => (string) ($agent['last_test_status'] + ?? 'never'), + 'last_error_code' => (string) ($agent['last_error_code'] ?? ''), + 'last_agent_version' => (string) ($agent['version'] ?? ''), + 'last_tested_at' => (int) ($agent['last_tested_at'] ?? 0), + 'updated_at' => (int) ($agent['updated_at'] ?? 0), + ]); + } + + private static function settingFields(?CloudHostAgent $setting): array + { + return [ 'enabled' => $setting !== null && (bool) $setting->enabled, 'secret_configured' => $setting !== null && trim((string) $setting->sealed_api_secret) !== '', 'revision' => $setting === null ? 0 : (int) $setting->revision, 'tested_revision' => $setting === null ? 0 : (int) $setting->tested_revision, - 'ready' => $ready, - 'storage_configured' => $storage !== null - && $storage->isApplied(), 'last_test_status' => $setting === null ? 'never' : (string) $setting->last_test_status, 'last_error_code' => $setting === null ? '' : (string) $setting->last_error_code, 'last_agent_version' => $setting === null ? '' : (string) $setting->last_agent_version, - 'last_tested_at' => $setting === null ? 0 : (int) $setting->last_tested_at, + 'last_tested_at' => $setting === null + ? 0 : (int) $setting->last_tested_at, 'updated_at' => $setting === null ? 0 : (int) $setting->updated_at, ]; } + + private static function withStatus(array $agent): array + { + if (!($agent['secret_configured'] ?? false)) { + $status = 'unconfigured'; + } elseif (!($agent['enabled'] ?? false)) { + $status = 'disabled'; + } elseif (($agent['ready'] ?? false) === true) { + $status = 'ready'; + } elseif (($agent['last_test_status'] ?? '') === 'unhealthy') { + $status = 'failed'; + } else { + $status = 'pending'; + } + $agent['status'] = $status; + $agent['needs_attention'] = in_array( + $status, ['unconfigured', 'pending', 'failed'], true + ); + return $agent; + } + + private static function emptyAgentSummary(): array + { + return [ + 'agents_total' => 0, + 'ready_agents' => 0, + 'pending_agents' => 0, + 'failed_agents' => 0, + 'unconfigured_agents' => 0, + 'disabled_agents' => 0, + 'attention_agents' => 0, + ]; + } + + private static function addAgentToSummary(array &$summary, + array $agent): void + { + $status = (string) ($agent['status'] ?? 'unconfigured'); + $key = $status . '_agents'; + $summary['agents_total']++; + if (array_key_exists($key, $summary)) { + $summary[$key]++; + } + $summary['attention_agents'] += + ($agent['needs_attention'] ?? false) ? 1 : 0; + } } diff --git a/web/tests/CloudAdminHostListProjectionTest.php b/web/tests/CloudAdminHostListProjectionTest.php index 275d8d79..f525a45c 100644 --- a/web/tests/CloudAdminHostListProjectionTest.php +++ b/web/tests/CloudAdminHostListProjectionTest.php @@ -3,6 +3,7 @@ require dirname(__DIR__) . '/vendor/autoload.php'; use App\Controllers\CloudAdminController; +use App\Services\Cloud\CloudAgentSettingsService; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Schema\Blueprint; use Slim\Http\Environment; @@ -305,6 +306,28 @@ $assert($payload['summary']['regions_total'] === 1 && $payload['regions'][0]['summary']['hosts_total'] === 1 && $payload['regions'][1]['id'] === null, 'Host list did not provide stable region groups and page summary data.'); +$assert($first['agent']['secret_configured'] === true + && $first['agent']['revision'] === 2 + && $first['agent']['tested_revision'] === 2 + && $first['agent']['updated_at'] === 100 + && !array_key_exists('sealed_api_secret', $first['agent']), + 'Host projection did not expose safe Agent metadata or leaked its secret.'); + +Capsule::connection('default')->flushQueryLog(); +$agentListing = CloudAgentSettingsService::listing(); +$agentQueries = count(Capsule::connection('default')->getQueryLog()); +$assert($agentListing['summary']['regions_total'] === 1 + && $agentListing['summary']['agents_total'] === 2 + && $agentListing['summary']['ready_agents'] === 1 + && $agentListing['summary']['unconfigured_agents'] === 1 + && $agentListing['summary']['attention_agents'] === 1 + && count($agentListing['regions']) === 2 + && $agentListing['regions'][0]['name'] === 'HongKong' + && $agentListing['regions'][0]['agents'][0]['status'] === 'ready' + && $agentListing['regions'][1]['agents'][0]['status'] === 'unconfigured' + && !str_contains(json_encode($agentListing, JSON_THROW_ON_ERROR), + 'sealed_api_secret'), + 'Agent administrator projection lost grouping, status totals or secrecy.'); Capsule::table('cloud_hosts')->insert([ 'id' => 3, 'name' => 'Host C', 'region' => 'Singapore', @@ -322,6 +345,13 @@ $expandedPayload = json_decode( $assert(count($expandedPayload['hosts']) === 3 && $queries === $expandedQueries, 'Host list introduced an N+1 query pattern: ' . $queries . ' versus ' . $expandedQueries . ' queries.'); +Capsule::connection('default')->flushQueryLog(); +$expandedAgentListing = CloudAgentSettingsService::listing(); +$expandedAgentQueries = count(Capsule::connection('default')->getQueryLog()); +$assert($expandedAgentListing['summary']['agents_total'] === 3 + && $agentQueries === $expandedAgentQueries, + 'Agent listing introduced an N+1 query pattern: ' + . $agentQueries . ' versus ' . $expandedAgentQueries . ' queries.'); $oneRequest = Request::createFromEnvironment(Environment::mock([ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts/1',