|
|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|