Files
Gset/static/js/pages/RootCause.js
T
treeandClaude Opus 5.5 e9616af34d 修复 P0:根因判定、退服率口径、影响评估与告警关联量化
- 根因判定改为三级(衍生关系/时间窗/疑似)并带置信度;小区退服告警 ±2 分钟匹配;
  衍生告警按父告警归类;优先选随业务恢复清除的告警;时间窗按历史流水 95% 分位数标定
- 退服率按日历统计周期计算;站点数分母支持站名关键字;重复退服率除以全部站点;
  月均按统计周期自然月数;导出与看板同一口径
- 影响评估新增受影响用户·小时与 500 米邻站覆盖补偿
- 新增告警—退服关联量化(支持度/置信度/提升度)及 /api/association,根因页展示
- 新增 tests/(扰动数据集留出检验、指标口径检验)与 bench.py;旧库启动时自动迁移

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:43:34 +08:00

154 lines
8.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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]]}<br>判定:${causes[s.value[0]]}<br><b>${s.value[2]} 条</b>`},
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}<br>次数 ${r.count}<br>平均时长 <b>${r.avg_min} min</b><br>超长占比 ${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}>全量重新分析</${GlassButton}>`;
return html`
<div className="grid">
<${Section} span=${5} title="根因判定规则" extra=${runButton}>
<div className="note">
<b>预处理 · 衍生告警归并</b>:带父告警的告警按父告警类别计(停电引起的传输告警归为电源故障)。<br/>
<b>第一级 · 衍生关系(置信度高)</b>:与退服开始相差 ≤ 2 分钟的“小区退服”告警带父告警时,取父告警类别。<br/>
<b>第二级 · 时间窗(置信度中)</b>:同站在退服时仍未清除、提前量在该类时间窗内的告警。<br/>
<b>第三级 · 疑似(置信度低)</b>:时间窗放宽到 3 倍才命中,提示人工复核。<br/>
多条命中时依次比较:级别 → 是否随业务恢复清除(±15 分钟)→ 规则优先级 → 时间早。
时间窗在每次全量分析时按已闭环流水的提前量 95% 分位数重新标定(不小于默认值);均未命中判为“其他”。
</div>
<div className="chips">
<div className="chip"><span>参与校验</span>${num(total)} 条</div>
<div className="chip" data-tone="green"><span>判定准确率</span>${total ? (hit / total * 100).toFixed(2) : 0}%</div>
${confusion.by_confidence.map(c => html`
<div className="chip" key=${c.level}><span>置信度${c.level}</span>${num(c.n)} 条 / ${c.acc}%</div>`)}
<div className="chip"><span>上次分析</span>${lastRun ? `${num(lastRun.n)} 条 / ${lastRun.elapsed}s` : '—'}</div>
</div>
</${Section}>
<${Section} span=${7} title="上报原因 × 系统判定根因 交叉矩阵" hint="对角线为判定一致"><${EChart} option=${matrixChart}/></${Section}>
<${Section} span=${5} title="时间窗标定" hint="单位:分钟">
<${DataTable} rowKey="category" columns=${WINDOW_COLUMNS} rows=${assoc.windows}/>
</${Section}>
<${Section} span=${7} title="告警 → 退服 关联强度" hint="提升度 > 1 表示该类告警出现后退服概率高于随机">
<${DataTable} rowKey="category" columns=${ASSOC_COLUMNS} rows=${assoc.rows}/>
<div className="note">
支持度 = 开始前 W 分钟内有该类告警的退服 ÷ 退服总数;置信度 = 其后 W 分钟内发生退服的告警 ÷ 该类告警总数;
提升度 = 置信度 ÷ 随机一个 W 分钟窗口内遇到退服的概率(W 为该类时间窗)。
</div>
</${Section}>
<${Section} title="根因维度统计" hint="柱高为平均退服时长(分钟)"><${EChart} height=${260} option=${causeChart}/></${Section}>
<${AdviceList}/>
</div>`;
}
/** 超长退服记录 + 分级处置建议(分页) */
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)}>导出建议清单</${GlassButton}>
</${Fragment}>`;
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`
<div>
<${DataTable} rows=${data.rows} columns=${columns}/>
<${Pager} page=${page} size=${PAGE_SIZE} total=${data.total} onPage=${setPage}
prefix=${`超长退服 ${num(data.total)} 条,100% 已生成处置建议`}/>
</div>` : html`<${Loading}/>`}
</${Section}>`;
}