|
| 1 | +import { format } from "npm:date-fns@3.2.0"; |
| 2 | +import type { ganttJob, ganttStep, StepConclusion } from "./types.ts"; |
| 3 | + |
| 4 | +export const diffSec = ( |
| 5 | + start?: string | Date | null, |
| 6 | + end?: string | Date | null, |
| 7 | +): number => { |
| 8 | + if (!start || !end) return 0; |
| 9 | + const startDate = new Date(start); |
| 10 | + const endDate = new Date(end); |
| 11 | + |
| 12 | + return (endDate.getTime() - startDate.getTime()) / 1000; |
| 13 | +}; |
| 14 | + |
| 15 | +// Sec to elapsed format time like HH:mm:ss (ex. 70sec -> 00:01:10) |
| 16 | +export const formatElapsedTime = (sec: number): string => { |
| 17 | + const date = new Date(sec * 1000); |
| 18 | + const offsetMinute = date.getTimezoneOffset(); |
| 19 | + const timezonreIgnoredDate = new Date(sec * 1000 + offsetMinute * 60 * 1000); |
| 20 | + return format(timezonreIgnoredDate, "HH:mm:ss"); |
| 21 | +}; |
| 22 | + |
| 23 | +// Sec to elapsed short format time like 1h2m3s (ex. 70sec -> 1m10s) |
| 24 | +export const formatShortElapsedTime = (sec: number): string => { |
| 25 | + const date = new Date(sec * 1000); |
| 26 | + const offsetMinute = date.getTimezoneOffset(); |
| 27 | + const timezonreIgnoredDate = new Date(sec * 1000 + offsetMinute * 60 * 1000); |
| 28 | + if (sec < 60) { |
| 29 | + return format(timezonreIgnoredDate, "s's'"); |
| 30 | + } else if (sec < 60 * 60) { |
| 31 | + return format(timezonreIgnoredDate, "m'm's's'"); |
| 32 | + } else { |
| 33 | + return format(timezonreIgnoredDate, "H'h'm'm's's'"); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +export const formatStep = (step: ganttStep): string => { |
| 38 | + switch (step.status) { |
| 39 | + case "": |
| 40 | + return `${step.name} :${step.id}, ${step.position}, ${step.sec}s`; |
| 41 | + default: |
| 42 | + return `${step.name} :${step.status}, ${step.id}, ${step.position}, ${step.sec}s`; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +export const formatName = (name: string, sec: number): string => { |
| 47 | + return `${escapeName(name)} (${formatShortElapsedTime(sec)})`; |
| 48 | +}; |
| 49 | + |
| 50 | +export const escapeName = (name: string): string => { |
| 51 | + return name.replaceAll(":", ""); |
| 52 | +}; |
| 53 | + |
| 54 | +export function formatSection(job: ganttJob): string { |
| 55 | + return [ |
| 56 | + `section ${job.section}`, |
| 57 | + ...job.steps.map((step) => formatStep(step)), |
| 58 | + ].join("\n"); |
| 59 | +} |
| 60 | + |
| 61 | +export const convertStepToStatus = ( |
| 62 | + conclusion: StepConclusion, |
| 63 | +): ganttStep["status"] => { |
| 64 | + switch (conclusion) { |
| 65 | + case "success": |
| 66 | + return ""; |
| 67 | + case "failure": |
| 68 | + return "crit"; |
| 69 | + case "cancelled": |
| 70 | + case "skipped": |
| 71 | + case "timed_out": |
| 72 | + return "done"; |
| 73 | + case "neutral": |
| 74 | + case "action_required": |
| 75 | + case null: |
| 76 | + return "active"; |
| 77 | + default: |
| 78 | + return "active"; |
| 79 | + } |
| 80 | +}; |
0 commit comments