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