Przeglądaj źródła

fix: 修复 timestamp2string1 跨年显示问题,仅在数据跨年时显示年份

CaIon 23 godzin temu
rodzic
commit
a195e88896
2 zmienionych plików z 32 dodań i 12 usunięć
  1. 17 5
      web/src/helpers/dashboard.jsx
  2. 15 7
      web/src/helpers/utils.jsx

+ 17 - 5
web/src/helpers/dashboard.jsx

@@ -26,6 +26,7 @@ import {
 import {
   timestamp2string,
   timestamp2string1,
+  isDataCrossYear,
   copy,
   showSuccess,
 } from './utils';
@@ -259,13 +260,16 @@ export const processRawData = (
     timeCountMap: new Map(),
   };
 
+  // 检查数据是否跨年
+  const showYear = isDataCrossYear(data.map(item => item.created_at));
+
   data.forEach((item) => {
     result.uniqueModels.add(item.model_name);
     result.totalTokens += item.token_used;
     result.totalQuota += item.quota;
     result.totalTimes += item.count;
 
-    const timeKey = timestamp2string1(item.created_at, dataExportDefaultTime);
+    const timeKey = timestamp2string1(item.created_at, dataExportDefaultTime, showYear);
     if (!result.timePoints.includes(timeKey)) {
       result.timePoints.push(timeKey);
     }
@@ -323,8 +327,11 @@ export const calculateTrendData = (
 export const aggregateDataByTimeAndModel = (data, dataExportDefaultTime) => {
   const aggregatedData = new Map();
 
+  // 检查数据是否跨年
+  const showYear = isDataCrossYear(data.map(item => item.created_at));
+
   data.forEach((item) => {
-    const timeKey = timestamp2string1(item.created_at, dataExportDefaultTime);
+    const timeKey = timestamp2string1(item.created_at, dataExportDefaultTime, showYear);
     const modelKey = item.model_name;
     const key = `${timeKey}-${modelKey}`;
 
@@ -358,10 +365,15 @@ export const generateChartTimePoints = (
     const lastTime = Math.max(...data.map((item) => item.created_at));
     const interval = getTimeInterval(dataExportDefaultTime, true);
 
-    chartTimePoints = Array.from(
+    // 生成时间点数组,用于检查是否跨年
+    const generatedTimestamps = Array.from(
       { length: DEFAULTS.MAX_TREND_POINTS },
-      (_, i) =>
-        timestamp2string1(lastTime - (6 - i) * interval, dataExportDefaultTime),
+      (_, i) => lastTime - (6 - i) * interval,
+    );
+    const showYear = isDataCrossYear(generatedTimestamps);
+
+    chartTimePoints = generatedTimestamps.map(ts =>
+      timestamp2string1(ts, dataExportDefaultTime, showYear),
     );
   }
 

+ 15 - 7
web/src/helpers/utils.jsx

@@ -217,15 +217,12 @@ export function timestamp2string(timestamp) {
   );
 }
 
-export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
+export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour', showYear = false) {
   let date = new Date(timestamp * 1000);
-  // let year = date.getFullYear().toString();
+  let year = date.getFullYear();
   let month = (date.getMonth() + 1).toString();
   let day = date.getDate().toString();
   let hour = date.getHours().toString();
-  if (day === '24') {
-    console.log('timestamp', timestamp);
-  }
   if (month.length === 1) {
     month = '0' + month;
   }
@@ -235,11 +232,13 @@ export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
   if (hour.length === 1) {
     hour = '0' + hour;
   }
-  let str = month + '-' + day;
+  // 仅在跨年时显示年份
+  let str = showYear ? year + '-' + month + '-' + day : month + '-' + day;
   if (dataExportDefaultTime === 'hour') {
     str += ' ' + hour + ':00';
   } else if (dataExportDefaultTime === 'week') {
     let nextWeek = new Date(timestamp * 1000 + 6 * 24 * 60 * 60 * 1000);
+    let nextWeekYear = nextWeek.getFullYear();
     let nextMonth = (nextWeek.getMonth() + 1).toString();
     let nextDay = nextWeek.getDate().toString();
     if (nextMonth.length === 1) {
@@ -248,11 +247,20 @@ export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
     if (nextDay.length === 1) {
       nextDay = '0' + nextDay;
     }
-    str += ' - ' + nextMonth + '-' + nextDay;
+    // 周视图结束日期也仅在跨年时显示年份
+    let nextStr = showYear ? nextWeekYear + '-' + nextMonth + '-' + nextDay : nextMonth + '-' + nextDay;
+    str += ' - ' + nextStr;
   }
   return str;
 }
 
+// 检查时间戳数组是否跨年
+export function isDataCrossYear(timestamps) {
+  if (!timestamps || timestamps.length === 0) return false;
+  const years = new Set(timestamps.map(ts => new Date(ts * 1000).getFullYear()));
+  return years.size > 1;
+}
+
 export function downloadTextAsFile(text, filename) {
   let blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
   let url = URL.createObjectURL(blob);