From a195e8889609361bbc9de830ec19ae5662bf30fe Mon Sep 17 00:00:00 2001 From: CaIon Date: Thu, 1 Jan 2026 15:42:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20timestamp2string1?= =?UTF-8?q?=20=E8=B7=A8=E5=B9=B4=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E4=BB=85=E5=9C=A8=E6=95=B0=E6=8D=AE=E8=B7=A8=E5=B9=B4?= =?UTF-8?q?=E6=97=B6=E6=98=BE=E7=A4=BA=E5=B9=B4=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/helpers/dashboard.jsx | 22 +++++++++++++++++----- web/src/helpers/utils.jsx | 22 +++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/web/src/helpers/dashboard.jsx b/web/src/helpers/dashboard.jsx index 71278f495..8df375f11 100644 --- a/web/src/helpers/dashboard.jsx +++ b/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), ); } diff --git a/web/src/helpers/utils.jsx b/web/src/helpers/utils.jsx index 0cbdebd15..a54676e47 100644 --- a/web/src/helpers/utils.jsx +++ b/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);