Skip to content

Commit ad65dba

Browse files
authored
feat: add cron job health command (#17)
* chore(deps): bump sdk to version 1.3.9 * feat: add health command for refresh cron * feat: implement new command in main file * chore(release): bump package version to 1.4.0
1 parent c5277ee commit ad65dba

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

bun.lockb

0 Bytes
Binary file not shown.

commands/source.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { bin, description, version, repository } from "package.json";
55

66
import major from "commands/major";
77
import events from "commands/events";
8+
import source from "commands/source";
89
import planets from "commands/planets";
910
import sectors from "commands/sectors";
1011
import reports from "commands/reports";
@@ -24,8 +25,15 @@ program
2425
.description(programDescription)
2526
.addHelpText("after", helpText);
2627

27-
[planets, sectors, stratagems, reports, events, major, statistics].forEach(
28-
com => com(program),
29-
);
28+
[
29+
planets,
30+
sectors,
31+
stratagems,
32+
reports,
33+
events,
34+
major,
35+
statistics,
36+
source,
37+
].forEach(com => com(program));
3038

3139
program.parse(process.argv);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"license": "MIT",
44
"private": false,
55
"description": "A CLI for Helldivers 2: View the war progress without leaving your terminal! Powered by the HellHub API.",
6-
"version": "1.3.0",
6+
"version": "1.4.0",
77
"main": "build/index.mjs",
88
"types": "build/index.d.ts",
99
"keywords": [
@@ -54,7 +54,7 @@
5454
"email": "nettisfabio@gmail.com"
5555
},
5656
"dependencies": {
57-
"@hellhub-collective/sdk": "^1.2.1",
57+
"@hellhub-collective/sdk": "^1.3.0",
5858
"accounting": "^0.4.1",
5959
"boxen": "^7.1.1",
6060
"chalk": "^5.3.0",

0 commit comments

Comments
 (0)