import {Fragment, GlassButton, html, useState, useToast} from 'vendor'; import {api, download, useApp, useFetch} from '../api.js'; import {BLUE_SCALE, CAUSE_COLOR, heatLabelColor, num} from '../format.js'; import {EChart} from '../components/EChart.js'; import {DataTable, Pager} from '../components/DataTable.js'; import {Cause, LevelTag, Loading, Section, Select, StatusTag} from '../components/ui.js'; const PAGE_SIZE = 15; const WINDOW_COLUMNS = [ {key: 'priority', title: '优先级'}, {key: 'category', title: '告警类别', render: v => html`<${Cause} value=${v}/>`}, {key: 'default_min', title: '默认'}, {key: 'window_min', title: '当前'}, {key: 'samples', title: '标定样本', render: num}, {key: 'source', title: '来源'}, ]; const ASSOC_COLUMNS = [ {key: 'category', title: '告警类别', render: v => html`<${Cause} value=${v}/>`}, {key: 'window_min', title: '窗口(分)'}, {key: 'alarms', title: '告警数', render: num}, {key: 'hits', title: '其后退服', render: num}, {key: 'support', title: '支持度(%)'}, {key: 'confidence', title: '置信度(%)'}, {key: 'lift', title: '提升度'}, ]; export default function RootCause() { const {filters, isAdmin} = useApp(); const toast = useToast(); const {data: confusion} = useFetch('/api/confusion', filters); const {data: stats} = useFetch('/api/stats', {...filters, dim: 'root_cause'}); const {data: assoc, reload: reloadAssoc} = useFetch('/api/association', filters); const [lastRun, setLastRun] = useState(null); const [running, setRunning] = useState(false); const runAnalysis = async () => { setRunning(true); try { const r = await api('/api/analyze', {method: 'POST'}); setLastRun(r); reloadAssoc(); toast({message: `全量分析完成:${num(r.n)} 条,耗时 ${r.elapsed}s,准确率 ${r.acc}%`, tone: 'success'}); } catch (e) { toast({message: e.message, tone: 'error'}); } setRunning(false); }; if (!confusion || !stats || !assoc) return html`<${Loading}/>`; // 交叉矩阵:matrix[i][j] = 上报原因 i、系统判定 j 的条数;对角线 i === j 为判定一致 const {causes, matrix} = confusion; const n = causes.length; let total = 0; let hit = 0; let maxCount = 1; for (const row of matrix) { maxCount = Math.max(maxCount, ...row); } // 热力图数据 value = [列, 行, 条数];行号倒过来,让第一个原因显示在最上面;深色格子用白字 const cells = []; for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { const count = matrix[i][j]; total += count; if (i === j) hit += count; cells.push({value: [j, n - 1 - i, count], label: {color: heatLabelColor(count, maxCount)}}); } } const matrixChart = { tooltip: {formatter: s => `上报:${causes[n - 1 - s.value[1]]}
判定:${causes[s.value[0]]}
${s.value[2]} 条`}, grid: {left: 12, right: 12, top: 10, bottom: 30, containLabel: true}, xAxis: {type: 'category', data: causes, name: '判定', nameLocation: 'center', nameGap: 26}, yAxis: {type: 'category', data: [...causes].reverse()}, visualMap: {show: false, min: 0, max: maxCount, inRange: {color: BLUE_SCALE}}, series: [{type: 'heatmap', data: cells, label: {show: true}, itemStyle: {borderColor: 'transparent', borderWidth: 2, borderRadius: 6}}], }; const causeChart = { tooltip: {trigger: 'axis', axisPointer: {type: 'shadow'}, formatter: ([s]) => { const r = stats.rows[s.dataIndex]; return `${r.key}
次数 ${r.count}
平均时长 ${r.avg_min} min
超长占比 ${r.long_ratio}%`; }}, xAxis: {type: 'category', data: stats.rows.map(r => r.key)}, yAxis: {type: 'value'}, series: [{type: 'bar', barMaxWidth: 40, label: {show: true, position: 'top'}, data: stats.rows.map(r => ({value: r.avg_min, itemStyle: {color: CAUSE_COLOR[r.key]}}))}], }; const runButton = isAdmin && html` <${GlassButton} variant="glassProminent" loading=${running} onClick=${runAnalysis}>全量重新分析`; return html`
<${Section} span=${5} title="根因判定规则" extra=${runButton}>
预处理 · 衍生告警归并:带父告警的告警按父告警类别计(停电引起的传输告警归为电源故障)。
第一级 · 衍生关系(置信度高):与退服开始相差 ≤ 2 分钟的“小区退服”告警带父告警时,取父告警类别。
第二级 · 时间窗(置信度中):同站在退服时仍未清除、提前量在该类时间窗内的告警。
第三级 · 疑似(置信度低):时间窗放宽到 3 倍才命中,提示人工复核。
多条命中时依次比较:级别 → 是否随业务恢复清除(±15 分钟)→ 规则优先级 → 时间早。 时间窗在每次全量分析时按已闭环流水的提前量 95% 分位数重新标定(不小于默认值);均未命中判为“其他”。
参与校验${num(total)} 条
判定准确率${total ? (hit / total * 100).toFixed(2) : 0}%
${confusion.by_confidence.map(c => html`
置信度${c.level}${num(c.n)} 条 / ${c.acc}%
`)}
上次分析${lastRun ? `${num(lastRun.n)} 条 / ${lastRun.elapsed}s` : '—'}
<${Section} span=${7} title="上报原因 × 系统判定根因 交叉矩阵" hint="对角线为判定一致"><${EChart} option=${matrixChart}/> <${Section} span=${5} title="时间窗标定" hint="单位:分钟"> <${DataTable} rowKey="category" columns=${WINDOW_COLUMNS} rows=${assoc.windows}/> <${Section} span=${7} title="告警 → 退服 关联强度" hint="提升度 > 1 表示该类告警出现后退服概率高于随机"> <${DataTable} rowKey="category" columns=${ASSOC_COLUMNS} rows=${assoc.rows}/>
支持度 = 开始前 W 分钟内有该类告警的退服 ÷ 退服总数;置信度 = 其后 W 分钟内发生退服的告警 ÷ 该类告警总数; 提升度 = 置信度 ÷ 随机一个 W 分钟窗口内遇到退服的概率(W 为该类时间窗)。
<${Section} title="根因维度统计" hint="柱高为平均退服时长(分钟)"><${EChart} height=${260} option=${causeChart}/> <${AdviceList}/>
`; } /** 超长退服记录 + 分级处置建议(分页) */ function AdviceList() { const {meta, filters} = useApp(); const [cause, setCause] = useState(''); const [page, setPage] = useState(1); const params = {...filters, long: 1, root_cause: cause}; const {data} = useFetch('/api/outages', {...params, sort: 'duration_min', page, size: PAGE_SIZE}); const tools = html`<${Fragment}> <${Select} label="根因" all="全部根因" options=${meta.causes} value=${cause} onChange=${v => { setCause(v); setPage(1); }}/> <${GlassButton} onClick=${() => download('/api/export/advice', params)}>导出建议清单 `; const columns = [ {key: 'ne_id', title: '基站编号'}, {key: 'site_name', title: '基站名称'}, {key: 'scene', title: '场景'}, {key: 'start_time', title: '退服开始'}, {key: 'duration_min', title: '时长(分)'}, {key: 'root_cause', title: '根因', render: v => html`<${Cause} value=${v}/>`}, {key: 'confidence', title: '置信度'}, {key: 'evidence', title: '关联告警证据'}, {key: 'level', title: '分级', render: v => html`<${LevelTag} value=${v}/>`}, {key: 'status', title: '状态', render: v => html`<${StatusTag} value=${v}/>`}, {key: 'advice', title: '处置建议', wrap: true}, ]; return html` <${Section} title="超长退服根因与分级处置建议" extra=${tools}> ${data ? html`
<${DataTable} rows=${data.rows} columns=${columns}/> <${Pager} page=${page} size=${PAGE_SIZE} total=${data.total} onPage=${setPage} prefix=${`超长退服 ${num(data.total)} 条,100% 已生成处置建议`}/>
` : html`<${Loading}/>`} `; }