|
| 1 | +import boxen from "boxen"; |
| 2 | +import chalk from "chalk"; |
| 3 | +import type { Command } from "commander"; |
| 4 | +import HellHub, { type Cron } from "@hellhub-collective/sdk"; |
| 5 | + |
| 6 | +import ascii from "utils/ascii"; |
| 7 | +import output from "utils/output"; |
| 8 | +import request from "utils/request"; |
| 9 | + |
| 10 | +function timeAgo(input: Date | string | number) { |
| 11 | + const ranges = [ |
| 12 | + ["years", 3600 * 24 * 365], |
| 13 | + ["months", 3600 * 24 * 30], |
| 14 | + ["weeks", 3600 * 24 * 7], |
| 15 | + ["days", 3600 * 24], |
| 16 | + ["hours", 3600], |
| 17 | + ["minutes", 60], |
| 18 | + ["seconds", 1], |
| 19 | + ] as const; |
| 20 | + |
| 21 | + const formatter = new Intl.RelativeTimeFormat("en-US"); |
| 22 | + const date = input instanceof Date ? input : new Date(input); |
| 23 | + |
| 24 | + const secondsElapsed = (date.getTime() - Date.now()) / 1000; |
| 25 | + for (const [rangeType, rangeVal] of ranges) { |
| 26 | + if (rangeVal < Math.abs(secondsElapsed)) { |
| 27 | + const delta = secondsElapsed / rangeVal; |
| 28 | + return formatter.format(Math.round(delta), rangeType); |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export default function source(program: Command) { |
| 34 | + const handler = async (...args: any[]) => { |
| 35 | + // @ts-expect-error |
| 36 | + const { data, url } = await request<Cron>( |
| 37 | + HellHub.crons, |
| 38 | + "refresh_from_source", |
| 39 | + ); |
| 40 | + |
| 41 | + if (process.argv.includes("--raw") || process.argv.includes("-r")) { |
| 42 | + output( |
| 43 | + // @ts-expect-error |
| 44 | + data, |
| 45 | + process.argv.includes("--pretty") || process.argv.includes("-p"), |
| 46 | + ); |
| 47 | + process.exit(0); |
| 48 | + } |
| 49 | + |
| 50 | + await ascii("database", { |
| 51 | + name: chalk.bold("Service health"), |
| 52 | + description: chalk.gray("Information about the source refresh cron job"), |
| 53 | + }); |
| 54 | + |
| 55 | + const result = data as Cron; |
| 56 | + const values: string[][] = []; |
| 57 | + |
| 58 | + values.push([ |
| 59 | + "Status", |
| 60 | + chalk.bold( |
| 61 | + result.busy |
| 62 | + ? chalk.yellow("Executing") |
| 63 | + : result.status === "ok" |
| 64 | + ? chalk.green("OK") |
| 65 | + : chalk.red("Error"), |
| 66 | + ), |
| 67 | + ]); |
| 68 | + |
| 69 | + values.push([ |
| 70 | + Array.from({ length: 6 }) |
| 71 | + .map(() => "-") |
| 72 | + .join(""), |
| 73 | + Array.from({ length: 62 }) |
| 74 | + .map(() => "-") |
| 75 | + .join(""), |
| 76 | + ]); |
| 77 | + |
| 78 | + if (!result.busy && result.runs.previous) { |
| 79 | + const time = timeAgo(result.runs.previous) ?? "N/A"; |
| 80 | + values.push(["Last refresh", time]); |
| 81 | + } |
| 82 | + |
| 83 | + if (!result.busy && result.runs.next) { |
| 84 | + const time = timeAgo(result.runs.next) ?? "N/A"; |
| 85 | + values.push(["Next refresh", time]); |
| 86 | + } |
| 87 | + |
| 88 | + const str = values.map(s => s.join(": ")).join("\n"); |
| 89 | + console.log( |
| 90 | + boxen( |
| 91 | + `${str}${result.busy ? `\n${chalk.bold(chalk.white("Source data is refreshing"))}` : ""}`, |
| 92 | + { |
| 93 | + padding: 1, |
| 94 | + title: chalk.bold("Arrowhead API Synchronization"), |
| 95 | + borderColor: "white", |
| 96 | + borderStyle: "bold", |
| 97 | + width: 78, |
| 98 | + }, |
| 99 | + ), |
| 100 | + ); |
| 101 | + |
| 102 | + if (process.argv.includes("--url") || process.argv.includes("-u")) { |
| 103 | + console.log(chalk.bold("\nRequest Source")); |
| 104 | + console.log(chalk.gray(`/${url.split("/").slice(3).join("/")}`)); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + program |
| 109 | + .command("health") |
| 110 | + .description("displays the health of the source refresh cron job") |
| 111 | + .option("-u, --url [boolean]", "prints the request url") |
| 112 | + .option("-a, --no-ascii [boolean]", "disables the ascii art headers") |
| 113 | + .option("-r, --raw [boolean]", "outputs the raw response data as json") |
| 114 | + .option("-p, --pretty [boolean]", "use with --raw to pretty print the json") |
| 115 | + .action(handler); |
| 116 | +} |
0 commit comments