Line data Source code
1 : import { computed } from 'vue'
2 : import type { ComputedRef, Ref } from 'vue'
3 :
4 : import type { useExecutionStore } from '@/stores/executionStore'
5 : import type { TaskItemImpl, useQueueStore } from '@/stores/queueStore'
6 : import type { JobState } from '@/types/queue'
7 :
8 : type QueueStore = ReturnType<typeof useQueueStore>
9 : type ExecutionStore = ReturnType<typeof useExecutionStore>
10 :
11 : export type UseQueueEstimatesOptions = {
12 : queueStore: QueueStore
13 : executionStore: ExecutionStore
14 : taskForJob: ComputedRef<TaskItemImpl | null>
15 : jobState: ComputedRef<JobState | null>
16 : firstSeenTs: ComputedRef<number | undefined>
17 : jobsAhead: ComputedRef<number | null>
18 : nowTs: Ref<number>
19 : }
20 :
21 : type EstimateRange = [number, number]
22 :
23 2355 : export const formatElapsedTime = (ms: number): string => {
24 1 : const totalSec = Math.max(0, Math.floor(ms / 1000))
25 : const minutes = Math.floor(totalSec / 60)
26 : const seconds = totalSec % 60
27 1 : return `${minutes}m ${seconds}s`
28 : }
29 :
30 2355 : const pickRecentDurations = (queueStore: QueueStore) =>
31 : queueStore.historyTasks
32 : .map((task: TaskItemImpl) => Number(task.executionTimeInSeconds))
33 : .filter(
34 : (value: number | undefined) =>
35 : typeof value === 'number' && !Number.isNaN(value)
36 : )
37 :
38 2355 : export const useQueueEstimates = ({
39 : queueStore,
40 : executionStore,
41 : taskForJob,
42 : jobState,
43 : firstSeenTs,
44 : jobsAhead,
45 : nowTs
46 : }: UseQueueEstimatesOptions) => {
47 1 : const runningWorkflowCount = computed(
48 : () => executionStore.runningWorkflowCount
49 : )
50 :
51 1 : const showParallelQueuedStats = computed(
52 : () =>
53 : jobState.value === 'pending' &&
54 : !!firstSeenTs.value &&
55 : runningWorkflowCount.value > 1
56 : )
57 :
58 1 : const recentDurations = computed<number[]>(() =>
59 : pickRecentDurations(queueStore).slice(-20)
60 : )
61 :
62 1 : const runningRemainingRangeSeconds = computed<EstimateRange | null>(() => {
63 0 : const durations = recentDurations.value
64 0 : if (!durations.length) return null
65 0 : const sorted = durations.slice().sort((a, b) => a - b)
66 0 : const avg = sorted.reduce((sum, value) => sum + value, 0) / sorted.length
67 0 : const p75 =
68 : sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.75))]
69 0 : const running = queueStore.runningTasks
70 0 : const now = nowTs.value
71 0 : const remaining = running
72 : .map((task) => task.executionStartTimestamp)
73 : .filter((timestamp): timestamp is number => typeof timestamp === 'number')
74 : .map((startTs) => {
75 0 : const elapsed = Math.max(0, Math.floor((now - startTs) / 1000))
76 0 : return {
77 : lo: Math.max(0, Math.round(avg - elapsed)),
78 : hi: Math.max(0, Math.round(p75 - elapsed))
79 : }
80 : })
81 0 : if (!remaining.length) return null
82 : const minLo = remaining.reduce(
83 : (min, range) => Math.min(min, range.lo),
84 : Infinity
85 : )
86 : const minHi = remaining.reduce(
87 : (min, range) => Math.min(min, range.hi),
88 : Infinity
89 : )
90 0 : return [minLo, minHi]
91 : })
92 :
93 : const estimateRangeSeconds = computed<EstimateRange | null>(() => {
94 0 : const durations = recentDurations.value
95 0 : if (!durations.length) return null
96 0 : const ahead = jobsAhead.value
97 0 : if (ahead == null) return null
98 0 : const sorted = durations.slice().sort((a, b) => a - b)
99 0 : const avg = sorted.reduce((sum, value) => sum + value, 0) / sorted.length
100 0 : const p75 =
101 : sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.75))]
102 0 : if (ahead <= 0) {
103 0 : return runningRemainingRangeSeconds.value ?? [0, 0]
104 : }
105 0 : const runningCount = Math.max(1, runningWorkflowCount.value || 1)
106 0 : const batches = Math.ceil(ahead / runningCount)
107 0 : return [Math.round(avg * batches), Math.round(p75 * batches)]
108 : })
109 :
110 : const estimateRemainingRangeSeconds = computed<EstimateRange | null>(() => {
111 0 : const durations = recentDurations.value
112 0 : if (!durations.length) return null
113 0 : const sorted = durations.slice().sort((a, b) => a - b)
114 0 : const avg = sorted.reduce((sum, value) => sum + value, 0) / sorted.length
115 0 : const p75 =
116 : sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.75))]
117 : const execStart =
118 : jobState.value === 'running'
119 : ? taskForJob.value?.executionStartTimestamp
120 : : undefined
121 0 : const baseTs = execStart ?? firstSeenTs.value
122 0 : const elapsed = baseTs
123 : ? Math.max(0, Math.floor((nowTs.value - baseTs) / 1000))
124 : : 0
125 : const lo = Math.max(0, Math.round(avg - elapsed))
126 : const hi = Math.max(0, Math.round(p75 - elapsed))
127 0 : return [lo, hi]
128 : })
129 :
130 : const timeElapsedValue = computed(() => {
131 : const execStart =
132 : jobState.value === 'running'
133 : ? taskForJob.value?.executionStartTimestamp
134 : : undefined
135 0 : const baseTs = execStart ?? firstSeenTs.value
136 0 : if (!baseTs) return ''
137 0 : return formatElapsedTime(nowTs.value - baseTs)
138 : })
139 :
140 1 : return {
141 : runningWorkflowCount,
142 : showParallelQueuedStats,
143 : estimateRangeSeconds,
144 : estimateRemainingRangeSeconds,
145 : timeElapsedValue
146 : }
147 : }
|