From df554680a5471be58f05034db58ec2ff34c6e552 Mon Sep 17 00:00:00 2001 From: codex Date: Sat, 31 Oct 2026 18:57:34 +0800 Subject: [PATCH] feat(admin): connect resource summaries to exact filters --- .../Cloud/CloudAdminResourceService.php | 31 ++++++++++++++++++- web/public/cloud/admin-resources.js | 22 +++++++++++-- .../views/venix/admin/cloud/resources.tpl | 30 ++++++++++++------ web/resources/views/venix/layout_assets.tpl | 2 +- .../AdminResourceProjectionContractTest.php | 14 +++++++++ 5 files changed, 85 insertions(+), 14 deletions(-) diff --git a/web/app/Services/Cloud/CloudAdminResourceService.php b/web/app/Services/Cloud/CloudAdminResourceService.php index 0b2660c8..c1453f38 100644 --- a/web/app/Services/Cloud/CloudAdminResourceService.php +++ b/web/app/Services/Cloud/CloudAdminResourceService.php @@ -30,12 +30,17 @@ final class CloudAdminResourceService $view = is_string($requested) && in_array($requested, self::VIEWS, true) ? $requested : 'topology'; $catalog = self::catalog(); + $focus = is_string($query['focus'] ?? null) ? $query['focus'] : 'all'; + if (!in_array($focus, ['all', 'ready_pools', 'hosts', 'ready_hosts', 'cpu', 'ip'], true)) { + $focus = 'all'; + } $reservationPage = $view === 'reservations' ? self::reservations($query) : self::emptyReservationPage(); - return array_merge($catalog, [ + return array_merge($catalog, self::capacitySelection($catalog, $focus), [ 'view' => $view, + 'focus' => $focus, 'line_feature_enabled' => CloudNetworkLineFeature::enabled(), 'country_options' => CloudCountryCatalog::options(), 'reservations' => $reservationPage, @@ -48,6 +53,21 @@ final class CloudAdminResourceService return ['network_lines_enabled' => CloudNetworkLineFeature::enabled()]; } + /** Select from the exact projection used to compute the summary cards. */ + private static function capacitySelection(array $catalog, string $focus): array + { + return [ + 'capacity_pools' => array_values(array_filter($catalog['pools'], + static fn (array $pool): bool => match ($focus) { + 'ready_pools' => $pool['effective_state'] === 'ready', + 'cpu', 'ip' => (int) $pool['placement_capacity'][$focus]['free'] > 0, + default => true, + })), + 'capacity_hosts' => array_values(array_filter($catalog['hosts'], + static fn (array $host): bool => $focus !== 'ready_hosts' || $host['placement_ready'])), + ]; + } + public static function saveSettings(array $body): array { $enabled = filter_var($body['network_lines_enabled'] ?? null, @@ -597,6 +617,7 @@ final class CloudAdminResourceService } } $poolRows = []; + $hostRows = []; foreach ($pools as $pool) { $poolId = (int) $pool->id; $line = $lineById[(int) $pool->line_id] ?? null; @@ -703,6 +724,13 @@ final class CloudAdminResourceService if ($hostBaseReady && $capacityReady) { $health['placement_ready_host_count']++; } + $hostRows[] = [ + 'id' => $hostId, + 'name' => (string) $host->name, + 'pool_name' => (string) $pool->name, + 'region_name' => (string) ($region->name ?? ''), + 'placement_ready' => $hostBaseReady && $capacityReady, + ]; } $held = 0; $uncertain = 0; @@ -841,6 +869,7 @@ final class CloudAdminResourceService } return [ + 'hosts' => $hostRows, 'summary' => self::summary($regionRows, $lineRows, $poolRows), 'topology' => self::topology( $regionRows, $lineRows, $poolRows, $lineFeatureEnabled diff --git a/web/public/cloud/admin-resources.js b/web/public/cloud/admin-resources.js index 4954a345..c40bdfb2 100644 --- a/web/public/cloud/admin-resources.js +++ b/web/public/cloud/admin-resources.js @@ -304,13 +304,29 @@ function applyResourceFilters(form) { if (!row.hidden) visibleRows++; } const tableEmpty = root?.querySelector('tr[data-resource-filter-empty]'); - if (tableEmpty) tableEmpty.hidden = visibleRows !== 0; + if (tableEmpty) tableEmpty.hidden = visibleRows !== 0 + || Boolean(root?.querySelector('[data-resource-empty-row]')); } for (const form of root?.querySelectorAll('[data-resource-local-filter]') || []) { + const params = new URLSearchParams(window.location.search); + const search = form.querySelector('[data-resource-search]'); + const issues = form.querySelector('[data-resource-only-issues]'); + if (search) search.value = params.get('resource_q') || ''; + if (issues) issues.checked = params.get('resource_issues') === '1'; + const changed = () => { + applyResourceFilters(form); + const url = new URL(window.location.href); + if (search?.value.trim()) url.searchParams.set('resource_q', search.value.trim()); + else url.searchParams.delete('resource_q'); + if (issues?.checked) url.searchParams.set('resource_issues', '1'); + else url.searchParams.delete('resource_issues'); + window.history.replaceState(window.history.state, '', url); + }; form.addEventListener('submit', event => event.preventDefault()); - form.addEventListener('input', () => applyResourceFilters(form)); - form.addEventListener('change', () => applyResourceFilters(form)); + form.addEventListener('input', changed); + form.addEventListener('change', changed); + applyResourceFilters(form); } function showReservation(id, opener) { diff --git a/web/resources/views/venix/admin/cloud/resources.tpl b/web/resources/views/venix/admin/cloud/resources.tpl index e16d121a..7cb354c8 100644 --- a/web/resources/views/venix/admin/cloud/resources.tpl +++ b/web/resources/views/venix/admin/cloud/resources.tpl @@ -11,6 +11,7 @@ {if !$resource_page} {assign var=resource_page value=['view'=>'topology','line_feature_enabled'=>false,'summary'=>['regions_total'=>0,'lines_total'=>0,'pools_total'=>0,'placement_ready_pools'=>0,'hosts_total'=>0,'placement_ready_hosts'=>0,'held_reservations'=>0,'uncertain_reservations'=>0],'country_options'=>[],'topology'=>[],'regions'=>[],'lines'=>[],'pools'=>[],'reservations'=>['items'=>[],'filters'=>['state'=>'','pool_id'=>'','q'=>''],'pagination'=>['page'=>1,'per_page'=>20,'total'=>0,'total_pages'=>1,'from'=>0,'to'=>0]] ]} {/if} +{if !$resource_page.focus}{assign var=resource_focus value='all'}{else}{assign var=resource_focus value=$resource_page.focus}{/if}
@@ -30,10 +31,10 @@ {if $resource_page.view == 'topology'}
-

地区

{$resource_page.summary.regions_total|default:0|escape}

{if $resource_page.line_feature_enabled}{$resource_page.summary.lines_total|default:0|escape} 条线路{else}使用系统默认线路{/if}

-

基础就绪资源池

{$resource_page.summary.placement_ready_pools|default:0|escape} / {$resource_page.summary.pools_total|default:0|escape}

套餐与镜像尚需下单校验

-

基础就绪节点

{$resource_page.summary.placement_ready_hosts|default:0|escape} / {$resource_page.summary.hosts_total|default:0|escape}

Agent、存储、网络和容量均就绪

-

异常保留

{$resource_page.summary.uncertain_reservations|default:0|escape}

占用中 {$resource_page.summary.held_reservations|default:0|escape} 条

+ + + +
@@ -92,17 +93,27 @@
{elseif $resource_page.view == 'capacity'}
-

计算资源池

{$resource_page.summary.placement_ready_pools|default:0|escape} / {$resource_page.summary.pools_total|default:0|escape}

基础就绪 / 总数

-

计算节点

{$resource_page.summary.placement_ready_hosts|default:0|escape} / {$resource_page.summary.hosts_total|default:0|escape}

基础就绪 / 总数

-

CPU 可用

{$resource_page.summary.placement_capacity.cpu.free|default:0|escape}

共 {$resource_page.summary.capacity.cpu.configured|default:0|escape} 核已配置

-

公网 IP 可用

{$resource_page.summary.placement_capacity.ip.free|default:0|escape}

共 {$resource_page.summary.capacity.ip.total|default:0|escape} 个已登记

+ + + +
+
+ 当前显示:{if $resource_focus == 'ready_pools'}基础就绪资源池{elseif $resource_focus == 'ready_hosts'}基础就绪节点{elseif $resource_focus == 'hosts'}全部计算节点{elseif $resource_focus == 'cpu'}有可用 CPU 的资源池{elseif $resource_focus == 'ip'}有可用公网 IP 的资源池{else}全部资源池{/if} + +

容量与健康

基础就绪只表示 Agent、存储、网络和容量已通过;具体套餐与镜像在下单时继续校验。
配置计算节点
+ {if $resource_focus == 'hosts' || $resource_focus == 'ready_hosts'} +
+ {foreach $resource_page.capacity_hosts as $host}{foreachelse}{/foreach} + +
计算节点基础条件
计算节点地区与资源池基础条件
{$host.name|escape}ID {$host.id|escape}{$host.region_name|escape} · {$host.pool_name|escape}{if $host.placement_ready}基础就绪{else}未就绪{/if}
没有符合条件的计算节点。
+ {else}
- {foreach $resource_page.pools as $pool} + {foreach $resource_page.capacity_pools as $pool} @@ -113,6 +124,7 @@ {foreachelse}{/foreach}
计算资源池容量与健康
位置与资源池基础就绪节点CPU内存磁盘公网 IP健康状态
{$pool.name|escape}{$pool.region_name|escape}{if $resource_page.line_feature_enabled} · {$pool.line_name|escape}{/if} {$pool.health.placement_ready_host_count|default:0|escape} / {$pool.health.host_count|default:0|escape}Agent {$pool.health.agent_ready_host_count|default:0|escape} · 存储 {$pool.health.storage_ready_host_count|default:0|escape} · 网络 {$pool.health.network_ready_host_count|default:0|escape} {$pool.placement_capacity.cpu.free|default:0|escape} 核配置 {$pool.capacity.cpu.configured|default:0|escape} · 可分配 {$pool.capacity.cpu.allocatable|default:0|escape}
尚未建立计算资源池。
+ {/if}
{else} diff --git a/web/resources/views/venix/layout_assets.tpl b/web/resources/views/venix/layout_assets.tpl index 2fbd075d..4e2a5c24 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='2026103129' scope='global'} +{assign var=venix_asset_version value='2026103130' scope='global'} diff --git a/web/tests/AdminResourceProjectionContractTest.php b/web/tests/AdminResourceProjectionContractTest.php index 18b1193b..0b86de98 100644 --- a/web/tests/AdminResourceProjectionContractTest.php +++ b/web/tests/AdminResourceProjectionContractTest.php @@ -73,6 +73,20 @@ $assert(str_contains($source, '$isNew = !$region->exists;') 'Hidden-line mode must serialize on the feature row, then atomically create or backfill an active default line for every region.'); $reflection = new ReflectionClass(CloudAdminResourceService::class); +$selection = $reflection->getMethod('capacitySelection'); +$catalog = ['pools' => [ + ['id' => 1, 'effective_state' => 'ready', + 'placement_capacity' => ['cpu' => ['free' => 4], 'ip' => ['free' => 0]]], + ['id' => 2, 'effective_state' => 'blocked', + 'placement_capacity' => ['cpu' => ['free' => 0], 'ip' => ['free' => 2]]], +], 'hosts' => [['id' => 10, 'placement_ready' => true], + ['id' => 11, 'placement_ready' => false]]]; +$assert(array_column($selection->invoke(null, $catalog, 'ready_pools')['capacity_pools'], 'id') === [1] + && array_column($selection->invoke(null, $catalog, 'ready_hosts')['capacity_hosts'], 'id') === [10] + && count($selection->invoke(null, $catalog, 'hosts')['capacity_hosts']) === 2 + && array_column($selection->invoke(null, $catalog, 'cpu')['capacity_pools'], 'id') === [1] + && array_column($selection->invoke(null, $catalog, 'ip')['capacity_pools'], 'id') === [2], + 'Summary shortcuts must use placement readiness and usable capacity, not generic online status.'); $entityCode = $reflection->getMethod('entityCode'); $newModel = (object) ['exists' => false, 'code' => '']; $generated = $entityCode->invoke(null, [], $newModel, 'region', '东京', 32,