💥 重新优化监控表的计算;修复之前百分百成功的错误计算

This commit is contained in:
hi2hi 2025-12-08 13:53:39 +08:00
parent 015ce40586
commit 33f1625ab1

View File

@ -261,6 +261,8 @@ const monitorChartType = computed(() => {
//
const nowServerTime = computed(() => store.state.serverTime || Date.now());
// const nowServerTime = computed(() => Date.now());
// console.log(store.state.serverTime);
const accpetShowTime = computed(() => (Math.floor(nowServerTime.value / 60000) - minute.value) * 60000);
const minuteActiveArrowStyle = computed(() => {
@ -281,109 +283,161 @@ const monitorChartData = computed(() => {
* - name {String}: 监控名称
* - data {Array}: [时间戳, 平均延迟] 对的数组
*/
const cateList = [];
const cateMap = {};
const dateSet = new Set();
let valueList = [];
monitorData.value.forEach((i) => {
const dateMap = {};
if (!cateMap[i.monitor_name]) {
cateMap[i.monitor_name] = {
id: i.monitor_id,
dateMap,
avgs: [],
const dateMap = new Map();
const {
monitor_name,
monitor_id,
created_at,
avg_delay,
} = i;
if (!cateMap[monitor_name]) {
cateMap[monitor_name] = {
id: monitor_id,
};
}
const showAvgDelay = [];
const showCreateTime = [];
const accpeTimeMap = {};
i.created_at.forEach((o, index) => {
const status = o >= accpetShowTime.value;
const cateDelayMap = new Map();
const cateAcceptTimeMap = new Map();
const cateCreateTime = new Set();
//
let earliestTimestamp = nowServerTime.value;
created_at.forEach((time, index) => {
if (time < earliestTimestamp) {
earliestTimestamp = time;
}
const status = time >= accpetShowTime.value;
// cateAcceptTime
if (status) {
accpeTimeMap[o] = i.avg_delay[index];
if (import.meta.env.VITE_MONITOR_DEBUG === '1' && cateAcceptTimeMap.has(time)) {
console.log(`${monitor_name} ${time} 重复,值对比: ${avg_delay[index]} vs ${cateAcceptTimeMap.get(time)}`);
}
cateAcceptTimeMap.set(time, avg_delay[index]);
}
});
const allMintues = Math.floor((Date.now() - accpetShowTime.value) / 60000);
for (let j = 0; j < allMintues; j += 1) {
const time = accpetShowTime.value + j * 60000;
showCreateTime.push(time);
const timeProp = accpeTimeMap[time];
if (timeProp) {
showAvgDelay.push(timeProp);
} else {
showAvgDelay.push(null);
}
if (import.meta.env.VITE_MONITOR_DEBUG === '1') {
console.log(`${monitor_name} created_at`, earliestTimestamp);
console.log(`${monitor_name} created_at`, JSON.parse(JSON.stringify(created_at)));
console.log(`${monitor_name} avg_delay`, JSON.parse(JSON.stringify(avg_delay)));
}
//
const actualStartTime = Math.max(
accpetShowTime.value,
earliestTimestamp,
);
//
const allMintues = Math.floor((Date.now() - actualStartTime) / 60000);
//
for (let j = 0; j < allMintues; j += 1) {
const time = actualStartTime + j * 60000;
//
cateCreateTime.add(time);
//
const timeProp = cateAcceptTimeMap.get(time);
cateDelayMap.set(time, timeProp ?? undefined);
}
//
const {
median,
tolerancePercent,
} = peakShaving.value ? getThreshold(showAvgDelay) : {};
showCreateTime.forEach((o, index) => {
if (Object.prototype.hasOwnProperty.call(dateMap, o)) {
return;
}
const avgDelay = showAvgDelay[index];
// 0
if (avgDelay === null || avgDelay === 0) {
dateMap[o] = undefined;
return;
}
} = peakShaving.value ? getThreshold(Array.from(cateDelayMap.values())) : {};
//
cateCreateTime.values().forEach((time) => {
const avgDelay = cateDelayMap.get(time) * 1;
//
if (peakShaving.value) {
//
const threshold = median * tolerancePercent;
//
if (Math.abs(avgDelay - median) > threshold) {
dateMap[o] = undefined;
dateMap.set(time, null);
return;
}
}
dateMap[o] = (avgDelay).toFixed(2) * 1;
// undefined
if (Number.isNaN(avgDelay)) {
dateMap.set(time, undefined);
} else {
dateMap.set(time, (avgDelay).toFixed(2) * 1);
}
});
});
let dateList = [];
let valueList = [];
const cateList = [];
Object.keys(cateMap).forEach((i) => {
const {
id,
dateMap,
avgs,
} = cateMap[i];
Object.entries(dateMap).forEach(([key, value]) => {
const time = parseInt(key, 10);
avgs.push([time, value]);
dateList.push(time);
const lineData = [];
const validatedData = [];
const overValidatedData = [];
let delayTotal = 0;
dateMap.forEach((val, key) => {
const time = parseInt(key, 10); //
lineData.push([time, val || null]);
if (val) {
dateSet.add(time);
validatedData.push([time, val]);
delayTotal += val;
}
if (val !== undefined) {
overValidatedData.push([time, val]);
}
});
const color = getLineColor(id);
if (avgs.length) {
if (import.meta.env.VITE_MONITOR_DEBUG === '1') {
cateMap[monitor_name].origin = {
cateCreateTime,
cateDelayMap,
cateAcceptTimeMap,
dateMap,
lineData,
validatedData,
overValidatedData,
delayTotal,
};
}
const id = monitor_id;
//
const avgDelay = delayTotal / validatedData.length || 0;
if (lineData && lineData.length) {
if (!validate.hasOwn(showCates.value, id)) {
showCates.value[id] = true;
}
//
// (undefined)
const realAvgs = avgs.filter((a) => a[1] !== undefined);
const validAvgs = realAvgs.filter((a) => a[1] !== 0 && a[1] !== null);
const avg = validAvgs.reduce((a, b) => a + b[1], 0) / validAvgs.length;
const over = validAvgs.length / realAvgs.length;
const color = getLineColor(id);
// = /
const over = overValidatedData.length > 0 ? overValidatedData.length / lineData.length : 0;
const validRate = 1 - ((validatedData.length > 0 && overValidatedData.length > 0)
? validatedData.length / overValidatedData.length : 0);
const cateItem = {
id,
name: i,
name: monitor_name,
color,
avg: avg.toFixed(2) * 1,
avg: avgDelay.toFixed(2) * 1,
over: (over * 100).toFixed(2) * 1,
validRate: (validRate * 100).toFixed(2) * 1,
};
if (Number.isNaN(cateItem.avg)) {
cateItem.avg = 0;
}
const titles = [
cateItem.name,
cateItem.avg === 0 ? '' : `平均延迟:${cateItem.avg}ms`,
`成功率:${cateItem.over}%`,
];
if (peakShaving.value) {
titles.push(`削峰率: ${cateItem.validRate}%`);
}
cateItem.title = titles.filter((s) => s).join('\n');
cateList.push(cateItem);
valueList.push({
id,
name: i,
data: avgs,
name: monitor_name,
data: lineData,
itemStyle: {
color,
},
@ -393,8 +447,15 @@ const monitorChartData = computed(() => {
});
}
});
dateList = dateList.sort((a, b) => a - b);
const dateList = Array.from(dateSet).sort((a, b) => a - b);
valueList = valueList.filter((i) => showCates.value[i.id]);
if (import.meta.env.VITE_MONITOR_DEBUG === '1') {
window._cateMap = cateMap;
console.log(window._cateMap);
console.log(dateList, cateList, valueList);
}
return {
dateList,
cateList,