feat:自定义指标统计信息改天数显示

This commit is contained in:
zhongzm
2025-08-22 15:38:55 +08:00
parent 77631de0ff
commit ab1725e673

View File

@@ -282,7 +282,7 @@ const statsColumns: TableColumnType<any>[] = [
if (value === '-') { if (value === '-') {
return value; return value;
} }
if (unit.includes('%') || unit.toLowerCase().includes('mbps') || unit.toLowerCase().includes('bps')) { if (unit.includes('%') || unit === 'Mbps') {
return `${value} ${t('views.perfManage.customTarget.avg')}`; return `${value} ${t('views.perfManage.customTarget.avg')}`;
} else { } else {
return `${value} ${t('views.perfManage.customTarget.total')}`; return `${value} ${t('views.perfManage.customTarget.total')}`;
@@ -302,7 +302,7 @@ const statsColumns: TableColumnType<any>[] = [
if (value === '-') { if (value === '-') {
return value; return value;
} }
if (unit.includes('%') || unit.toLowerCase().includes('mbps') || unit.toLowerCase().includes('bps')) { if (unit.includes('%') || unit === 'Mbps') {
return `${value} ${t('views.perfManage.customTarget.avg')}`; return `${value} ${t('views.perfManage.customTarget.avg')}`;
} else { } else {
return `${value} ${t('views.perfManage.customTarget.total')}`; return `${value} ${t('views.perfManage.customTarget.total')}`;
@@ -322,7 +322,7 @@ const statsColumns: TableColumnType<any>[] = [
if (value === '-') { if (value === '-') {
return value; return value;
} }
if (unit.includes('%') || unit.toLowerCase().includes('mbps') || unit.toLowerCase().includes('bps')) { if (unit.includes('%') || unit === 'Mbps') {
return `${value} ${t('views.perfManage.customTarget.avg')}`; return `${value} ${t('views.perfManage.customTarget.avg')}`;
} else { } else {
return `${value} ${t('views.perfManage.customTarget.total')}`; return `${value} ${t('views.perfManage.customTarget.total')}`;
@@ -419,19 +419,10 @@ async function fnGetStatsData() {
if (!state.neType[0]) return; if (!state.neType[0]) return;
const now = new Date(); const now = new Date();
const stats: Record<string, any> = {};
// 获取近1天、7天、30天的数据 // 只请求一次近30天的数据
const periods = [
{ key: 'last1Day', days: 1 },
{ key: 'last7Days', days: 7 },
{ key: 'last30Days', days: 30 }
];
// 获取所有统计数据,等全部计算完成后统一更新
for (const period of periods) {
const startTime = new Date(now); const startTime = new Date(now);
startTime.setDate(now.getDate() - period.days); startTime.setDate(now.getDate() - 30);
startTime.setHours(0, 0, 0, 0); startTime.setHours(0, 0, 0, 0);
const endTime = new Date(now); const endTime = new Date(now);
@@ -449,7 +440,13 @@ async function fnGetStatsData() {
try { try {
const res = await listCustomData(params); const res = await listCustomData(params);
if (res.code === RESULT_CODE_SUCCESS && Array.isArray(res.data)) { if (res.code === RESULT_CODE_SUCCESS && Array.isArray(res.data)) {
// 为每个指标计算统计值暂存到stats对象中 // 计算时间边界
const now_ms = now.getTime();
const day1_start = now_ms - (1 * 24 * 60 * 60 * 1000); // 1天前
const day7_start = now_ms - (7 * 24 * 60 * 60 * 1000); // 7天前
const day30_start = now_ms - (30 * 24 * 60 * 60 * 1000); // 30天前
// 为每个指标计算统计值
for (const columns of tableColumns.value) { for (const columns of tableColumns.value) {
if ( if (
columns.key === 'neName' || columns.key === 'neName' ||
@@ -459,57 +456,56 @@ async function fnGetStatsData() {
continue; continue;
} }
const values = res.data.map((item: any) => { const unit = columns.unit || '';
return item[columns.key] ? Number(item[columns.key]) : 0; const isAverageType = unit.includes('%') || unit === 'Mbps';
// 根据时间范围筛选非零数据
const data1Day = res.data.filter((item: any) => {
const itemTime = new Date(item.timeGroup).getTime();
const value = item[columns.key] ? Number(item[columns.key]) : 0;
return itemTime >= day1_start && value !== 0;
}); });
let calculatedValue = 0; const data7Days = res.data.filter((item: any) => {
if (values.length > 0) { const itemTime = new Date(item.timeGroup).getTime();
const unit = columns.unit || ''; const value = item[columns.key] ? Number(item[columns.key]) : 0;
// 智能单位判断:百分比值%和速率值Mbps计算平均值普通值计算累加值 return itemTime >= day7_start && value !== 0;
if (unit.includes('%') || unit.toLowerCase().includes('mbps') || unit.toLowerCase().includes('bps')) { });
const data30Days = res.data.filter((item: any) => {
const itemTime = new Date(item.timeGroup).getTime();
const value = item[columns.key] ? Number(item[columns.key]) : 0;
return itemTime >= day30_start && value !== 0;
});
// 计算统计值(只对非零数据进行计算)
const calculateValue = (dataArray: any[]) => {
if (dataArray.length === 0) return 0;
const values = dataArray.map((item: any) => Number(item[columns.key]));
if (isAverageType) {
// 百分比和速率值使用平均值 // 百分比和速率值使用平均值
calculatedValue = Number((values.reduce((sum, val) => sum + val, 0) / values.length).toFixed(2)); return Number((values.reduce((sum, val) => sum + val, 0) / values.length).toFixed(2));
} else { } else {
// 普通值使用累加值 // 普通值使用累加值
calculatedValue = Number(values.reduce((sum, val) => sum + val, 0).toFixed(2)); return Number(values.reduce((sum, val) => sum + val, 0).toFixed(2));
}
} }
};
if (!stats[columns.key]) { // 更新对应的统计数据
stats[columns.key] = {}; const statsIndex = kpiStats.value.findIndex((item: any) => item.kpiId === columns.key);
if (statsIndex !== -1) {
kpiStats.value[statsIndex].last1Day = calculateValue(data1Day);
kpiStats.value[statsIndex].last7Days = calculateValue(data7Days);
kpiStats.value[statsIndex].last30Days = calculateValue(data30Days);
} }
stats[columns.key][period.key] = calculatedValue;
} }
} }
} catch (error) { } catch (error) {
console.error(`获取${period.key}数据失败:`, error); console.error('获取统计数据失败:', error);
// 如果获取失败,设置默认值为0 // 如果获取失败,保持默认值
for (const columns of tableColumns.value) {
if (
columns.key === 'neName' ||
columns.key === 'startIndex' ||
columns.key === 'timeGroup'
) {
continue;
}
if (!stats[columns.key]) {
stats[columns.key] = {};
}
stats[columns.key][period.key] = 0;
}
}
}
// 所有统计数据计算完成后,统一更新显示
for (const statsItem of kpiStats.value) { for (const statsItem of kpiStats.value) {
const kpiId = statsItem.kpiId;
if (stats[kpiId]) {
statsItem.last1Day = stats[kpiId].last1Day || 0;
statsItem.last7Days = stats[kpiId].last7Days || 0;
statsItem.last30Days = stats[kpiId].last30Days || 0;
} else {
// 如果没有统计数据,保持默认值
statsItem.last1Day = '-'; statsItem.last1Day = '-';
statsItem.last7Days = '-'; statsItem.last7Days = '-';
statsItem.last30Days = '-'; statsItem.last30Days = '-';
@@ -594,8 +590,7 @@ function fnGetListTitle() {
.then(result => { .then(result => {
if (result) { if (result) {
fnGetList(); fnGetList();
// 获取近期统计数据 // 统计数据将在 fnGetList() 成功后获取,避免重复调用
//fnGetStatsData();
} }
}); });
} }