mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 00:53:33 +00:00
Merge branch 'dev' into main
合并dev分支的时区修复和其他改进: - 修复仪表板图表时区处理问题 - 修复自定义时间选择器的UTC转换 - 删除dist构建产物目录
This commit is contained in:
@@ -64,7 +64,7 @@ router.get('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
// 今日 - 使用时区日期
|
||||
const redis = require('../models/redis');
|
||||
const tzDate = redis.getDateInTimezone(now);
|
||||
const dateStr = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}-${String(tzDate.getDate()).padStart(2, '0')}`;
|
||||
const dateStr = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}-${String(tzDate.getUTCDate()).padStart(2, '0')}`;
|
||||
searchPatterns.push(`usage:daily:*:${dateStr}`);
|
||||
} else if (timeRange === '7days') {
|
||||
// 最近7天
|
||||
@@ -73,14 +73,14 @@ router.get('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
const date = new Date(now);
|
||||
date.setDate(date.getDate() - i);
|
||||
const tzDate = redis.getDateInTimezone(date);
|
||||
const dateStr = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}-${String(tzDate.getDate()).padStart(2, '0')}`;
|
||||
const dateStr = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}-${String(tzDate.getUTCDate()).padStart(2, '0')}`;
|
||||
searchPatterns.push(`usage:daily:*:${dateStr}`);
|
||||
}
|
||||
} else if (timeRange === 'monthly') {
|
||||
// 本月
|
||||
const redis = require('../models/redis');
|
||||
const tzDate = redis.getDateInTimezone(now);
|
||||
const currentMonth = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}`;
|
||||
const currentMonth = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}`;
|
||||
searchPatterns.push(`usage:monthly:*:${currentMonth}`);
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ router.get('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
const redis = require('../models/redis');
|
||||
const tzToday = redis.getDateStringInTimezone(now);
|
||||
const tzDate = redis.getDateInTimezone(now);
|
||||
const tzMonth = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}`;
|
||||
const tzMonth = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}`;
|
||||
|
||||
const modelKeys = timeRange === 'today'
|
||||
? await client.keys(`usage:${apiKey.id}:model:daily:*:${tzToday}`)
|
||||
@@ -1321,7 +1321,8 @@ router.get('/dashboard', authenticateAdmin, async (req, res) => {
|
||||
claudeAccountsHealthy: activeClaudeAccounts > 0,
|
||||
geminiAccountsHealthy: activeGeminiAccounts > 0,
|
||||
uptime: process.uptime()
|
||||
}
|
||||
},
|
||||
systemTimezone: config.system.timezoneOffset || 8
|
||||
};
|
||||
|
||||
res.json({ success: true, data: dashboard });
|
||||
@@ -1356,8 +1357,9 @@ router.get('/usage-stats', authenticateAdmin, async (req, res) => {
|
||||
router.get('/model-stats', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { period = 'daily' } = req.query; // daily, monthly
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const currentMonth = `${new Date().getFullYear()}-${String(new Date().getMonth() + 1).padStart(2, '0')}`;
|
||||
const today = redis.getDateStringInTimezone();
|
||||
const tzDate = redis.getDateInTimezone();
|
||||
const currentMonth = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}`;
|
||||
|
||||
logger.info(`📊 Getting global model stats, period: ${period}, today: ${today}, currentMonth: ${currentMonth}`);
|
||||
|
||||
@@ -1479,6 +1481,14 @@ router.get('/usage-trend', authenticateAdmin, async (req, res) => {
|
||||
// 使用自定义时间范围
|
||||
startTime = new Date(startDate);
|
||||
endTime = new Date(endDate);
|
||||
|
||||
// 调试日志
|
||||
logger.info(`📊 Usage trend hour granularity - received times:`);
|
||||
logger.info(` startDate (raw): ${startDate}`);
|
||||
logger.info(` endDate (raw): ${endDate}`);
|
||||
logger.info(` startTime (parsed): ${startTime.toISOString()}`);
|
||||
logger.info(` endTime (parsed): ${endTime.toISOString()}`);
|
||||
logger.info(` System timezone offset: ${config.system.timezoneOffset || 8}`);
|
||||
} else {
|
||||
// 默认最近24小时
|
||||
endTime = new Date();
|
||||
@@ -1498,8 +1508,11 @@ router.get('/usage-trend', authenticateAdmin, async (req, res) => {
|
||||
currentHour.setMinutes(0, 0, 0);
|
||||
|
||||
while (currentHour <= endTime) {
|
||||
const dateStr = currentHour.toISOString().split('T')[0];
|
||||
const hour = String(currentHour.getHours()).padStart(2, '0');
|
||||
// 注意:前端发送的时间已经是UTC时间,不需要再次转换
|
||||
// 直接从currentHour生成对应系统时区的日期和小时
|
||||
const tzCurrentHour = redis.getDateInTimezone(currentHour);
|
||||
const dateStr = redis.getDateStringInTimezone(currentHour);
|
||||
const hour = String(tzCurrentHour.getUTCHours()).padStart(2, '0');
|
||||
const hourKey = `${dateStr}:${hour}`;
|
||||
|
||||
// 获取当前小时的模型统计数据
|
||||
@@ -1570,9 +1583,16 @@ router.get('/usage-trend', authenticateAdmin, async (req, res) => {
|
||||
hourCost = costResult.costs.total;
|
||||
}
|
||||
|
||||
// 格式化时间标签 - 使用系统时区的显示
|
||||
const tzDateForLabel = redis.getDateInTimezone(currentHour);
|
||||
const month = String(tzDateForLabel.getUTCMonth() + 1).padStart(2, '0');
|
||||
const day = String(tzDateForLabel.getUTCDate()).padStart(2, '0');
|
||||
const hourStr = String(tzDateForLabel.getUTCHours()).padStart(2, '0');
|
||||
|
||||
trendData.push({
|
||||
date: hourKey,
|
||||
hour: currentHour.toISOString(),
|
||||
// 对于小时粒度,只返回hour字段,不返回date字段
|
||||
hour: currentHour.toISOString(), // 保留原始ISO时间用于排序
|
||||
label: `${month}/${day} ${hourStr}:00`, // 添加格式化的标签
|
||||
inputTokens: hourInputTokens,
|
||||
outputTokens: hourOutputTokens,
|
||||
requests: hourRequests,
|
||||
@@ -1595,7 +1615,7 @@ router.get('/usage-trend', authenticateAdmin, async (req, res) => {
|
||||
for (let i = 0; i < daysCount; i++) {
|
||||
const date = new Date(today);
|
||||
date.setDate(date.getDate() - i);
|
||||
const dateStr = date.toISOString().split('T')[0];
|
||||
const dateStr = redis.getDateStringInTimezone(date);
|
||||
|
||||
// 汇总当天所有API Key的使用数据
|
||||
const pattern = `usage:daily:*:${dateStr}`;
|
||||
@@ -1711,8 +1731,9 @@ router.get('/api-keys/:keyId/model-stats', authenticateAdmin, async (req, res) =
|
||||
logger.info(`📊 Getting model stats for API key: ${keyId}, period: ${period}, startDate: ${startDate}, endDate: ${endDate}`);
|
||||
|
||||
const client = redis.getClientSafe();
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const currentMonth = `${new Date().getFullYear()}-${String(new Date().getMonth() + 1).padStart(2, '0')}`;
|
||||
const today = redis.getDateStringInTimezone();
|
||||
const tzDate = redis.getDateInTimezone();
|
||||
const currentMonth = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}`;
|
||||
|
||||
let searchPatterns = [];
|
||||
|
||||
@@ -1734,7 +1755,7 @@ router.get('/api-keys/:keyId/model-stats', authenticateAdmin, async (req, res) =
|
||||
|
||||
// 生成日期范围内所有日期的搜索模式
|
||||
for (let d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) {
|
||||
const dateStr = d.toISOString().split('T')[0];
|
||||
const dateStr = redis.getDateStringInTimezone(d);
|
||||
searchPatterns.push(`usage:${keyId}:model:daily:*:${dateStr}`);
|
||||
}
|
||||
|
||||
@@ -1928,14 +1949,25 @@ router.get('/api-keys-usage-trend', authenticateAdmin, async (req, res) => {
|
||||
currentHour.setMinutes(0, 0, 0);
|
||||
|
||||
while (currentHour <= endTime) {
|
||||
const hourKey = currentHour.toISOString().split(':')[0].replace('T', ':');
|
||||
// 使用时区转换后的时间来生成键
|
||||
const tzCurrentHour = redis.getDateInTimezone(currentHour);
|
||||
const dateStr = redis.getDateStringInTimezone(currentHour);
|
||||
const hour = String(tzCurrentHour.getUTCHours()).padStart(2, '0');
|
||||
const hourKey = `${dateStr}:${hour}`;
|
||||
|
||||
// 获取这个小时所有API Key的数据
|
||||
const pattern = `usage:hourly:*:${hourKey}`;
|
||||
const keys = await client.keys(pattern);
|
||||
|
||||
// 格式化时间标签
|
||||
const tzDateForLabel = redis.getDateInTimezone(currentHour);
|
||||
const monthLabel = String(tzDateForLabel.getUTCMonth() + 1).padStart(2, '0');
|
||||
const dayLabel = String(tzDateForLabel.getUTCDate()).padStart(2, '0');
|
||||
const hourLabel = String(tzDateForLabel.getUTCHours()).padStart(2, '0');
|
||||
|
||||
const hourData = {
|
||||
hour: currentHour.toISOString(),
|
||||
hour: currentHour.toISOString(), // 使用原始时间,不进行时区转换
|
||||
label: `${monthLabel}/${dayLabel} ${hourLabel}:00`, // 添加格式化的标签
|
||||
apiKeys: {}
|
||||
};
|
||||
|
||||
@@ -1973,7 +2005,7 @@ router.get('/api-keys-usage-trend', authenticateAdmin, async (req, res) => {
|
||||
for (let i = 0; i < daysCount; i++) {
|
||||
const date = new Date(today);
|
||||
date.setDate(date.getDate() - i);
|
||||
const dateStr = date.toISOString().split('T')[0];
|
||||
const dateStr = redis.getDateStringInTimezone(date);
|
||||
|
||||
// 获取这一天所有API Key的数据
|
||||
const pattern = `usage:daily:*:${dateStr}`;
|
||||
@@ -2065,8 +2097,9 @@ router.get('/usage-costs', authenticateAdmin, async (req, res) => {
|
||||
|
||||
// 按模型统计费用
|
||||
const client = redis.getClientSafe();
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const currentMonth = `${new Date().getFullYear()}-${String(new Date().getMonth() + 1).padStart(2, '0')}`;
|
||||
const today = redis.getDateStringInTimezone();
|
||||
const tzDate = redis.getDateInTimezone();
|
||||
const currentMonth = `${tzDate.getUTCFullYear()}-${String(tzDate.getUTCMonth() + 1).padStart(2, '0')}`;
|
||||
|
||||
let pattern;
|
||||
if (period === 'today') {
|
||||
|
||||
Reference in New Issue
Block a user