Merge pull request #289 from AAEE86/rust

fix(usage): 统一使用记录与仪表盘的缓存命中率计算口径
This commit is contained in:
fawney19
2026-04-12 16:12:22 +08:00
committed by GitHub
33 changed files with 3333 additions and 456 deletions

View File

@@ -197,7 +197,7 @@ export const MOCK_DASHBOARD_STATS: DashboardStatsResponse = {
cache_read_tokens: 200000,
cache_creation_cost: 0.25,
cache_read_cost: 0.10,
cache_hit_rate: 0.35,
cache_hit_rate: 35.0,
total_cache_tokens: 250000
},
users: {

View File

@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'
import { parseDateLike } from '../date'
describe('parseDateLike', () => {
it('parses date-only strings as local calendar dates', () => {
const date = parseDateLike('2026-04-12')
expect(date.getFullYear()).toBe(2026)
expect(date.getMonth()).toBe(3)
expect(date.getDate()).toBe(12)
})
it('keeps timestamp strings delegated to native Date parsing', () => {
const date = parseDateLike('2026-04-12T15:30:00Z')
expect(Number.isNaN(date.getTime())).toBe(false)
expect(date.toISOString()).toBe('2026-04-12T15:30:00.000Z')
})
})

View File

@@ -0,0 +1,15 @@
const DATE_ONLY_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/
/**
* 将 `YYYY-MM-DD` 解析为本地时区日期,避免浏览器按 UTC 解析后串天。
* 其他带时间/时区的信息仍交给原生 Date 处理。
*/
export function parseDateLike(dateString: string): Date {
const matched = DATE_ONLY_PATTERN.exec(dateString)
if (!matched) {
return new Date(dateString)
}
const [, year, month, day] = matched
return new Date(Number(year), Number(month) - 1, Number(day))
}

View File

@@ -819,6 +819,7 @@ import {
DollarSign,
Key,
Hash,
Zap,
Bell,
AlertCircle,
AlertTriangle,
@@ -830,6 +831,7 @@ import {
Shuffle
} from 'lucide-vue-next'
import { formatTokens, formatCurrency } from '@/utils/format'
import { parseDateLike } from '@/utils/date'
import { marked } from 'marked'
import { sanitizeMarkdown } from '@/utils/sanitize'
import type { ChartData, ChartOptions, ChartDataset, TooltipItem } from 'chart.js'
@@ -1000,7 +1002,7 @@ const selectedAnnouncement = ref<Announcement | null>(null)
const detailDialogOpen = ref(false)
const iconMap: Record<string, unknown> = {
Users, Activity, TrendingUp, DollarSign, Key, Hash, Database
Users, Activity, TrendingUp, DollarSign, Key, Hash, Zap, Database
}
// 空状态占位卡片
@@ -1315,7 +1317,10 @@ onBeforeUnmount(() => {
async function loadDashboardData() {
loading.value = true
try {
const statsData = await dashboardApi.getStats()
const statsData = await dashboardApi.getStats({
timezone: dailyTimeRange.value.timezone,
tz_offset_minutes: dailyTimeRange.value.tz_offset_minutes
})
stats.value = statsData.stats.map(stat => ({
...stat,
icon: iconMap[stat.icon] || Activity
@@ -1382,7 +1387,7 @@ function scheduleDailyStatsLoad() {
watch(dailyTimeRange, scheduleDailyStatsLoad, { deep: true })
function formatDate(dateString: string): string {
const date = new Date(dateString)
const date = parseDateLike(dateString)
const today = new Date()
const yesterday = new Date(today)
yesterday.setDate(yesterday.getDate() - 1)
@@ -1392,7 +1397,7 @@ function formatDate(dateString: string): string {
}
function formatDateForChart(dateString: string): string {
const date = new Date(dateString)
const date = parseDateLike(dateString)
const today = new Date()
const yesterday = new Date(today)
yesterday.setDate(yesterday.getDate() - 1)