diff --git a/web/app/Services/Cloud/CloudAdminVpcControlService.php b/web/app/Services/Cloud/CloudAdminVpcControlService.php index aea7a3c9..985092b3 100644 --- a/web/app/Services/Cloud/CloudAdminVpcControlService.php +++ b/web/app/Services/Cloud/CloudAdminVpcControlService.php @@ -8,6 +8,7 @@ use App\Models\CloudRegion; use App\Models\CloudVpc; use App\Models\CloudVpcHostState; use App\Models\CloudVpcMembershipBatch; +use App\Models\CloudVpcMembershipBatchItem; use App\Models\CloudVpcPort; use App\Services\AdminDataMasker; @@ -198,10 +199,25 @@ final class CloudAdminVpcControlService ->where('state.vpc_id', $vpcId)->orderBy('state.host_id') ->get(['state.*', 'host.name as host_name']) ->map(static fn ($state): array => self::hostState($state))->all(); - $batches = CloudVpcMembershipBatch::query() - ->where('vpc_id', $vpcId)->orderByDesc('id')->limit(10)->get() - ->map(static fn ($batch): array => self::batch($batch))->all(); - $batchIds = array_column($batches, 'id'); + $batchModels = CloudVpcMembershipBatch::query() + ->where('vpc_id', $vpcId)->orderByDesc('id')->limit(10)->get(); + $batchIds = $batchModels->pluck('id')->map( + static fn ($id): int => (int) $id + )->all(); + $itemCounts = []; + if ($batchIds !== []) { + $countRows = CloudVpcMembershipBatchItem::query() + ->whereIn('batch_id', $batchIds) + ->selectRaw('batch_id, state, COUNT(*) AS row_count') + ->groupBy('batch_id')->groupBy('state')->get(); + foreach ($countRows as $row) { + $itemCounts[(int) $row->batch_id][(string) $row->state] + = (int) $row->row_count; + } + } + $batches = $batchModels->map(static fn ($batch): array => self::batch( + $batch, $itemCounts[(int) $batch->id] ?? [] + ))->all(); $jobs = CloudJob::query()->where(static function ($query) use ( $vpcId, $batchIds ): void { @@ -212,20 +228,29 @@ final class CloudAdminVpcControlService })->orderByDesc('id')->limit(20)->get([ 'id', 'operation_id', 'kind', 'phase', 'outcome', 'status', 'error_code', 'vpc_batch_id', 'created_at', 'updated_at', - ])->map(static fn ($job): array => [ - 'id' => (int) $job->id, - 'operation_id' => (string) $job->operation_id, - 'kind' => (string) $job->kind, - 'phase' => (string) $job->phase, - 'outcome' => $job->outcome === null - ? null : (string) $job->outcome, - 'status' => (string) $job->status, - 'error_code' => (string) ($job->error_code ?? ''), - 'batch_id' => $job->vpc_batch_id === null - ? null : (int) $job->vpc_batch_id, - 'updated_at' => (int) $job->updated_at, - 'url' => '/admin/operations?q=' . (int) $job->id, - ])->all(); + ])->map(static function ($job): array { + $labels = CloudOperationCatalog::labels((string) $job->kind); + $terminal = (string) $job->phase === 'terminal'; + $statusLabel = !$terminal ? '执行中' + : ((string) $job->outcome === 'succeeded' + ? '已完成' : '失败'); + return [ + 'id' => (int) $job->id, + 'operation_id' => (string) $job->operation_id, + 'kind' => (string) $job->kind, + 'kind_label' => $labels['kind_label'], + 'phase' => (string) $job->phase, + 'outcome' => $job->outcome === null + ? null : (string) $job->outcome, + 'status' => (string) $job->status, + 'status_label' => $statusLabel, + 'error_code' => (string) ($job->error_code ?? ''), + 'batch_id' => $job->vpc_batch_id === null + ? null : (int) $job->vpc_batch_id, + 'updated_at' => (int) $job->updated_at, + 'url' => '/admin/operations?q=' . (int) $job->id, + ]; + })->all(); return [ 'vpc' => self::row($vpc, $actor, $sets, $related), 'ports' => $ports, @@ -527,8 +552,12 @@ final class CloudAdminVpcControlService ]; } - private static function batch($batch): array + private static function batch($batch, array $itemCounts = []): array { + $total = array_sum($itemCounts); + $finished = (int) ($itemCounts['succeeded'] ?? 0) + + (int) ($itemCounts['failed'] ?? 0) + + (int) ($itemCounts['unknown'] ?? 0); return [ 'id' => (int) $batch->id, 'batch_key' => (string) $batch->batch_key, @@ -541,6 +570,13 @@ final class CloudAdminVpcControlService 'terminal_at' => $batch->terminal_at === null ? null : (int) $batch->terminal_at, 'updated_at' => (int) $batch->updated_at, + 'items' => [ + 'total' => $total, + 'finished' => $finished, + 'succeeded' => (int) ($itemCounts['succeeded'] ?? 0), + 'failed' => (int) ($itemCounts['failed'] ?? 0), + 'unknown' => (int) ($itemCounts['unknown'] ?? 0), + ], 'job_url' => '/admin/operations?q=' . (int) $batch->parent_job_id, ]; } diff --git a/web/public/cloud/admin-vpc.css b/web/public/cloud/admin-vpc.css index 6e2e7875..955ac17a 100644 --- a/web/public/cloud/admin-vpc.css +++ b/web/public/cloud/admin-vpc.css @@ -434,6 +434,106 @@ button.admin-vpc-summary-card:focus-visible { display: contents; } +.admin-vpc-detail-body, +.admin-vpc-detail-list { + display: grid; + gap: 14px; +} + +.admin-vpc-detail-section { + display: grid; + gap: 12px; +} + +.admin-vpc-detail-section + .admin-vpc-detail-section { + margin-top: 24px; + padding-top: 24px; + border-top: 1px solid #e5ebf2; +} + +.admin-vpc-detail-section > header h3 { + margin: 0 0 4px; + color: #172033; + font-size: 16px; +} + +.admin-vpc-detail-section > header p { + margin: 0; + color: #7d8a9c; + font-size: 12px; + line-height: 1.5; +} + +.admin-vpc-detail-card { + padding: 15px; + border: 1px solid #dfe7ef; + border-radius: 12px; + background: #fbfcfe; +} + +.admin-vpc-detail-card > header, +.admin-vpc-detail-card > footer { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; +} + +.admin-vpc-detail-card h4 { + margin: 0 0 3px; + color: #26354a; + font-size: 14px; +} + +.admin-vpc-detail-card header p, +.admin-vpc-detail-note { + margin: 0; + color: #8290a2; + font-size: 11px; + overflow-wrap: anywhere; +} + +.admin-vpc-detail-card dl { + display: grid; + grid-template-columns: 110px minmax(0, 1fr); + gap: 8px 12px; + margin: 14px 0; + padding: 12px; + border-radius: 9px; + background: #fff; +} + +.admin-vpc-detail-card dt { + color: #8a99ab; + font-size: 11px; + font-weight: 650; +} + +.admin-vpc-detail-card dd { + margin: 0; + color: #34445a; + font-size: 12px; + overflow-wrap: anywhere; +} + +.admin-vpc-detail-card.is-compact .admin-vpc-detail-note { + margin: 12px 0; + padding: 10px 12px; + border-radius: 9px; + background: #fff; + line-height: 1.55; +} + +.admin-vpc-detail-empty { + margin: 0; + padding: 20px; + border-radius: 10px; + color: #8290a2; + background: #f8fafc; + text-align: center; + font-size: 13px; +} + @media (max-width: 1399.98px) { .admin-vpc-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); @@ -576,6 +676,20 @@ button.admin-vpc-summary-card:focus-visible { text-align: center; } + .admin-vpc-detail-card > header, + .admin-vpc-detail-card > footer { + align-items: flex-start; + flex-direction: column; + } + + .admin-vpc-detail-card > footer .btn { + width: 100%; + } + + .admin-vpc-detail-card dl { + grid-template-columns: minmax(0, 1fr); + } + .admin-vpc-pagination .btn { width: 100%; } diff --git a/web/public/cloud/admin-vpc.js b/web/public/cloud/admin-vpc.js index b9291442..41ad0a37 100644 --- a/web/public/cloud/admin-vpc.js +++ b/web/public/cloud/admin-vpc.js @@ -321,6 +321,156 @@ function detailField(list, label, value) { list.append(create('dt', '', label), create('dd', '', value || '未记录')); } +function detailSection(title, description) { + const section = create('section', 'admin-vpc-detail-section'); + const header = create('header'); + header.append(create('h3', '', title), create('p', '', description)); + const content = create('div', 'admin-vpc-detail-list'); + section.append(header, content); + return { section, content }; +} + +function detailState(label, tone = 'warning') { + return create('span', `admin-vpc-state is-${tone}`, label); +} + +function portCard(port) { + const labels = { + reserved: '已预留', preparing: '准备中', attached: '已连接', + detaching: '移出中', migrating: '迁移中', release_pending: '等待清理', + }; + const healthy = port.state === 'attached' + && Number(port.desired_revision) === Number(port.applied_revision) + && !port.last_error_code; + const card = create('article', 'admin-vpc-detail-card'); + const header = create('header'); + const heading = create('div'); + heading.append(create('h4', '', port.instance_name || `实例 #${port.instance_id}`), + create('p', 'admin-mono', port.instance_uuid || `ID ${port.instance_id}`)); + header.append(heading, detailState(labels[port.state] || port.state, + healthy ? 'success' : (port.last_error_code ? 'danger' : 'warning'))); + const details = create('dl'); + detailField(details, '公网 IPv4', port.public_ip); + detailField(details, '私网 IPv4', port.private_ip); + detailField(details, '所在节点', port.host_name || `节点 #${port.host_id}`); + detailField(details, '版本', `${port.applied_revision} / ${port.desired_revision}`); + if (port.last_error_code) detailField(details, '最近错误', port.last_error_code); + const footer = create('footer'); + const link = create('a', 'btn btn-light btn-sm', '查看实例'); + link.href = port.instance_url; + footer.append(link); + card.append(header, details, footer); + return card; +} + +function hostStateCard(host) { + const labels = { ready: '已收敛', pending: '等待应用', applying: '应用中', + failed: '应用失败', unknown: '效果未知' }; + const card = create('article', 'admin-vpc-detail-card'); + const header = create('header'); + const heading = create('div'); + heading.append(create('h4', '', host.host_name || `节点 #${host.host_id}`), + create('p', '', `节点 ID ${host.host_id}`)); + header.append(heading, detailState(labels[host.sync_state] || host.sync_state, + host.ready ? 'success' : (['failed', 'unknown'].includes(host.sync_state) + ? 'danger' : 'warning'))); + const details = create('dl'); + detailField(details, '期望 / 已应用', `${host.desired_revision} / ${host.applied_revision}`); + detailField(details, '摘要核对', host.digest_match ? '一致' : '不一致'); + detailField(details, '最近成功', formatTime(host.last_success_at)); + detailField(details, '最近失败', formatTime(host.last_failure_at)); + if (host.last_error_code) detailField(details, '最近错误', host.last_error_code); + const footer = create('footer'); + const link = create('a', 'btn btn-light btn-sm', '查看计算节点'); + link.href = host.host_url; + footer.append(link); + card.append(header, details, footer); + return card; +} + +function batchCard(batch) { + const labels = { routing: '应用路由', ports: '应用实例端口', cleanup: '清理中', + blocked: '需要处理', succeeded: '已完成', failed: '失败' }; + const card = create('article', 'admin-vpc-detail-card is-compact'); + const header = create('header'); + const heading = create('div'); + heading.append(create('h4', '', `${batch.kind === 'attach' ? '批量加入' : '批量移出'} · #${batch.id}`), + create('p', '', `任务 #${batch.job_id} · 目标版本 ${batch.desired_revision}`)); + const tone = batch.state === 'succeeded' ? 'success' + : (['blocked', 'failed'].includes(batch.state) ? 'danger' : 'warning'); + header.append(heading, detailState(labels[batch.state] || batch.state, tone)); + const progress = batch.items || {}; + const note = create('p', 'admin-vpc-detail-note', + progress.total ? `实例进度 ${progress.finished || 0} / ${progress.total};成功 ${progress.succeeded || 0},失败 ${progress.failed || 0},未知 ${progress.unknown || 0}` + : '该批次没有实例明细或明细已清理。'); + if (batch.last_error_code) note.append(document.createTextNode(` · ${batch.last_error_code}`)); + const footer = create('footer'); + const link = create('a', 'btn btn-light btn-sm', '查看任务'); + link.href = batch.job_url; + footer.append(link); + card.append(header, note, footer); + return card; +} + +function jobCard(job) { + const tone = job.status_label === '已完成' ? 'success' + : (job.status_label === '失败' ? 'danger' : 'warning'); + const card = create('article', 'admin-vpc-detail-card is-compact'); + const header = create('header'); + const heading = create('div'); + heading.append(create('h4', '', `${job.kind_label} · #${job.id}`), + create('p', 'admin-mono', job.operation_id)); + header.append(heading, detailState(job.status_label, tone)); + const note = create('p', 'admin-vpc-detail-note', + job.error_code ? `错误:${job.error_code}` : `最近更新:${formatTime(job.updated_at)}`); + const footer = create('footer'); + const link = create('a', 'btn btn-light btn-sm', '查看任务详情'); + link.href = job.url; + footer.append(link); + card.append(header, note, footer); + return card; +} + +function renderDetail(payload) { + const body = byId('admin-vpc-detail-body'); + const network = payload.vpc; + body.replaceChildren(); + byId('admin-vpc-detail-title').textContent = network.name; + const overview = detailSection('网络概览', '面板期望状态与当前资源归属。'); + const details = create('dl', 'admin-instance-details'); + detailField(details, '所有者', `${network.owner_name || '未记录'} · ID ${network.user_id}`); + detailField(details, '地区', network.region_name); + detailField(details, '私网网段', network.cidr); + detailField(details, '默认网关', network.gateway); + detailField(details, '目标版本', String(network.desired_revision)); + detailField(details, '运行状态', network.display?.label); + overview.content.append(details); + body.append(overview.section); + + const ports = detailSection('实例端口', '显示当前有效私网网卡;不会在此处强制加入或移出。'); + for (const port of payload.ports || []) ports.content.append(portCard(port)); + if (!payload.ports?.length) ports.content.append(create('p', 'admin-vpc-detail-empty', '当前没有有效实例端口。')); + body.append(ports.section); + + const hosts = detailSection('宿主机投影', '期望版本、已应用版本和摘要必须同时一致。'); + for (const host of payload.host_states || []) hosts.content.append(hostStateCard(host)); + if (!payload.host_states?.length) hosts.content.append(create('p', 'admin-vpc-detail-empty', '当前没有宿主机路由投影。')); + body.append(hosts.section); + + const batches = detailSection('批量变更', '批量加入或移出的持久化协调状态。'); + for (const batch of payload.batches || []) batches.content.append(batchCard(batch)); + if (!payload.batches?.length) batches.content.append(create('p', 'admin-vpc-detail-empty', '没有近期批量变更。')); + body.append(batches.section); + + const jobs = detailSection('相关任务', '恢复和人工核查必须进入原任务,禁止直接重复提交。'); + for (const job of payload.jobs || []) jobs.content.append(jobCard(job)); + if (!payload.jobs?.length) jobs.content.append(create('p', 'admin-vpc-detail-empty', '没有近期相关任务。')); + body.append(jobs.section); + const focusJob = (payload.jobs || []).find(job => job.status_label !== '已完成') + || payload.jobs?.[0]; + byId('admin-vpc-detail-operations').href = focusJob?.url || '/admin/operations'; +} + async function openDetail(id) { const dialog = byId('admin-vpc-detail'); const body = byId('admin-vpc-detail-body'); @@ -332,17 +482,7 @@ async function openDetail(id) { window.VenixDrawerMotion?.openDrawerWithMotion(dialog) || dialog.showModal(); try { const payload = await api(`/api/v1/admin/vpc-control/vpcs/${encodeURIComponent(id)}`); - const network = payload.vpc; - byId('admin-vpc-detail-title').textContent = network.name; - const details = create('dl', 'admin-instance-details'); - detailField(details, '所有者', `${network.owner_name || '未记录'} · ID ${network.user_id}`); - detailField(details, '地区', network.region_name); - detailField(details, '私网网段', network.cidr); - detailField(details, '默认网关', network.gateway); - detailField(details, '实例端口', String(network.port_count)); - detailField(details, '宿主机投影', - `${network.host_summary.ready} / ${network.host_summary.total} 已收敛`); - body.append(details); + renderDetail(payload); message.hidden = true; } catch (error) { message.className = 'alert alert-danger'; diff --git a/web/resources/views/venix/admin/cloud/vpc.tpl b/web/resources/views/venix/admin/cloud/vpc.tpl index 9a4cc78a..d54526d1 100644 --- a/web/resources/views/venix/admin/cloud/vpc.tpl +++ b/web/resources/views/venix/admin/cloud/vpc.tpl @@ -75,7 +75,7 @@ -

VPC Detail

VPC 详情

详情功能将在下一阶段启用。

+

VPC Detail

VPC 详情

正在读取详情…

Allocation Boundary

添加 VPC 地址池

同一地区地址池不能重叠。已有 VPC 占用时,CIDR 和分配前缀自动锁定,但仍可停用后续分配。

保存后显示可分配子网数量。
diff --git a/web/resources/views/venix/layout_assets.tpl b/web/resources/views/venix/layout_assets.tpl index 60a925ec..1d0bd92c 100644 --- a/web/resources/views/venix/layout_assets.tpl +++ b/web/resources/views/venix/layout_assets.tpl @@ -1,2 +1,2 @@ {* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *} -{assign var=venix_asset_version value='2026103115' scope='global'} +{assign var=venix_asset_version value='2026103116' scope='global'} diff --git a/web/tests/CloudAdminVpcControlServiceTest.php b/web/tests/CloudAdminVpcControlServiceTest.php index 3595fb63..070a2340 100644 --- a/web/tests/CloudAdminVpcControlServiceTest.php +++ b/web/tests/CloudAdminVpcControlServiceTest.php @@ -114,6 +114,15 @@ $schema->create('cloud_vpc_membership_batches', $table->integer('terminal_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); +$schema->create('cloud_vpc_membership_batch_items', + static function (Blueprint $table): void { + $table->increments('id'); $table->integer('batch_id'); + $table->integer('user_id'); $table->integer('instance_id'); + $table->integer('vpc_port_id'); $table->integer('host_id'); + $table->integer('child_job_id')->nullable(); $table->string('state'); + $table->string('last_error_code')->default(''); + $table->integer('created_at'); $table->integer('updated_at'); + }); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->string('operation_id'); $table->string('kind'); $table->string('phase'); @@ -221,6 +230,12 @@ Capsule::table('cloud_vpc_membership_batches')->insert([ 'last_error_code' => 'vpc_result_uncertain', 'terminal_at' => null, 'created_at' => 25, 'updated_at' => 30, ]); +Capsule::table('cloud_vpc_membership_batch_items')->insert([ + 'id' => 1, 'batch_id' => 1, 'user_id' => 8, 'instance_id' => 12, + 'vpc_port_id' => 2, 'host_id' => 1, 'child_job_id' => null, + 'state' => 'unknown', 'last_error_code' => 'vpc_result_uncertain', + 'created_at' => 25, 'updated_at' => 30, +]); Capsule::table('cloud_jobs')->insert([ 'id' => 91, 'operation_id' => str_repeat('6', 32), 'kind' => 'vpc_membership_batch', 'phase' => 'awaiting_receipt', @@ -272,6 +287,8 @@ $assert($detail['vpc']['name'] === 'production' 'Healthy VPC detail projection is incomplete.'); $degradedDetail = CloudAdminVpcControlService::detail($actor, 2); $assert($degradedDetail['host_states'][0]['ready'] === false + && $degradedDetail['batches'][0]['items']['unknown'] === 1 + && $degradedDetail['jobs'][0]['kind_label'] !== '' && $degradedDetail['jobs'][0]['url'] === '/admin/operations?q=91', 'VPC detail did not retain the exact task recovery navigation.'); try { diff --git a/web/tests/CloudAdminVpcUiContractTest.php b/web/tests/CloudAdminVpcUiContractTest.php index ffb7d96c..6ec926a1 100644 --- a/web/tests/CloudAdminVpcUiContractTest.php +++ b/web/tests/CloudAdminVpcUiContractTest.php @@ -60,11 +60,16 @@ $assert(str_contains($template, 'data-admin-vpc') && str_contains($template, 'id="admin-vpc-network-body"') && str_contains($template, 'id="admin-vpc-pool-drawer"') && str_contains($template, 'id="admin-vpc-pool-form"') + && str_contains($template, 'id="admin-vpc-detail-operations"') && str_contains($template, '/cloud/admin-vpc.css?v={$venix_asset_version}') && str_contains($template, '/cloud/admin-vpc.js?v={$venix_asset_version}') && str_contains($javascript, 'globalThis.crypto?.getRandomValues') && str_contains($javascript, 'pool.address_locked') && str_contains($javascript, 'openPoolDrawer') + && str_contains($javascript, 'renderDetail') + && str_contains($javascript, 'hostStateCard') + && str_contains($javascript, 'batchCard') + && str_contains($javascript, 'job.url') && str_contains($javascript, "api('/api/v1/admin/vpc-control')") && str_contains($javascript, "api(`/api/v1/admin/vpc-control/vpcs") @@ -81,7 +86,7 @@ $assert(str_contains($siteSettings, 'id="cloud_vpc_enabled"') 'The VPC feature switch is not editable in site settings or the administrator page still reports a hard-coded state.'); $assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets, $version) === 1 - && (int) $version[1] >= 2026103115, + && (int) $version[1] >= 2026103116, 'Administrator VPC browser assets were not cache-busted.'); echo "Administrator VPC CIDR pool page and API contracts passed.\n";