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

This commit is contained in:
CaIon
2026-01-01 15:42:15 +08:00
parent 87a75b0565
commit a3c1bc6350
2 changed files with 32 additions and 12 deletions

View File

@@ -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),
);
}