Flask + SQLite 后端,免构建 React + ECharts 前端,含合成样例数据。 测试参考数据(FOR-TEST-ONLY)与运行时数据库不入库。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
6.2 KiB
JavaScript
112 lines
6.2 KiB
JavaScript
import {GlassSegmentedControl, html, useState} from 'vendor';
|
||
import {useApp, useFetch} from '../api.js';
|
||
import {accentMax, BLUE, BLUE_SCALE, CAUSE_COLOR, fixed, heatLabelColor, num, shortSite} from '../format.js';
|
||
import {EChart} from '../components/EChart.js';
|
||
import {Kpi, Loading, Section} from '../components/ui.js';
|
||
|
||
export default function Dashboard() {
|
||
const {filters} = useApp();
|
||
const {data} = useFetch('/api/dashboard', filters);
|
||
if (!data) return html`<${Loading}/>`;
|
||
const k = data.kpi;
|
||
|
||
// 横向条形图自下而上绘制,反转后最大值在最上
|
||
const rank = [...data.rank].reverse();
|
||
const scenes = [...new Set(data.heat.map(h => h[0]))];
|
||
const bench = data.bench.filter(x => x.count);
|
||
|
||
// 热力图:每个格子单独设置数字颜色,深色格子用白字
|
||
const heatMax = Math.max(1, ...data.heat.map(h => h[2]));
|
||
const heatCells = [];
|
||
for (const [scene, month, hours] of data.heat) {
|
||
heatCells.push({
|
||
value: [data.months.indexOf(month), scenes.indexOf(scene), hours],
|
||
label: {color: heatLabelColor(hours, heatMax)},
|
||
});
|
||
}
|
||
|
||
const rankChart = {
|
||
tooltip: {trigger: 'axis', axisPointer: {type: 'shadow'}, formatter: ([s]) => {
|
||
const r = rank[s.dataIndex];
|
||
return `${r.site_name}<br>场景:${r.scene}<br>累计:<b>${s.value} h</b>(${r.count} 次)`;
|
||
}},
|
||
xAxis: {type: 'value'},
|
||
yAxis: {type: 'category', data: rank.map(r => shortSite(r.site_name)), axisLabel: {fontSize: 11}},
|
||
series: [{type: 'bar', barMaxWidth: 16, itemStyle: {borderRadius: [0, 6, 6, 0]},
|
||
data: accentMax(rank.map(r => +(r.total_min / 60).toFixed(1)))}],
|
||
};
|
||
const pieChart = {
|
||
tooltip: {trigger: 'item', formatter: '{b}<br>{c} 次({d}%)'},
|
||
legend: {bottom: 0, icon: 'circle', itemWidth: 8},
|
||
series: [{type: 'pie', radius: ['50%', '74%'], center: ['50%', '44%'], padAngle: 2,
|
||
itemStyle: {borderRadius: 6}, label: {formatter: '{d}%'},
|
||
data: data.pie.map(([name, value]) => ({name, value, itemStyle: {color: CAUSE_COLOR[name]}}))}],
|
||
};
|
||
const heatChart = {
|
||
tooltip: {formatter: s => `${scenes[s.value[1]]} · ${data.months[s.value[0]]}<br><b>${s.value[2]} h</b>`},
|
||
grid: {left: 12, right: 70, top: 10, bottom: 10, containLabel: true},
|
||
xAxis: {type: 'category', data: data.months},
|
||
yAxis: {type: 'category', data: scenes},
|
||
visualMap: {min: 0, max: heatMax, right: 0, top: 'middle',
|
||
itemHeight: 160, calculable: true, inRange: {color: BLUE_SCALE}},
|
||
series: [{type: 'heatmap', data: heatCells,
|
||
itemStyle: {borderColor: 'transparent', borderWidth: 2, borderRadius: 6},
|
||
label: {show: true, fontSize: 10}}],
|
||
};
|
||
const hourChart = {
|
||
tooltip: {trigger: 'axis', formatter: ([s]) => `${s.name}:00 - ${s.name}:59<br><b>${s.value} 次</b>`},
|
||
xAxis: {type: 'category', data: [...Array(24).keys()]},
|
||
yAxis: {type: 'value'},
|
||
series: [{type: 'bar', barMaxWidth: 12, data: accentMax(data.hours)}],
|
||
};
|
||
const benchChart = {
|
||
tooltip: {trigger: 'axis', formatter: ([s]) =>
|
||
`${s.name}<br>平均退服时长 <b>${s.value} min</b><br>退服率 ${bench[s.dataIndex].outage_rate}%`},
|
||
xAxis: {type: 'category', data: bench.map(x => x.scene)},
|
||
yAxis: {type: 'value'},
|
||
series: [{type: 'bar', data: accentMax(bench.map(x => x.avg_min)), label: {show: true, position: 'top'}}],
|
||
};
|
||
|
||
return html`
|
||
<div className="grid">
|
||
<${Section} title="运维考核指标" hint=${`计算耗时 ${data.elapsed}s`}>
|
||
<div className="kpis">
|
||
<${Kpi} title="退服次数" value=${num(k.count)} unit="次" sub=${`覆盖 ${k.sites} 个基站`}/>
|
||
<${Kpi} title="累计退服时长" value=${num(k.total_h)} unit="h"/>
|
||
<${Kpi} title="平均退服时长" value=${k.avg_min} unit="min"/>
|
||
<${Kpi} title="退服率" value=${fixed(k.outage_rate)} unit="%" sub="退服时长 / (站点数 × 统计时长)"/>
|
||
<${Kpi} title="网络可用率" value=${fixed(k.availability)} unit="%"/>
|
||
<${Kpi} title="超长退服占比" value=${fixed(k.long_ratio)} unit="%"/>
|
||
<${Kpi} title="重复退服率" value=${fixed(k.repeat_rate)} unit="%" sub="高频站点 / 有退服站点"/>
|
||
<${Kpi} title="受影响用户" value=${num(k.users)} unit="人次"/>
|
||
<${Kpi} title="根因判定准确率" value=${fixed(k.acc)} unit="%"/>
|
||
<${Kpi} title="闭环率" value=${fixed(k.closed)} unit="%"/>
|
||
<${Kpi} title="上报时长差错" value=${k.dur_fix} unit="条" sub="已按起止时间重算纠正"/>
|
||
</div>
|
||
</${Section}>
|
||
<${Section} span=${5} title="站点累计退服时长 Top 15" hint="单位:小时"><${EChart} height=${380} option=${rankChart}/></${Section}>
|
||
<${TrendCard} trend=${data.trend}/>
|
||
<${Section} span=${4} title="退服根因占比"><${EChart} option=${pieChart}/></${Section}>
|
||
<${Section} span=${8} title="场景 × 月份 退服时长热力图" hint="单位:小时"><${EChart} option=${heatChart}/></${Section}>
|
||
<${Section} span=${7} title="退服发生时段分布" hint="单位:次"><${EChart} option=${hourChart}/></${Section}>
|
||
<${Section} span=${5} title="分场景平均退服时长对标" hint="单位:分钟"><${EChart} option=${benchChart}/></${Section}>
|
||
</div>`;
|
||
}
|
||
|
||
/** 月度趋势:看板的主图(最宽、唯一带面积填充的图),可切换次数 / 时长 */
|
||
function TrendCard({trend}) {
|
||
const [key, setKey] = useState('n');
|
||
const switcher = html`
|
||
<${GlassSegmentedControl} aria-label="趋势指标" value=${key} onValueChange=${setKey}
|
||
items=${[{value: 'n', label: '次数'}, {value: 'h', label: '时长(h)'}]}/>`;
|
||
const option = {
|
||
tooltip: {trigger: 'axis'},
|
||
xAxis: {type: 'category', data: trend.map(t => t.month), boundaryGap: false},
|
||
yAxis: {type: 'value', name: key === 'n' ? '次' : '小时'},
|
||
series: [{type: 'line', data: trend.map(t => t[key]), color: BLUE,
|
||
areaStyle: {color: {type: 'linear', x: 0, y: 0, x2: 0, y2: 1,
|
||
colorStops: [{offset: 0, color: 'rgba(0,122,255,.22)'}, {offset: 1, color: 'rgba(0,122,255,0)'}]}}}],
|
||
};
|
||
return html`<${Section} span=${7} title="月度退服趋势" extra=${switcher}><${EChart} height=${380} option=${option}/></${Section}>`;
|
||
}
|