feat(traffic): add administrator pool console
parent
3beb24fff8
commit
aae35410b8
@ -0,0 +1,207 @@
|
||||
const root = document.querySelector('[data-admin-resources][data-resource-view="traffic"]');
|
||||
const bootstrapNode = document.getElementById('admin-resources-bootstrap');
|
||||
let page = null;
|
||||
try { page = JSON.parse(bootstrapNode?.textContent || 'null'); } catch { page = null; }
|
||||
|
||||
if (root) {
|
||||
const requestRuntime = window.VenixAdminRequestRuntime.create();
|
||||
const drawerState = { dialog: null, opener: null, scrollY: 0 };
|
||||
|
||||
function byId(id) { return document.getElementById(id); }
|
||||
function group(id) {
|
||||
return (page?.traffic_pools?.items || []).find(item => String(item.id) === String(id)) || null;
|
||||
}
|
||||
function notice(id, message, tone = '') {
|
||||
const node = byId(id);
|
||||
if (!node) return;
|
||||
node.textContent = message;
|
||||
node.className = `admin-config-status mt-3 ${tone ? `is-${tone}` : ''}`;
|
||||
node.setAttribute('role', tone === 'error' ? 'alert' : 'status');
|
||||
}
|
||||
function pageNotice(message, error = false) {
|
||||
const node = byId('resource-notice');
|
||||
if (!node) return;
|
||||
node.textContent = message;
|
||||
node.className = `admin-config-status ${error ? 'is-error' : 'is-success'}`;
|
||||
node.setAttribute('role', error ? 'alert' : 'status');
|
||||
}
|
||||
async function mutate(path, options) {
|
||||
return requestRuntime.mutate(path, {
|
||||
method: options.method,
|
||||
body: options.body,
|
||||
timeoutMs: 20000,
|
||||
response: 'json',
|
||||
});
|
||||
}
|
||||
function openDrawer(dialog, opener) {
|
||||
if (!dialog) return;
|
||||
drawerState.dialog = dialog;
|
||||
drawerState.opener = opener || document.activeElement;
|
||||
drawerState.scrollY = window.scrollY;
|
||||
document.documentElement.style.setProperty('--admin-instance-scroll-top', `-${drawerState.scrollY}px`);
|
||||
document.documentElement.classList.add('admin-instance-detail-open');
|
||||
window.VenixDrawerMotion?.openDrawerWithMotion(dialog) || dialog.showModal();
|
||||
}
|
||||
async function closeDrawer(dialog) {
|
||||
if (!dialog?.open) return;
|
||||
if (window.VenixDrawerMotion) await window.VenixDrawerMotion.closeDrawerWithMotion(dialog);
|
||||
else dialog.close();
|
||||
document.documentElement.classList.remove('admin-instance-detail-open');
|
||||
document.documentElement.style.removeProperty('--admin-instance-scroll-top');
|
||||
window.scrollTo({ top: drawerState.scrollY, left: 0, behavior: 'instant' });
|
||||
const opener = drawerState.opener;
|
||||
drawerState.dialog = null;
|
||||
drawerState.opener = null;
|
||||
if (opener instanceof HTMLElement && opener.isConnected) opener.focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
for (const dialog of document.querySelectorAll('[data-admin-traffic-drawer]')) {
|
||||
dialog.addEventListener('cancel', event => { event.preventDefault(); void closeDrawer(dialog); });
|
||||
dialog.addEventListener('click', event => { if (event.target === dialog) void closeDrawer(dialog); });
|
||||
}
|
||||
byId('traffic-group-editor-close')?.addEventListener('click', () => void closeDrawer(byId('traffic-group-editor')));
|
||||
byId('traffic-line-editor-close')?.addEventListener('click', () => void closeDrawer(byId('traffic-line-editor')));
|
||||
|
||||
function configureGroupEditor(item = null) {
|
||||
const form = byId('traffic-group-editor-form');
|
||||
if (!form) return false;
|
||||
form.reset();
|
||||
form.elements.namedItem('group_id').value = item?.id || '';
|
||||
form.elements.namedItem('expected_revision').value = item?.revision || '';
|
||||
form.elements.namedItem('name').value = item?.name || '';
|
||||
const codeField = form.querySelector('[data-traffic-code-field]');
|
||||
const codeInput = form.elements.namedItem('code');
|
||||
codeField.hidden = Boolean(item);
|
||||
codeInput.disabled = Boolean(item);
|
||||
codeInput.required = !item;
|
||||
byId('traffic-group-editor-title').textContent = item ? '修改分组名称' : '新建流量池分组';
|
||||
byId('traffic-group-editor-eyebrow').textContent = item ? `${item.name} · r${item.revision}` : '共享流量池';
|
||||
notice('traffic-group-editor-notice', '等待提交');
|
||||
return true;
|
||||
}
|
||||
|
||||
function configureLineEditor(item) {
|
||||
const form = byId('traffic-line-editor-form');
|
||||
if (!form || !item) return false;
|
||||
form.reset();
|
||||
form.elements.namedItem('group_id').value = item.id;
|
||||
form.elements.namedItem('expected_revision').value = item.revision;
|
||||
byId('traffic-line-editor-title').textContent = `配置 ${item.name} 的线路`;
|
||||
for (const checkbox of form.querySelectorAll('input[name="line_ids[]"]')) {
|
||||
const owner = Number(checkbox.dataset.lineOwner || 0);
|
||||
checkbox.checked = owner === Number(item.id);
|
||||
checkbox.disabled = owner > 0 && owner !== Number(item.id);
|
||||
checkbox.closest('label')?.classList.toggle('is-unavailable', checkbox.disabled);
|
||||
}
|
||||
notice('traffic-line-editor-notice', '等待提交');
|
||||
return true;
|
||||
}
|
||||
|
||||
root.addEventListener('click', async event => {
|
||||
const create = event.target.closest('[data-traffic-create]');
|
||||
if (create && configureGroupEditor()) {
|
||||
openDrawer(byId('traffic-group-editor'), create);
|
||||
return;
|
||||
}
|
||||
const rename = event.target.closest('[data-traffic-rename]');
|
||||
if (rename) {
|
||||
const item = group(rename.dataset.trafficRename);
|
||||
if (!item) return pageNotice('找不到流量池分组,请刷新页面。', true);
|
||||
if (configureGroupEditor(item)) openDrawer(byId('traffic-group-editor'), rename);
|
||||
return;
|
||||
}
|
||||
const lines = event.target.closest('[data-traffic-lines]');
|
||||
if (lines) {
|
||||
const item = group(lines.dataset.trafficLines);
|
||||
if (!item) return pageNotice('找不到流量池分组,请刷新页面。', true);
|
||||
if (configureLineEditor(item)) openDrawer(byId('traffic-line-editor'), lines);
|
||||
return;
|
||||
}
|
||||
const modeButton = event.target.closest('[data-traffic-mode]');
|
||||
if (!modeButton || modeButton.disabled) return;
|
||||
const item = group(modeButton.dataset.trafficMode);
|
||||
const targetMode = modeButton.dataset.targetMode;
|
||||
if (!item || !['disabled', 'shadow', 'enforced'].includes(targetMode)) {
|
||||
return pageNotice('流量池状态操作无效,请刷新页面。', true);
|
||||
}
|
||||
const config = targetMode === 'enforced' ? {
|
||||
tone: 'danger',
|
||||
title: '开始强制执行共享流量池',
|
||||
message: '启用后会按照共享余额真实限制实例网络,且不能回退、停用或调整线路。请确认就绪检查与业务验收均已完成。',
|
||||
confirmText: '开始强制执行',
|
||||
requiredText: item.name,
|
||||
requiredLabel: '输入完整分组名称以确认',
|
||||
} : (targetMode === 'shadow' ? {
|
||||
tone: 'warning',
|
||||
title: '进入影子核算',
|
||||
message: '系统将开始建立真实账本投影,但暂不因共享额度限制实例网络。',
|
||||
confirmText: '进入影子核算',
|
||||
} : {
|
||||
tone: 'warning',
|
||||
title: '停用影子核算',
|
||||
message: '仅在尚未形成持久服务周期或账本引用时允许停用。',
|
||||
confirmText: '确认停用',
|
||||
});
|
||||
if (!await window.VenixAdminDangerDialog.confirm(config)) return;
|
||||
modeButton.disabled = true;
|
||||
pageNotice('正在更新流量池模式…');
|
||||
try {
|
||||
await mutate(`/api/v1/admin/traffic-pool-groups/${item.id}`, {
|
||||
method: 'PATCH',
|
||||
body: { mode: targetMode, expected_revision: Number(item.revision) },
|
||||
});
|
||||
pageNotice('流量池模式已更新,正在刷新…');
|
||||
location.reload();
|
||||
} catch (error) {
|
||||
pageNotice(error.message || '流量池模式更新失败,请刷新后核对。', true);
|
||||
modeButton.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
byId('traffic-group-editor-form')?.addEventListener('submit', async event => {
|
||||
event.preventDefault();
|
||||
const form = event.currentTarget;
|
||||
if (!form.reportValidity()) return;
|
||||
const groupId = form.elements.namedItem('group_id').value;
|
||||
const submit = byId('traffic-group-editor-submit');
|
||||
const body = { name: form.elements.namedItem('name').value.trim() };
|
||||
if (groupId) body.expected_revision = Number(form.elements.namedItem('expected_revision').value);
|
||||
else body.code = form.elements.namedItem('code').value.trim().toLowerCase();
|
||||
submit.disabled = true;
|
||||
notice('traffic-group-editor-notice', '正在保存…', 'loading');
|
||||
try {
|
||||
await mutate(`/api/v1/admin/traffic-pool-groups${groupId ? `/${groupId}` : ''}`, {
|
||||
method: groupId ? 'PATCH' : 'POST', body,
|
||||
});
|
||||
notice('traffic-group-editor-notice', '已保存,正在刷新…', 'success');
|
||||
location.reload();
|
||||
} catch (error) {
|
||||
notice('traffic-group-editor-notice', error.message || '保存失败,请刷新后重试。', 'error');
|
||||
submit.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
byId('traffic-line-editor-form')?.addEventListener('submit', async event => {
|
||||
event.preventDefault();
|
||||
const form = event.currentTarget;
|
||||
const groupId = Number(form.elements.namedItem('group_id').value);
|
||||
const item = group(groupId);
|
||||
if (!item) return notice('traffic-line-editor-notice', '分组版本已失效,请刷新页面。', 'error');
|
||||
const lineIds = Array.from(form.querySelectorAll('input[name="line_ids[]"]:checked:not(:disabled)'),
|
||||
checkbox => Number(checkbox.value));
|
||||
const submit = byId('traffic-line-editor-submit');
|
||||
submit.disabled = true;
|
||||
notice('traffic-line-editor-notice', '正在保存线路…', 'loading');
|
||||
try {
|
||||
await mutate(`/api/v1/admin/traffic-pool-groups/${groupId}/lines`, {
|
||||
method: 'PUT',
|
||||
body: { line_ids: lineIds, expected_revision: Number(item.revision) },
|
||||
});
|
||||
notice('traffic-line-editor-notice', '线路已保存,正在刷新…', 'success');
|
||||
location.reload();
|
||||
} catch (error) {
|
||||
notice('traffic-line-editor-notice', error.message || '线路保存失败,请刷新后重试。', 'error');
|
||||
submit.disabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026103135' scope='global'}
|
||||
{assign var=venix_asset_version value='2026110101' scope='global'}
|
||||
|
||||
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$read = static function (string $path): string {
|
||||
$source = file_get_contents($path);
|
||||
if (!is_string($source)) {
|
||||
throw new RuntimeException('Unable to read ' . $path);
|
||||
}
|
||||
return $source;
|
||||
};
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$resourceService = $read(
|
||||
$root . '/app/Services/Cloud/CloudAdminResourceService.php'
|
||||
);
|
||||
$trafficService = $read(
|
||||
$root . '/app/Services/Cloud/CloudAdminTrafficPoolService.php'
|
||||
);
|
||||
$template = $read(
|
||||
$root . '/resources/views/venix/admin/cloud/resources.tpl'
|
||||
);
|
||||
$script = $read($root . '/public/cloud/admin-traffic-pools.js');
|
||||
$styles = $read($root . '/public/theme/venix/css/admin-home.css');
|
||||
$assets = $read($root . '/resources/views/venix/layout_assets.tpl');
|
||||
|
||||
$assert(str_contains($resourceService,
|
||||
"private const VIEWS = ['topology', 'capacity', 'reservations', 'traffic']")
|
||||
&& str_contains($resourceService,
|
||||
"'traffic_pools' => CloudAdminTrafficPoolService::page(\$query)")
|
||||
&& str_contains($template, 'href="/admin/resources?view=traffic"')
|
||||
&& str_contains($template,
|
||||
'<script id="admin-resources-bootstrap" type="application/json">')
|
||||
&& str_contains($template,
|
||||
'/cloud/admin-traffic-pools.js?v={$venix_asset_version}'),
|
||||
'Traffic-pool administration must have a database-owned SSR first paint inside resources.');
|
||||
|
||||
$assert(str_contains($trafficService, 'private const BLOCKER_LABELS = [')
|
||||
&& str_contains($trafficService, "'label' => self::BLOCKER_LABELS[\$code]")
|
||||
&& str_contains($template, '{$blocker.label|escape}')
|
||||
&& str_contains($template, '{$blocker.code|escape}'),
|
||||
'Readiness blockers must have human labels while preserving technical codes.');
|
||||
|
||||
$assert(str_contains($trafficService, "use App\\Models\\CloudRegion;")
|
||||
&& str_contains($trafficService, "'region_name' =>")
|
||||
&& str_contains($template,
|
||||
'{$line.region_name|escape} · {$line.name|escape}')
|
||||
&& str_contains($template, '强制执行状态存在'),
|
||||
'Line choices must disambiguate duplicate regional codes and enforced faults must be described as incidents.');
|
||||
|
||||
$assert(str_contains($template, '{if $admin_can_manage|default:false}<button')
|
||||
&& str_contains($template, 'data-traffic-create')
|
||||
&& str_contains($template, 'data-traffic-rename=')
|
||||
&& str_contains($template, 'data-traffic-lines=')
|
||||
&& str_contains($template, 'data-target-mode="enforced"')
|
||||
&& !str_contains($template, 'data-traffic-delete'),
|
||||
'Only authorized administrators may receive traffic-pool mutation controls and no delete control may exist.');
|
||||
|
||||
$assert(str_contains($script,
|
||||
"window.VenixAdminRequestRuntime.create()")
|
||||
&& str_contains($script,
|
||||
'`/api/v1/admin/traffic-pool-groups${groupId ?')
|
||||
&& str_contains($script,
|
||||
'`/api/v1/admin/traffic-pool-groups/${groupId}/lines`')
|
||||
&& str_contains($script, "method: groupId ? 'PATCH' : 'POST'")
|
||||
&& str_contains($script, "method: 'PUT'")
|
||||
&& str_contains($script, 'expected_revision: Number(item.revision)')
|
||||
&& !str_contains($script, "method: 'DELETE'")
|
||||
&& !str_contains($script, 'setInterval('),
|
||||
'Mutations must use the shared runtime, exact revisions, and no replay or delete path.');
|
||||
|
||||
$assert(str_contains($script,
|
||||
"body: { mode: targetMode, expected_revision: Number(item.revision) }")
|
||||
&& str_contains($script, "targetMode === 'enforced'")
|
||||
&& str_contains($script, 'requiredText: item.name')
|
||||
&& str_contains($script, 'window.VenixAdminDangerDialog.confirm(config)')
|
||||
&& str_contains($script, '不能回退、停用或调整线路'),
|
||||
'Enforced rollout must require the shared typed danger confirmation and an exact revision.');
|
||||
|
||||
$assert(str_contains($script,
|
||||
'checkbox.disabled = owner > 0 && owner !== Number(item.id)')
|
||||
&& str_contains($template, 'data-line-owner=')
|
||||
&& str_contains($template, '只有停用且尚未形成持久账本引用的分组可以调整线路'),
|
||||
'Line assignment must visibly reject lines owned by a different group.');
|
||||
|
||||
$assert(str_contains($styles,
|
||||
'grid-template-columns: repeat(4, minmax(0, 1fr));')
|
||||
&& str_contains($styles, '.admin-traffic-group-list')
|
||||
&& str_contains($styles, '.admin-traffic-metrics')
|
||||
&& str_contains($styles, '[data-admin-traffic-drawer]')
|
||||
&& str_contains($styles, '@media (max-width: 767.98px)')
|
||||
&& preg_match("/venix_asset_version value='20261101[0-9]+'/", $assets) === 1,
|
||||
'Traffic-pool administration must remain responsive and use a new shared asset version.');
|
||||
|
||||
echo "Administrator shared traffic-pool UI contracts passed.\n";
|
||||
Loading…
Reference in New Issue