mirror of
https://github.com/fawney19/Aether.git
synced 2026-01-02 15:52:26 +08:00
refactor(frontend): 优化工具函数和 mock handler
- 更新 format.ts 工具函数 - 调整 mock handler 和 useDateRange composable
This commit is contained in:
@@ -47,7 +47,7 @@ export function getDateRangeFromPeriod(period: PeriodValue): DateRangeParams {
|
|||||||
*/
|
*/
|
||||||
export function formatDateTime(dateStr: string): string {
|
export function formatDateTime(dateStr: string): string {
|
||||||
// 后端返回的是 UTC 时间但没有时区标识,需要手动添加 'Z'
|
// 后端返回的是 UTC 时间但没有时区标识,需要手动添加 'Z'
|
||||||
const utcDateStr = dateStr.includes('Z') || dateStr.includes('+') ? dateStr : dateStr + 'Z'
|
const utcDateStr = dateStr.includes('Z') || dateStr.includes('+') ? dateStr : `${dateStr }Z`
|
||||||
const date = new Date(utcDateStr)
|
const date = new Date(utcDateStr)
|
||||||
|
|
||||||
// 只显示时分秒
|
// 只显示时分秒
|
||||||
|
|||||||
@@ -384,7 +384,7 @@ function generateMockUsageRecords(count: number = 100) {
|
|||||||
is_stream: apiFormat.includes('CLI'),
|
is_stream: apiFormat.includes('CLI'),
|
||||||
status_code: status === 'failed' ? [500, 502, 429, 400][Math.floor(Math.random() * 4)] : 200,
|
status_code: status === 'failed' ? [500, 502, 429, 400][Math.floor(Math.random() * 4)] : 200,
|
||||||
error_message: status === 'failed' ? ['Rate limit exceeded', 'Internal server error', 'Model overloaded'][Math.floor(Math.random() * 3)] : undefined,
|
error_message: status === 'failed' ? ['Rate limit exceeded', 'Internal server error', 'Model overloaded'][Math.floor(Math.random() * 3)] : undefined,
|
||||||
status: status,
|
status,
|
||||||
created_at: createdAt.toISOString(),
|
created_at: createdAt.toISOString(),
|
||||||
has_fallback: Math.random() > 0.9,
|
has_fallback: Math.random() > 0.9,
|
||||||
request_metadata: model.provider === 'google' ? { model_version: 'gemini-3-pro-preview-2025-01' } : undefined
|
request_metadata: model.provider === 'google' ? { model_version: 'gemini-3-pro-preview-2025-01' } : undefined
|
||||||
|
|||||||
@@ -13,22 +13,22 @@ export function formatTokens(num: number | undefined | null): string {
|
|||||||
if (num < 1000000) {
|
if (num < 1000000) {
|
||||||
const thousands = num / 1000
|
const thousands = num / 1000
|
||||||
if (thousands >= 100) {
|
if (thousands >= 100) {
|
||||||
return Math.round(thousands) + 'K'
|
return `${Math.round(thousands) }K`
|
||||||
} else if (thousands >= 10) {
|
} else if (thousands >= 10) {
|
||||||
return thousands.toFixed(1) + 'K'
|
return `${thousands.toFixed(1) }K`
|
||||||
} else {
|
} else {
|
||||||
return thousands.toFixed(2) + 'K'
|
return `${thousands.toFixed(2) }K`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For values >= 1M, show in millions
|
// For values >= 1M, show in millions
|
||||||
const millions = num / 1000000
|
const millions = num / 1000000
|
||||||
if (millions >= 100) {
|
if (millions >= 100) {
|
||||||
return Math.round(millions) + 'M'
|
return `${Math.round(millions) }M`
|
||||||
} else if (millions >= 10) {
|
} else if (millions >= 10) {
|
||||||
return millions.toFixed(1) + 'M'
|
return `${millions.toFixed(1) }M`
|
||||||
} else {
|
} else {
|
||||||
return millions.toFixed(2) + 'M'
|
return `${millions.toFixed(2) }M`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ export function formatCurrency(amount: number | undefined | null): string {
|
|||||||
const formatted = amount.toFixed(8)
|
const formatted = amount.toFixed(8)
|
||||||
// Remove trailing zeros but keep at least 2 decimal places
|
// Remove trailing zeros but keep at least 2 decimal places
|
||||||
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
||||||
return '$' + trimmed
|
return `$${ trimmed}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// For small amounts (< $0.0001), show up to 6 decimal places
|
// For small amounts (< $0.0001), show up to 6 decimal places
|
||||||
@@ -51,7 +51,7 @@ export function formatCurrency(amount: number | undefined | null): string {
|
|||||||
const formatted = amount.toFixed(6)
|
const formatted = amount.toFixed(6)
|
||||||
// Remove trailing zeros but keep at least 2 decimal places
|
// Remove trailing zeros but keep at least 2 decimal places
|
||||||
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
||||||
return '$' + trimmed
|
return `$${ trimmed}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// For small amounts (< $0.01), show up to 5 decimal places
|
// For small amounts (< $0.01), show up to 5 decimal places
|
||||||
@@ -59,7 +59,7 @@ export function formatCurrency(amount: number | undefined | null): string {
|
|||||||
const formatted = amount.toFixed(5)
|
const formatted = amount.toFixed(5)
|
||||||
// Remove trailing zeros but keep at least 2 decimal places
|
// Remove trailing zeros but keep at least 2 decimal places
|
||||||
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
||||||
return '$' + trimmed
|
return `$${ trimmed}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// For amounts less than $1, show 4 decimal places
|
// For amounts less than $1, show 4 decimal places
|
||||||
@@ -67,7 +67,7 @@ export function formatCurrency(amount: number | undefined | null): string {
|
|||||||
const formatted = amount.toFixed(4)
|
const formatted = amount.toFixed(4)
|
||||||
// Remove trailing zeros but keep at least 2 decimal places
|
// Remove trailing zeros but keep at least 2 decimal places
|
||||||
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
||||||
return '$' + trimmed
|
return `$${ trimmed}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// For amounts $1-$100, show 2-3 decimal places
|
// For amounts $1-$100, show 2-3 decimal places
|
||||||
@@ -75,11 +75,11 @@ export function formatCurrency(amount: number | undefined | null): string {
|
|||||||
const formatted = amount.toFixed(3)
|
const formatted = amount.toFixed(3)
|
||||||
// Remove trailing zeros but keep at least 2 decimal places
|
// Remove trailing zeros but keep at least 2 decimal places
|
||||||
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
const trimmed = formatted.replace(/(\.\d\d)0+$/, '$1')
|
||||||
return '$' + trimmed
|
return `$${ trimmed}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// For larger amounts, show 2 decimal places
|
// For larger amounts, show 2 decimal places
|
||||||
return '$' + amount.toFixed(2)
|
return `$${ amount.toFixed(2)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Number formatting with locale support
|
// Number formatting with locale support
|
||||||
@@ -111,9 +111,9 @@ export function formatModelPrice(price: number | undefined | null): string {
|
|||||||
|
|
||||||
// Price is already per 1M tokens, no conversion needed
|
// Price is already per 1M tokens, no conversion needed
|
||||||
if (price < 1) {
|
if (price < 1) {
|
||||||
return '$' + price.toFixed(4).replace(/\.?0+$/, '').padEnd(price.toFixed(4).indexOf('.') + 3, '0')
|
return `$${ price.toFixed(4).replace(/\.?0+$/, '').padEnd(price.toFixed(4).indexOf('.') + 3, '0')}`
|
||||||
} else {
|
} else {
|
||||||
return '$' + price.toFixed(2)
|
return `$${ price.toFixed(2)}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user