Line data Source code
1 : import type { JobModelDownload } from '@/stores/jobModelDownloadStore'
2 : import type { TaskItemImpl } from '@/stores/queueStore'
3 : import { resultItemUrl } from '@/utils/resultItemUrl'
4 : import { isImageResult } from '@/utils/resultItem'
5 : import type { JobState } from '@/types/queue'
6 : import { formatDuration, formatSize } from '@/utils/formatUtil'
7 : import { clampPercentInt, formatPercent0 } from '@/utils/numberUtil'
8 :
9 : export type BuildJobDisplayCtx = {
10 : t: (k: string, v?: Record<string, unknown>) => string
11 : locale: string
12 : formatClockTimeFn: (ts: number, locale: string) => string
13 : isActive: boolean
14 : totalPercent?: number
15 : currentNodePercent?: number
16 : currentNodeName?: string
17 : showAddedHint?: boolean
18 : /** Whether the app is running in cloud distribution */
19 : isCloud?: boolean
20 : /** The model file the job is downloading before it runs */
21 : modelDownload?: Omit<JobModelDownload, 'progress'>
22 : }
23 :
24 : type JobDisplay = {
25 : iconName: string
26 : iconImageUrl?: string
27 : primary: string
28 : secondary: string
29 : showClear: boolean
30 : }
31 :
32 2355 : export const iconForJobState = (state: JobState): string => {
33 136 : switch (state) {
34 : case 'pending':
35 63 : return 'icon-[lucide--loader-circle]'
36 : case 'initialization':
37 0 : return 'icon-[lucide--server-crash]'
38 : case 'running':
39 10 : return 'icon-[lucide--zap]'
40 : case 'completed':
41 31 : return 'icon-[lucide--check-check]'
42 : case 'failed':
43 32 : return 'icon-[lucide--alert-circle]'
44 : default:
45 0 : return 'icon-[lucide--circle]'
46 : }
47 : }
48 :
49 2355 : const buildTitle = (task: TaskItemImpl, t: (k: string) => string): string => {
50 0 : const prefix = t('g.job')
51 0 : const shortId = task.jobId.split('-')[0]
52 0 : const idx = task.job.priority
53 0 : if (typeof idx === 'number') return `${prefix} #${idx}`
54 0 : if (shortId) return `${prefix} ${shortId}`
55 0 : return prefix
56 : }
57 :
58 2355 : const buildQueuedTime = (
59 : task: TaskItemImpl,
60 : locale: string,
61 : formatClockTimeFn: (ts: number, locale: string) => string
62 : ): string => {
63 8 : const ts = task.createTime
64 8 : return formatClockTimeFn(ts, locale)
65 : }
66 :
67 2355 : export const buildJobDisplay = (
68 : task: TaskItemImpl,
69 : state: JobState,
70 : ctx: BuildJobDisplayCtx
71 : ): JobDisplay => {
72 81 : if (ctx.modelDownload) {
73 0 : const { name, index, count, bytesDownloaded, bytesTotal } =
74 : ctx.modelDownload
75 0 : return {
76 : iconName: 'icon-[lucide--download]',
77 : primary:
78 : count > 1
79 : ? ctx.t('queue.downloadingModelOf', { name, index, count })
80 : : ctx.t('queue.downloadingModel', { name }),
81 : secondary:
82 : bytesTotal === null
83 : ? formatSize(bytesDownloaded)
84 : : ctx.t('queue.downloadedOfTotal', {
85 : downloaded: formatSize(bytesDownloaded),
86 : total: formatSize(bytesTotal)
87 : }),
88 : showClear: true
89 : }
90 : }
91 81 : if (state === 'pending') {
92 8 : if (ctx.showAddedHint) {
93 8 : return {
94 : iconName: 'icon-[lucide--check]',
95 : primary: ctx.t('queue.jobAddedToQueue'),
96 : secondary: buildQueuedTime(task, ctx.locale, ctx.formatClockTimeFn),
97 : showClear: true
98 : }
99 : }
100 0 : return {
101 : iconName: iconForJobState(state),
102 : primary: ctx.t('queue.inQueue'),
103 : secondary: buildQueuedTime(task, ctx.locale, ctx.formatClockTimeFn),
104 : showClear: true
105 : }
106 : }
107 81 : if (state === 'initialization') {
108 0 : return {
109 : iconName: iconForJobState(state),
110 : primary: ctx.t('queue.initializingAlmostReady'),
111 : secondary: buildQueuedTime(task, ctx.locale, ctx.formatClockTimeFn),
112 : showClear: true
113 : }
114 : }
115 81 : if (state === 'running') {
116 10 : if (ctx.isActive) {
117 0 : const total = formatPercent0(
118 : ctx.locale,
119 : clampPercentInt(ctx.totalPercent)
120 : )
121 0 : const curr = formatPercent0(
122 : ctx.locale,
123 : clampPercentInt(ctx.currentNodePercent)
124 : )
125 0 : const primary = ctx.t('sideToolbar.queueProgressOverlay.total', {
126 : percent: total
127 : })
128 0 : const right = ctx.currentNodeName
129 : ? `${ctx.currentNodeName} ${ctx.t(
130 : 'sideToolbar.queueProgressOverlay.colonPercent',
131 : { percent: curr }
132 : )}`
133 : : ''
134 0 : return {
135 : iconName: iconForJobState(state),
136 : primary,
137 : secondary: right,
138 : showClear: true
139 : }
140 : }
141 10 : return {
142 : iconName: iconForJobState(state),
143 : primary: ctx.t('g.running'),
144 : secondary: '',
145 : showClear: true
146 : }
147 : }
148 81 : if (state === 'completed') {
149 31 : const time = task.executionTimeInSeconds
150 31 : const preview = task.previewOutput
151 31 : const iconImageUrl =
152 : preview && isImageResult(preview) ? resultItemUrl(preview) : undefined
153 :
154 : // Cloud shows "Completed in Xh Ym Zs", non-cloud shows filename
155 31 : const primary = ctx.isCloud
156 : ? ctx.t('queue.completedIn', {
157 : duration: formatDuration(task.executionTime ?? 0)
158 : })
159 : : preview?.filename && preview.filename.length
160 : ? preview.filename
161 : : buildTitle(task, ctx.t)
162 :
163 31 : return {
164 : iconName: iconForJobState(state),
165 : iconImageUrl,
166 : primary,
167 : secondary: time !== undefined ? `${time.toFixed(2)}s` : '',
168 : showClear: false
169 : }
170 : }
171 32 : return {
172 : iconName: iconForJobState(state),
173 : primary: ctx.t('g.failed'),
174 : secondary: ctx.t('g.failed'),
175 : showClear: true
176 : }
177 : }
|