-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_older_than.ts
66 lines (59 loc) · 1.93 KB
/
process_older_than.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
const durationUnitToMs = {
s: 1000,
m: 60000,
h: 3600000,
d: 86400000,
w: 604800000,
y: 31536000000,
}
export type WorkflowRun = {
conclusion: string | null,
created_at: string,
id: number,
name?: string | null,
workflow_id: number,
}
type WorkflowRuns = Array<WorkflowRun>
type ExtractedOlderThan = {
deleteOlderThanMs: number,
name: string,
}
type ListOfExtractedOlderThan = Array<ExtractedOlderThan>
export const extract = (data: boolean | string): ExtractedOlderThan => {
const [since, ...nameSlices] = String(data).split(' ')
const workflowName = nameSlices.length
? nameSlices.join(' ')
: '*'
let total = 0
if (since.match(/^(\d+[smhdwy])+$/)) {
const durations = [...since.matchAll(/\d+[smhdwy]/g)]
.map((duration) => duration[0])
.map((duration) => ({
amount: parseInt(duration.slice(0, -1)),
unit: duration.slice(-1) as keyof typeof durationUnitToMs,
}))
.map((duration) => duration.amount * durationUnitToMs[duration.unit])
total = durations.reduce((p, c) => p + c, total)
}
return {
deleteOlderThanMs: total,
name: workflowName,
}
}
export const filterWorkflowRuns = (workflowRuns: WorkflowRuns, extractedOlderThanList: ListOfExtractedOlderThan): WorkflowRuns => {
const now = new Date()
const olderThan = extractedOlderThanList
.map((since) => ({
deleteOlderThanDate: new Date(now).getTime() - since.deleteOlderThanMs,
name: since.name,
}))
return workflowRuns
.filter((run) => olderThan
.find((extractedInput) => {
if (extractedInput.name !== '*' && extractedInput.name !== run.name) {
return false
}
return new Date(run.created_at).getTime() < extractedInput.deleteOlderThanDate
})
)
}