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}
场景:${r.scene}
累计:${s.value} h(${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}
{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]]}
${s.value[2]} h`},
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
${s.value} 次`},
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}
平均退服时长 ${s.value} min
退服率 ${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`