2026-07-11 21:50:34 +08:00
|
|
|
import type { CostUsageSummary } from "../types/cost"
|
|
|
|
|
import type { GenerationJob } from "../types/job"
|
|
|
|
|
|
2026-07-20 17:57:02 +08:00
|
|
|
const BACKEND_NAIVE_UTC_TIMESTAMP = /^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?$/
|
|
|
|
|
const TERMINAL_JOB_STATUSES = new Set(["completed", "succeeded", "failed", "cancelled", "dead_letter", "superseded"])
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
export function normalizeStatus(status: string): string {
|
|
|
|
|
return String(status || "").trim().toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function statusLabel(status: string): string {
|
|
|
|
|
const labels: Record<string, string> = {
|
|
|
|
|
running: "运行中",
|
|
|
|
|
degraded: "降级",
|
|
|
|
|
stopped: "已停止",
|
|
|
|
|
loading: "加载中",
|
|
|
|
|
created: "已创建",
|
|
|
|
|
pending: "等待中",
|
|
|
|
|
queued: "排队中",
|
|
|
|
|
completed: "已完成",
|
|
|
|
|
succeeded: "已成功",
|
|
|
|
|
failed: "失败",
|
|
|
|
|
cancelled: "已取消",
|
|
|
|
|
dead_letter: "死信",
|
|
|
|
|
retrying: "重试执行中",
|
|
|
|
|
superseded: "已被重试替代",
|
|
|
|
|
}
|
|
|
|
|
return labels[normalizeStatus(status)] ?? status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function statusClass(status: string): "is-bad" | "is-warn" | "is-good" | "is-idle" {
|
|
|
|
|
const normalized = normalizeStatus(status)
|
|
|
|
|
if (["failed", "dead_letter", "cancelled"].includes(normalized)) return "is-bad"
|
|
|
|
|
if (["running", "queued", "created", "pending", "retrying"].includes(normalized)) return "is-warn"
|
|
|
|
|
if (["completed", "succeeded"].includes(normalized)) return "is-good"
|
|
|
|
|
return "is-idle"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatBytes(value: number): string {
|
|
|
|
|
const bytes = Number.isFinite(value) && value > 0 ? value : 0
|
|
|
|
|
if (bytes < 1024) return `${bytes} B`
|
|
|
|
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
|
|
|
|
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`
|
|
|
|
|
return `${(bytes / 1024 / 1024 / 1024).toFixed(1)} GB`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatCostEstimate(summary: CostUsageSummary): string {
|
|
|
|
|
const amount = Number(summary.totals.estimated_cost)
|
|
|
|
|
const currency = summary.currency || "USD"
|
|
|
|
|
try {
|
|
|
|
|
return `${new Intl.NumberFormat("zh-CN", {
|
|
|
|
|
style: "currency",
|
|
|
|
|
currency,
|
|
|
|
|
minimumFractionDigits: amount < 1 ? 4 : 2,
|
|
|
|
|
maximumFractionDigits: amount < 1 ? 6 : 2,
|
|
|
|
|
}).format(amount)} 估算`
|
|
|
|
|
} catch {
|
|
|
|
|
return `${currency} ${amount.toFixed(amount < 1 ? 6 : 2)} 估算`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatTime(value?: string): string {
|
|
|
|
|
if (!value) return "-"
|
2026-07-20 17:57:02 +08:00
|
|
|
const parsed = timestamp(value)
|
|
|
|
|
if (!parsed) return value
|
|
|
|
|
const date = new Date(parsed)
|
2026-07-11 21:50:34 +08:00
|
|
|
if (Number.isNaN(date.getTime())) return value
|
|
|
|
|
return new Intl.DateTimeFormat("zh-CN", {
|
|
|
|
|
month: "2-digit",
|
|
|
|
|
day: "2-digit",
|
|
|
|
|
hour: "2-digit",
|
|
|
|
|
minute: "2-digit",
|
|
|
|
|
}).format(date)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function timestamp(value: string): number {
|
2026-07-20 17:57:02 +08:00
|
|
|
const text = String(value || "").trim()
|
|
|
|
|
const normalized = BACKEND_NAIVE_UTC_TIMESTAMP.test(text)
|
|
|
|
|
? `${text.replace(" ", "T")}Z`
|
|
|
|
|
: text
|
|
|
|
|
const parsed = Date.parse(normalized)
|
2026-07-11 21:50:34 +08:00
|
|
|
return Number.isFinite(parsed) ? parsed : 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function waitTime(job: GenerationJob): string {
|
|
|
|
|
const start = timestamp(job.created_at)
|
|
|
|
|
if (!start) return "-"
|
2026-07-20 17:57:02 +08:00
|
|
|
const status = normalizeStatus(job.status)
|
|
|
|
|
const terminal = TERMINAL_JOB_STATUSES.has(status)
|
|
|
|
|
const persistedEnd = terminal ? timestamp(job.updated_at) : 0
|
|
|
|
|
const elapsedSeconds = Math.max(0, Math.round(((persistedEnd || Date.now()) - start) / 1000))
|
|
|
|
|
const elapsed = formatDurationSeconds(elapsedSeconds)
|
|
|
|
|
const timeout = jobTimeoutSeconds(job)
|
|
|
|
|
return timeout ? `${elapsed} / 上限 ${formatDurationSeconds(timeout)}` : elapsed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function jobTimeoutSeconds(job: GenerationJob): number | undefined {
|
|
|
|
|
const requestValue = job.request?.timeout_seconds
|
|
|
|
|
const metadataValue = job.metadata?.timeout_seconds
|
|
|
|
|
const value = requestValue ?? metadataValue
|
|
|
|
|
const timeout = Number(value)
|
|
|
|
|
if (!Number.isFinite(timeout) || timeout <= 0) return undefined
|
|
|
|
|
return Math.round(timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatDurationSeconds(value: number): string {
|
|
|
|
|
const seconds = Math.max(0, Math.round(value))
|
|
|
|
|
if (seconds < 60) return seconds < 1 ? "刚刚" : `${seconds} 秒`
|
|
|
|
|
const minutes = Math.round(seconds / 60)
|
2026-07-11 21:50:34 +08:00
|
|
|
if (minutes < 60) return `${minutes} 分钟`
|
2026-07-20 17:57:02 +08:00
|
|
|
const hours = Math.floor(minutes / 60)
|
|
|
|
|
const remainder = minutes % 60
|
|
|
|
|
return remainder ? `${hours} 小时 ${remainder} 分钟` : `${hours} 小时`
|
2026-07-11 21:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function shortId(value: string): string {
|
|
|
|
|
return value.length > 10 ? `${value.slice(0, 6)}...${value.slice(-4)}` : value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function progressValue(job: GenerationJob): number {
|
|
|
|
|
const value = Number(job.progress)
|
|
|
|
|
if (!Number.isFinite(value)) return 0
|
|
|
|
|
return Math.max(0, Math.min(100, value))
|
|
|
|
|
}
|