Files
pikasTech-unidesk/scripts/src/hwlab-node-web-sentinel-dashboard-assets.ts
T

250 lines
9.8 KiB
TypeScript

// SPEC: PJ2026-01060508 Web哨兵 draft-2026-06-26-p8-web-probe-sentinel-recovery.
// Responsibility: Static dashboard shell and asset serving for the web-probe sentinel frontend.
import { readFileSync } from "node:fs";
import { rootPath } from "./config";
interface DashboardShellConfig {
readonly node: string;
readonly lane: string;
readonly plan: { readonly ok: boolean };
readonly publicExposure: Record<string, unknown>;
}
const DASHBOARD_ASSET_ROOT = "scripts/assets/web-probe-sentinel-dashboard";
const DASHBOARD_CONTRACT_VERSION = "draft-2026-06-26-p8-web-probe-sentinel-recovery";
export function renderWebProbeSentinelDashboardHtml(config: DashboardShellConfig): string {
const publicOrigin = stringOrNull(config.publicExposure.publicBaseUrl) ?? "";
return `<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>HWLAB Web哨兵</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
</head>
<body>
<main
id="sentinel-dashboard"
class="sentinel-shell"
data-node="${escapeAttr(config.node)}"
data-lane="${escapeAttr(config.lane)}"
data-public-origin="${escapeAttr(publicOrigin)}"
data-config-ready="${config.plan.ok ? "true" : "false"}"
data-contract-version="${DASHBOARD_CONTRACT_VERSION}"
>
<section class="sentinel-topbar" aria-label="Web哨兵概览">
<div class="sentinel-title">
<div class="sentinel-mark" aria-hidden="true"></div>
<div>
<h1>HWLAB Web哨兵</h1>
<p id="sentinel-subtitle">${escapeHtml(config.node)} / ${escapeHtml(config.lane)}</p>
</div>
</div>
<div class="sentinel-toolbar" aria-label="监控面板控制">
<span id="status-pill" class="status-pill status-idle">空闲</span>
<label class="refresh-toggle">
<input id="auto-refresh-enabled" type="checkbox">
<span>自动刷新</span>
</label>
<select id="auto-refresh-interval" aria-label="自动刷新间隔">
<option value="5">5s</option>
<option value="10">10s</option>
<option value="30">30s</option>
</select>
<button id="manual-refresh" class="icon-button" type="button" title="刷新" aria-label="刷新">
<span aria-hidden="true">刷新</span>
</button>
</div>
</section>
<section id="loading-banner" class="banner banner-muted">加载中</section>
<section id="error-banner" class="banner banner-danger" hidden></section>
<section class="metric-grid" aria-label="哨兵状态指标">
<article class="metric-card">
<span class="metric-label">当前状态</span>
<strong id="metric-overall">-</strong>
<small id="metric-origin">-</small>
</article>
<article class="metric-card">
<span class="metric-label">最近运行</span>
<strong id="metric-latest-run">-</strong>
<small id="metric-latest-age">-</small>
</article>
<article class="metric-card">
<span class="metric-label">历史发现项</span>
<strong id="metric-findings">0</strong>
<small id="metric-findings-note">-</small>
</article>
<article class="metric-card">
<span class="metric-label">调度器</span>
<strong id="metric-scheduler">-</strong>
<small id="metric-budget">-</small>
</article>
</section>
<section class="overview-checks" aria-label="哨兵健康检查">
<span id="check-config" class="check-chip">config -</span>
<span id="check-pvc" class="check-chip">pvc -</span>
<span id="check-analyzer" class="check-chip">analyzer -</span>
<span id="check-public" class="check-chip">public -</span>
<span id="check-maintenance" class="check-chip">maintenance -</span>
</section>
<section class="panel timeline-panel" aria-labelledby="timeline-heading">
<div class="panel-header">
<h2 id="timeline-heading">运行时间线</h2>
<span id="timeline-count" class="panel-subtitle">-</span>
</div>
<div id="run-timeline" class="run-timeline"></div>
</section>
<section class="dashboard-grid">
<section class="panel panel-wide" aria-labelledby="runs-heading">
<div class="panel-header">
<h2 id="runs-heading">运行历史</h2>
<span id="runs-count" class="panel-subtitle">-</span>
</div>
<form id="runs-filter" class="runs-filter">
<label>
<span>状态</span>
<select id="filter-status" name="status">
<option value="">全部</option>
<option value="planned">已计划</option>
<option value="running">运行中</option>
<option value="analyzed">已分析</option>
<option value="blocked">阻塞</option>
<option value="interrupted">已中断</option>
</select>
</label>
<label>
<span>严重级别</span>
<select id="filter-severity" name="severity">
<option value="">全部</option>
<option value="red">红色</option>
<option value="warning">警告</option>
<option value="info">信息</option>
</select>
</label>
<label>
<span>时间</span>
<select id="filter-window" name="window">
<option value="">全部</option>
<option value="1h">1h</option>
<option value="6h">6h</option>
<option value="24h">24h</option>
<option value="7d">7d</option>
</select>
</label>
<label>
<span>排序</span>
<select id="filter-sort" name="sort">
<option value="updated">按更新时间</option>
<option value="created">按创建时间</option>
<option value="findings">按发现数量</option>
<option value="severity">按严重级别</option>
</select>
</label>
<label class="filter-search">
<span>搜索</span>
<input id="filter-search" name="search" type="search" placeholder="run、observer、report">
</label>
<button id="clear-filters" class="icon-button" type="button">清除</button>
</form>
<div class="table-frame">
<table class="runs-table">
<thead>
<tr>
<th>运行</th>
<th>状态</th>
<th>场景</th>
<th>发现项</th>
<th>更新时间</th>
</tr>
</thead>
<tbody id="runs-body"></tbody>
</table>
</div>
</section>
<section class="panel" aria-labelledby="findings-heading">
<div class="panel-header">
<h2 id="findings-heading">发现分析</h2>
<span id="findings-count" class="panel-subtitle">-</span>
</div>
<form id="findings-filter" class="findings-filter">
<label>
<span>严重级别</span>
<select id="finding-filter-severity" name="fseverity">
<option value="">全部</option>
<option value="red">红色</option>
<option value="warning">警告</option>
<option value="info">信息</option>
</select>
</label>
<label>
<span>窗口</span>
<select id="finding-filter-window" name="fwindow">
<option value="24h">24h</option>
<option value="1h">1h</option>
<option value="6h">6h</option>
<option value="7d">7d</option>
<option value="">全部</option>
</select>
</label>
<label>
<span>代码</span>
<input id="finding-filter-code" name="fcode" type="search" placeholder="finding code">
</label>
<label>
<span>场景</span>
<input id="finding-filter-scenario" name="fscenario" type="search" placeholder="scenario">
</label>
<button id="finding-clear-filters" class="icon-button" type="button">清除</button>
</form>
<div id="finding-aggregation" class="finding-aggregation"></div>
<div id="findings-list" class="finding-list"></div>
</section>
</section>
<section class="panel detail-panel" aria-labelledby="detail-heading" role="dialog" aria-modal="false">
<div class="panel-header">
<h2 id="detail-heading">运行详情</h2>
<span id="detail-subtitle" class="panel-subtitle">未选择运行</span>
</div>
<div id="detail-content" class="detail-content"></div>
</section>
</main>
<script type="module" src="/dashboard/assets/dashboard.js"></script>
</body>
</html>`;
}
export function webProbeSentinelDashboardAssetResponse(pathname: string): Response | null {
if (pathname === "/dashboard/assets/dashboard.css") return textAsset(`${DASHBOARD_ASSET_ROOT}/dashboard.css`, "text/css; charset=utf-8");
if (pathname === "/dashboard/assets/dashboard.js") return textAsset(`${DASHBOARD_ASSET_ROOT}/dashboard.js`, "application/javascript; charset=utf-8");
return null;
}
function textAsset(path: string, contentType: string): Response {
return new Response(readFileSync(rootPath(path), "utf8"), {
headers: {
"cache-control": "no-store",
"content-type": contentType,
},
});
}
function stringOrNull(value: unknown): string | null {
return typeof value === "string" && value.length > 0 ? value : null;
}
function escapeHtml(value: string): string {
return value.replace(/&/gu, "&amp;").replace(/</gu, "&lt;").replace(/>/gu, "&gt;").replace(/"/gu, "&quot;");
}
function escapeAttr(value: string): string {
return escapeHtml(value).replace(/'/gu, "&#39;");
}