import type { ParentHistoryItem } from '@/api/parent'
|
|
function pad2(value: number): string {
|
return String(value).padStart(2, '0')
|
}
|
|
export function formatStartedAt(ms?: number | null): string {
|
const value = Number(ms || 0)
|
if (!value)
|
return ''
|
const date = new Date(value)
|
if (Number.isNaN(date.getTime()))
|
return ''
|
return `${date.getMonth() + 1}/${date.getDate()} ${pad2(date.getHours())}:${pad2(date.getMinutes())}`
|
}
|
|
export function durationMsOf(item: Pick<ParentHistoryItem, 'durationMs' | 'startedAt' | 'endedAt' | 'status'>): number {
|
if (item.durationMs && item.durationMs > 0)
|
return item.durationMs
|
if (item.startedAt && item.endedAt && item.endedAt > item.startedAt)
|
return item.endedAt - item.startedAt
|
if (item.status === 'running' && item.startedAt)
|
return Math.max(0, Date.now() - item.startedAt)
|
return 0
|
}
|
|
export function formatDuration(ms: number, t: (key: string, params?: Record<string, string | number>) => string): string {
|
if (!ms || ms < 0)
|
return ''
|
const totalSec = Math.round(ms / 1000)
|
if (totalSec < 60)
|
return t('parentHistory.durationSec', { s: totalSec })
|
const minutes = Math.floor(totalSec / 60)
|
const seconds = totalSec % 60
|
return t('parentHistory.durationMin', { m: minutes, s: seconds })
|
}
|
|
export function statusLabel(
|
item: Pick<ParentHistoryItem, 'status' | 'endReason'>,
|
t: (key: string) => string,
|
): string {
|
if (item.status === 'running')
|
return t('parentHistory.status.running')
|
if (item.status === 'completed')
|
return t('parentHistory.status.completed')
|
if (item.endReason === 'preempted')
|
return t('parentHistory.status.preempted')
|
if (item.status === 'cancelled')
|
return t('parentHistory.status.cancelled')
|
return t('parentHistory.status.other')
|
}
|
|
export function sourceLabel(source: string | undefined, t: (key: string) => string): string {
|
if (source === 'mqtt_immediate')
|
return t('parentHistory.source.immediate')
|
if (source === 'schedule')
|
return t('parentHistory.source.schedule')
|
if (source === 'voice')
|
return t('parentHistory.source.voice')
|
return ''
|
}
|