From ab1725e6730b5781e98b3a69ee71262b2ca940d7 Mon Sep 17 00:00:00 2001 From: zhongzm Date: Fri, 22 Aug 2025 15:38:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8C=87?= =?UTF-8?q?=E6=A0=87=E7=BB=9F=E8=AE=A1=E4=BF=A1=E6=81=AF=E6=94=B9=E5=A4=A9?= =?UTF-8?q?=E6=95=B0=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/perfManage/kpiCReport/index.vue | 155 +++++++++++----------- 1 file changed, 75 insertions(+), 80 deletions(-) diff --git a/src/views/perfManage/kpiCReport/index.vue b/src/views/perfManage/kpiCReport/index.vue index 46caeebe..a1c6ee6d 100644 --- a/src/views/perfManage/kpiCReport/index.vue +++ b/src/views/perfManage/kpiCReport/index.vue @@ -282,7 +282,7 @@ const statsColumns: TableColumnType[] = [ 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[] = [ 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[] = [ 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 = {}; - // 获取近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() 成功后获取,避免重复调用 } }); }