-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworkflow_gantt.ts
147 lines (132 loc) · 4 KB
/
workflow_gantt.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { format } from "npm:date-fns@2.30.0";
import { Workflow, WorkflowJobs } from "./github.ts";
type ganttJob = {
section: string;
steps: ganttStep[];
};
export type ganttStep = {
name: string;
id: `job${number}-${number}`;
status: "" | "done" | "active" | "crit";
position: string;
sec: number;
};
// ref: https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#get-a-job-for-a-workflow-run
type StepConclusion =
| "success"
| "failure"
| "neutral"
| "cancelled"
| "skipped"
| "timed_out"
| "action_required"
| null;
const diffSec = (
start?: string | Date | null,
end?: string | Date | null,
): number => {
if (!start || !end) return 0;
const startDate = new Date(start);
const endDate = new Date(end);
return (endDate.getTime() - startDate.getTime()) / 1000;
};
// Sec to elapsed format time like HH:mm:ss (ex. 70sec -> 00:01:10)
const formatElapsedTime = (sec: number): string => {
const date = new Date(sec * 1000);
const offsetMinute = date.getTimezoneOffset();
const timezonreIgnoredDate = new Date(sec * 1000 + offsetMinute * 60 * 1000);
return format(timezonreIgnoredDate, "HH:mm:ss");
};
// Sec to elapsed short format time like 1h2m3s (ex. 70sec -> 1m10s)
export const formatShortElapsedTime = (sec: number): string => {
const date = new Date(sec * 1000);
const offsetMinute = date.getTimezoneOffset();
const timezonreIgnoredDate = new Date(sec * 1000 + offsetMinute * 60 * 1000);
if (sec < 60) {
return format(timezonreIgnoredDate, "s's'");
} else if (sec < 60 * 60) {
return format(timezonreIgnoredDate, "m'm's's'");
} else {
return format(timezonreIgnoredDate, "H'h'm'm's's'");
}
};
export const formatStep = (step: ganttStep): string => {
switch (step.status) {
case "":
return `${step.name} :${step.id}, ${step.position}, ${step.sec}s`;
default:
return `${step.name} :${step.status}, ${step.id}, ${step.position}, ${step.sec}s`;
}
};
const formatName = (name: string, sec: number): string => {
return `${escapeName(name)} (${formatShortElapsedTime(sec)})`;
};
const escapeName = (name: string): string => {
return name.replaceAll(":", "");
};
const convertStepToStatus = (
conclusion: StepConclusion,
): ganttStep["status"] => {
switch (conclusion) {
case "success":
return "";
case "failure":
return "crit";
case "cancelled":
case "skipped":
case "timed_out":
return "done";
case "neutral":
case "action_required":
case null:
return "active";
default:
return "active";
}
};
export const createGantt = (
workflow: Workflow,
workflowJobs: WorkflowJobs,
): string => {
const title = workflowJobs[0].workflow_name;
const jobs = workflowJobs.map((job, jobIndex, _jobs): ganttJob => {
const section = escapeName(job.name);
const status: ganttStep["status"] = "active";
const startJobElapsedSec = diffSec(workflow.created_at, job.created_at);
const waitingRunnerElapsedSec = diffSec(job.created_at, job.started_at);
const waitingRunnerStep: ganttStep = {
name: formatName("Waiting for a runner", waitingRunnerElapsedSec),
id: `job${jobIndex}-0`,
status,
position: formatElapsedTime(startJobElapsedSec),
sec: waitingRunnerElapsedSec,
};
const steps = job.steps?.map((step, stepIndex, _steps): ganttStep => {
const stepElapsedSec = diffSec(step.started_at, step.completed_at);
return {
name: formatName(step.name, stepElapsedSec),
id: `job${jobIndex}-${stepIndex + 1}`,
status: convertStepToStatus(step.conclusion as StepConclusion),
position: `after job${jobIndex}-${stepIndex}`,
sec: stepElapsedSec,
};
}) ?? [];
return { section, steps: [waitingRunnerStep, ...steps] };
});
return `
\`\`\`mermaid
gantt
title ${title}
dateFormat HH:mm:ss
axisFormat %H:%M:%S
${
jobs.flatMap((job) => {
return [
`section ${job.section}`,
...job.steps.map((step) => formatStep(step)),
];
}).join("\n")
}
\`\`\`
`;
};