wangzhibo
10 天以前 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 ''
}