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

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

View File

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

View File

@@ -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 date = new Date(timestamp * 1000);
// let year = date.getFullYear().toString(); let year = date.getFullYear();
let month = (date.getMonth() + 1).toString(); let month = (date.getMonth() + 1).toString();
let day = date.getDate().toString(); let day = date.getDate().toString();
let hour = date.getHours().toString(); let hour = date.getHours().toString();
if (day === '24') {
console.log('timestamp', timestamp);
}
if (month.length === 1) { if (month.length === 1) {
month = '0' + month; month = '0' + month;
} }
@@ -235,11 +232,13 @@ export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
if (hour.length === 1) { if (hour.length === 1) {
hour = '0' + hour; hour = '0' + hour;
} }
let str = month + '-' + day; // 仅在跨年时显示年份
let str = showYear ? year + '-' + month + '-' + day : month + '-' + day;
if (dataExportDefaultTime === 'hour') { if (dataExportDefaultTime === 'hour') {
str += ' ' + hour + ':00'; str += ' ' + hour + ':00';
} else if (dataExportDefaultTime === 'week') { } else if (dataExportDefaultTime === 'week') {
let nextWeek = new Date(timestamp * 1000 + 6 * 24 * 60 * 60 * 1000); let nextWeek = new Date(timestamp * 1000 + 6 * 24 * 60 * 60 * 1000);
let nextWeekYear = nextWeek.getFullYear();
let nextMonth = (nextWeek.getMonth() + 1).toString(); let nextMonth = (nextWeek.getMonth() + 1).toString();
let nextDay = nextWeek.getDate().toString(); let nextDay = nextWeek.getDate().toString();
if (nextMonth.length === 1) { if (nextMonth.length === 1) {
@@ -248,11 +247,20 @@ export function timestamp2string1(timestamp, dataExportDefaultTime = 'hour') {
if (nextDay.length === 1) { if (nextDay.length === 1) {
nextDay = '0' + nextDay; nextDay = '0' + nextDay;
} }
str += ' - ' + nextMonth + '-' + nextDay; // 周视图结束日期也仅在跨年时显示年份
let nextStr = showYear ? nextWeekYear + '-' + nextMonth + '-' + nextDay : nextMonth + '-' + nextDay;
str += ' - ' + nextStr;
} }
return str; 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) { export function downloadTextAsFile(text, filename) {
let blob = new Blob([text], { type: 'text/plain;charset=utf-8' }); let blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
let url = URL.createObjectURL(blob); let url = URL.createObjectURL(blob);