/
queue-response.ts
107 lines (85 loc) · 2.26 KB
/
queue-response.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { pessimisticThreshold } from '../utils';
export interface QueueItem {
_class: string;
blocked: boolean;
buildable: boolean;
id: number;
inQueueSince: number;
params: string;
task: Task;
url: string;
estimatedDuration: number;
stuck: boolean;
cancelled: boolean;
}
export interface Task {
_class: string;
name: string;
url: string;
color: string;
}
export interface Executable {
_class: string;
number: number;
url: string;
}
export interface QueueItemQuiet extends QueueItem {
why: string;
timestamp: number;
}
export function isQueueItemQuiet(
response: QueueItem,
): response is QueueItemQuiet {
return typeof (response as QueueItemQuiet).timestamp === 'number';
}
export interface QueueItemQueued extends QueueItem {
why: string;
buildableStartMilliseconds: number;
}
export function isQueueItemQueued(
response: QueueItem,
): response is QueueItemQueued {
return (
typeof (response as QueueItemQueued).buildableStartMilliseconds === 'number'
);
}
export interface QueueItemDone extends QueueItem {
cancelled: boolean;
executable: Executable;
}
export function isQueueItemDone(
response: QueueItem,
): response is QueueItemDone {
return !!(response as QueueItemDone).executable;
}
export function getQueueItemRemainingDuration(response: QueueItem): number {
let milliseconds = 0;
if (isQueueItemQuiet(response)) {
milliseconds = response.timestamp - +new Date();
} else if (isQueueItemQueued(response)) {
milliseconds = parseQueuedEstimatedDuration(response.why);
}
if (milliseconds < 0) {
milliseconds = 0;
}
return milliseconds + pessimisticThreshold;
}
function parseQueuedEstimatedDuration(why: string): number {
let milliseconds = 0;
const startIndex = why.indexOf('(ETA:');
const endIndex = why.indexOf(')');
if (startIndex === -1 || endIndex === -1) {
return milliseconds;
}
const time: string = why.slice(startIndex + 5, endIndex);
const times: string[] = time.split(' ');
const minutesIndex = times.indexOf('min');
if (minutesIndex !== -1) {
milliseconds += +times[minutesIndex - 1] * 60 * 1000;
}
const secondsIndex = times.indexOf('sec');
if (secondsIndex !== -1) {
milliseconds += +times[secondsIndex - 1] * 1000;
}
return milliseconds;
}