['可用', 'running'], 'creating' => ['正在备份', 'warning'], 'queued' => ['等待备份', 'warning'], 'failed' => ['备份失败', 'stopped'], 'uncertain' => ['等待核对', 'warning'], 'deleting' => ['正在删除', 'warning'], 'delete_uncertain' => ['删除待核对', 'warning'], 'delete_attention' => ['清理需人工处理', 'stopped'], ]); $source = $backup['source_instance'] ?? []; $sourceId = (string) ($source['id'] ?? ''); $deleted = ($source['deleted'] ?? false) === true || in_array($source['status'] ?? '', ['deleted', 'missing'], true); $snapshot = $backup['cloud_snapshot'] ?? []; $snapshotKey = (string) ($snapshot['snapshot_key'] ?? $snapshot['key'] ?? ''); $items[] = [ 'id' => $id, 'name' => self::text($backup['name'] ?? '', '云备份 #' . $id), 'source_label' => $deleted ? '源实例已删除' : '来源:' . self::text($source['name'] ?? '', $sourceId !== '' ? '实例 #' . $sourceId : '实例未记录'), 'state_label' => $label, 'state_class' => $class, 'date_display' => self::date($backup['ready_at'] ?? $backup['created_at'] ?? null), 'consistency_display' => ($backup['consistency'] ?? '') === 'filesystem' ? '文件系统一致' : '崩溃一致', 'object_display' => self::bytes($backup['object_size_bytes'] ?? null), 'disk_display' => self::bytes($backup['virtual_size_bytes'] ?? null), 'restore_href' => !$deleted && ($backup['can_restore_original'] ?? false) === true && preg_match('/^[1-9][0-9]*$/D', $sourceId) === 1 ? '/user/cloud/instances/' . $sourceId . '?tab=settings&setting=automatic_backup' : '', 'snapshot_href' => preg_match('/^[a-f0-9]{32}$/D', $snapshotKey) === 1 ? '/user/cloud/cloud-snapshots#cloud-snapshot-' . $snapshotKey : '', 'can_promote' => ($backup['can_promote'] ?? false) === true, ]; } return ['items' => $items, 'pagination' => self::pagination($payload)]; } public static function cloudSnapshots(array $payload): array { $items = []; foreach ($payload['items'] ?? [] as $snapshot) { $key = (string) ($snapshot['snapshot_key'] ?? ''); [$label, $class] = self::state((string) ($snapshot['state'] ?? ''), [ 'ready' => ['可用', 'running'], 'promoting' => ['正在保存', 'warning'], 'deleting' => ['正在删除', 'warning'], 'delete_uncertain' => ['删除待核对', 'warning'], 'failed' => ['保存失败', 'stopped'], ]); $source = $snapshot['source_instance'] ?? []; $sourceName = self::text($source['name'] ?? '', !empty($source['id']) ? '实例 #' . $source['id'] : '来源实例未记录'); $pending = max(0, (int) ($snapshot['active_invoice_count'] ?? 0)); $items[] = [ 'key' => $key, 'name' => self::text($snapshot['name'] ?? '', '云快照 ' . substr($key, 0, 8)), 'source_label' => '来源:' . $sourceName . ($pending > 0 ? ' · ' . $pending . ' 张待支付账单' : ''), 'state_label' => $label, 'state_class' => $class, 'date_display' => self::date($snapshot['ready_at'] ?? $snapshot['created_at'] ?? null), 'os_display' => self::text($snapshot['os_name'] ?? '', '系统未记录'), 'object_display' => self::bytes($snapshot['object_size_bytes'] ?? null), 'disk_display' => self::bytes($snapshot['virtual_size_bytes'] ?? null), 'create_href' => ($snapshot['can_create_instance'] ?? false) === true && preg_match('/^[a-f0-9]{32}$/D', $key) === 1 ? '/user/cloud/deploy?cloud_snapshot=' . $key : '', 'delete_title' => ($snapshot['can_delete'] ?? false) === true ? '' : ($pending > 0 ? '存在待支付账单,请先支付或等待账单失效后再删除。' : '云快照当前状态不允许删除。'), ]; } return ['items' => $items, 'pagination' => self::pagination($payload)]; } public static function trafficPools(array $payload): array { $linePools = []; foreach ($payload['line_pools'] ?? [] as $pool) { [$stateLabel, $stateClass] = self::state( (string) ($pool['summary_state'] ?? ''), [ 'available' => ['状态正常', 'running'], 'preparing' => ['额度准备中', 'warning'], 'applying' => ['网络策略应用中', 'warning'], 'exhausted' => ['流量已用尽', 'stopped'], 'restricted' => ['线路池已断网', 'stopped'], 'attention' => ['需要核查', 'stopped'], ] ); $coverage = []; foreach ($pool['coverage_regions'] ?? [] as $region) { $name = trim((string) ($region['name'] ?? '')); if ($name !== '' && !in_array($name, $coverage, true)) { $coverage[] = $name; } } $item = self::trafficPool($pool); $item['key'] = self::text( $pool['key'] ?? '', 'line-group:' . (int) ($pool['group_id'] ?? 0) ); $item['coverage_display'] = $coverage === [] ? '覆盖地区待核查' : '覆盖:' . implode('、', $coverage); $item['coverage_region_count'] = max( 0, (int) ($pool['coverage_region_count'] ?? count($coverage)) ); $item['instance_count'] = max( 0, (int) ($pool['instance_count'] ?? count($item['instances'])) ); $item['summary_state_label'] = $stateLabel; $item['summary_state_class'] = $stateClass; $item['requires_attention'] = ($pool['requires_attention'] ?? false) === true; $item['default_open'] = ($pool['default_open'] ?? false) === true; $linePools[] = $item; } return [ 'line_pools' => $linePools, 'line_pool_count' => max( 0, (int) ($payload['line_pool_count'] ?? count($linePools)) ), ]; } /** @return array */ private static function trafficPool(array $pool): array { $projectionReady = ($pool['projection_ready'] ?? false) === true; [$stateLabel, $stateClass] = self::state( (string) ($pool['state'] ?? ''), [ 'available' => ['可用', 'running'], 'exhausted' => ['已用尽', 'stopped'], 'attention' => ['需要核查', 'stopped'], 'preparing' => ['额度准备中', 'warning'], ] ); [$networkLabel, $networkClass] = self::state( (string) ($pool['network_restriction_state'] ?? ''), [ 'clear' => ['网络正常', 'running'], 'applying' => ['网络策略应用中', 'warning'], 'restricted' => ['已因共享流量耗尽断网', 'stopped'], 'attention' => ['网络策略需要人工核查', 'stopped'], ] ); $instances = []; foreach ($pool['instances'] ?? [] as $instance) { [$instanceNetworkLabel, $instanceNetworkClass] = self::state( (string) ($instance['network_status'] ?? ''), [ 'active' => ['网络正常', 'running'], 'blocking' => ['正在停用网络', 'warning'], 'blocked_traffic' => ['共享流量用尽,已断网', 'stopped'], 'blocked_expired' => ['服务到期,已断网', 'stopped'], 'restoring' => ['正在恢复网络', 'warning'], 'restore_pending' => ['等待恢复网络', 'warning'], ] ); $instances[] = [ 'id' => (string) ($instance['id'] ?? ''), 'name' => self::text( $instance['name'] ?? '', '实例 #' . (string) ($instance['id'] ?? '') ), 'used_display' => self::bytes( $instance['traffic_used_bytes'] ?? null ), 'period_quota_display' => self::bytes( $instance['period_traffic_quota_bytes'] ?? null ), 'download_display' => self::bytes( $instance['traffic_download_bytes'] ?? null ), 'upload_display' => self::bytes( $instance['traffic_upload_bytes'] ?? null ), 'expires_display' => self::date( $instance['service_expires_at'] ?? null ), 'region_label' => self::text( is_array($instance['region'] ?? null) ? ($instance['region']['name'] ?? '') : '', '地区待核查' ), 'network_label' => $instanceNetworkLabel, 'network_class' => $instanceNetworkClass, ]; } return [ 'group_id' => (int) ($pool['group_id'] ?? 0), 'name' => self::text($pool['name'] ?? '', '共享流量池'), 'state_label' => $stateLabel, 'state_class' => $stateClass, 'network_label' => $networkLabel, 'network_class' => $networkClass, 'entitlement_display' => $projectionReady ? self::bytes( $pool['active_entitlement_bytes'] ?? null ) : '正在准备', 'consumed_display' => $projectionReady ? self::bytes( $pool['active_consumed_bytes'] ?? null ) : '正在准备', 'available_display' => $projectionReady ? self::bytes( $pool['available_bytes'] ?? null ) : '正在准备', 'overage_display' => $projectionReady ? self::bytes( $pool['overage_bytes'] ?? null ) : '正在准备', 'next_expiry_display' => !$projectionReady ? '额度准备中' : (empty($pool['next_expiry_at']) ? '暂无即将到期额度' : self::date($pool['next_expiry_at'])), 'next_expiry_note' => $projectionReady ? '届时作废 ' . self::bytes( $pool['next_expiry_bytes'] ?? null ) : '账本投影尚未生成', 'last_observed_display' => !$projectionReady ? '等待首次核算' : (empty($pool['last_observed_at']) ? '暂未完成采样' : self::date($pool['last_observed_at'])), 'instances' => $instances, ]; } public static function vpcs(array $payload, array $query): array { $networks = []; $current = null; $key = is_string($query['vpc'] ?? null) ? $query['vpc'] : ''; $view = is_string($query['view'] ?? null) ? $query['view'] : ''; $page = $key !== '' ? 'members' : ($view === 'create' ? 'create' : 'home'); if ((isset($query['vpc']) && preg_match('/^[a-f0-9]{32}$/D', $key) !== 1) || (isset($query['view']) && !in_array($view, ['create', 'members', 'join'], true)) || ($key === '' && in_array($view, ['members', 'join'], true))) { $page = 'missing'; } $batches = $payload['membership_batches'] ?? []; foreach ($payload['items'] ?? [] as $network) { $networkKey = (string) ($network['vpc_key'] ?? ''); $batch = null; foreach ($batches as $candidate) { if (($candidate['vpc_key'] ?? '') === $networkKey) { $batch = $candidate; break; } } [$label, $class] = ($network['state'] ?? '') === 'active' ? ['运行正常', 'running'] : [self::text($network['state'] ?? '', '状态未知'), 'other']; $network['state_label'] = $batch !== null ? '变更中' : $label; $network['state_class'] = $batch !== null ? 'other' : $class; $network['members_href'] = '/user/cloud/vpcs?vpc=' . rawurlencode($networkKey); $network['join_href'] = $network['members_href'] . '&view=join'; $network['batch'] = $batch; $networks[] = $network; if ($networkKey === $key) { $current = $network; $current['state_label'] = $label; $current['state_class'] = $class; } } if ($page === 'members' && $current === null) $page = 'missing'; $members = []; $candidates = []; foreach ($payload['instances'] ?? [] as $instance) { if ($current === null) break; $port = $instance['port'] ?? null; $joined = is_array($port) && ($port['vpc_key'] ?? '') === $key; $candidate = $port === null && (int) ($instance['region_id'] ?? 0) === (int) $current['region_id']; if (!$joined && !$candidate) continue; [$label, $class] = $candidate ? (($instance['can_attach'] ?? false) === true ? ['可加入', 'running'] : ['暂不可加入', 'other']) : self::state((string) ($port['state'] ?? ''), [ 'preparing' => ['正在连接', 'other'], 'attached' => ['已连接', 'running'], 'detaching' => ['正在断开', 'other'], 'release_pending' => ['等待清理', 'other'], 'released' => ['已断开', 'other'], 'migrating' => ['正在迁移', 'other'], ]); foreach ($current['batch']['items'] ?? [] as $batchItem) { if ((string) ($batchItem['instance_id'] ?? '') !== (string) $instance['id']) continue; [$label, $class] = self::state((string) ($batchItem['state'] ?? ''), [ 'waiting_route' => ['等待中', 'other'], 'queued' => ['执行中', 'other'], 'succeeded' => ['已完成', 'running'], 'failed' => ['失败', 'failed'], 'unknown' => ['失败', 'failed'], ]); break; } $instance['state_label'] = $label; $instance['state_class'] = $class; $instance['public_ipv4'] = self::text($instance['public_ipv4'] ?? '', '未分配'); $instance['public_ipv6'] = self::text($instance['public_ipv6'] ?? '', '未分配'); if ($joined) $members[] = $instance; else $candidates[] = $instance; } return [ 'page' => $page, 'joining' => $view === 'join', 'networks' => $networks, 'regions' => $payload['regions'] ?? [], 'current' => $current, 'members' => $view === 'join' ? $candidates : $members, 'member_count' => count($members), 'joinable_count' => count(array_filter($candidates, static fn (array $item): bool => ($item['can_attach'] ?? false) === true)), 'used' => max(0, (int) ($payload['used'] ?? count($networks))), 'maximum' => max(1, (int) ($payload['maximum'] ?? 16)), ]; } public static function isos(array $payload): array { $items = []; foreach ($payload['isos'] ?? [] as $iso) { $iso['size_display'] = self::bytes($iso['size_bytes'] ?? null); $iso['fingerprint_display'] = !empty($iso['sha256']) ? ' · SHA-256 ' . substr((string) $iso['sha256'], 0, 12) . '…' : ''; $items[] = $iso; } return ['items' => $items, 'can_import' => ($payload['can_import'] ?? false) === true, 'import_active' => ($payload['active_import_job']['completed'] ?? true) === false]; } private static function pagination(array $payload): array { $value = $payload['pagination'] ?? []; $page = max(1, (int) ($value['page'] ?? 1)); $pages = max(1, (int) ($value['total_pages'] ?? 1)); return ['page' => $page, 'total_pages' => $pages, 'total' => max(0, (int) ($value['total'] ?? count($payload['items'] ?? []))), 'has_previous' => $page > 1, 'has_next' => $page < $pages]; } private static function text($value, string $fallback): string { $text = trim((string) $value); return $text !== '' ? $text : $fallback; } private static function state(string $value, array $labels): array { return $labels[$value] ?? [self::text($value, '状态未知'), 'other']; } private static function date($value): string { return is_numeric($value) && (float) $value > 0 ? date('Y/m/d H:i:s', (int) ((float) $value >= 1e12 ? (float) $value / 1000 : $value)) : '时间未记录'; } private static function bytes($value): string { if (!is_numeric($value) || (float) $value < 0) return '等待完成'; $amount = (float) $value; $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']; $index = 0; while ($amount >= 1024 && $index < count($units) - 1) { $amount /= 1024; ++$index; } $precision = $index === 0 ? 0 : ($amount >= 100 ? 0 : ($amount >= 10 ? 1 : 2)); return number_format($amount, $precision, '.', '') . ' ' . $units[$index]; } }