Skip to content

Commit 45e0612

Browse files
♻️ Add helpers for uptime, config, octokit
1 parent 52c8d45 commit 45e0612

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/helpers/calculate-uptime.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { readFile } from "fs-extra";
2+
import { safeLoad } from "js-yaml";
3+
import { getOctokit } from "./github";
4+
import { join } from "path";
5+
import { SiteHistory } from "../interfaces";
6+
7+
/**
8+
* Get the number of seconds a website has been down
9+
* @param slug - Slug of the site
10+
*/
11+
const getDowntimeSecondsForSite = async (slug: string): Promise<number> => {
12+
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
13+
const octokit = await getOctokit();
14+
let msDown = 0;
15+
16+
// Get all the issues for this website
17+
const { data } = await octokit.issues.listForRepo({
18+
owner,
19+
repo,
20+
labels: `status,${slug}`,
21+
filter: "all",
22+
per_page: 100,
23+
});
24+
25+
// If this issue has been closed already, calculate the difference
26+
// between when it was closed and when it was opened
27+
// If this issue is still open, calculate the time since it was opened
28+
data.forEach(
29+
(issue) =>
30+
(msDown +=
31+
new Date(issue.closed_at || new Date()).getTime() - new Date(issue.created_at).getTime())
32+
);
33+
34+
return Math.round(msDown / 1000);
35+
};
36+
37+
/**
38+
* Get the uptime percentage for a website
39+
* @returns Percent string, e.g., 94.43%
40+
* @param slug - Slug of the site
41+
*/
42+
export const getUptimePercentForSite = async (slug: string): Promise<string> => {
43+
const site = safeLoad(await readFile(join(".", "history", `${slug}.yml`), "utf8")) as SiteHistory;
44+
// Time when we started tracking this website's downtime
45+
const startDate = new Date(site.startTime ?? new Date());
46+
47+
// Number of seconds we have been tracking this site
48+
const totalSeconds = (new Date().getTime() - startDate.getTime()) / 1000;
49+
50+
// Number of seconds the site has been down
51+
const downtimeSeconds = await getDowntimeSecondsForSite(slug);
52+
53+
// Return a percentage string
54+
return `${((downtimeSeconds / totalSeconds) * 100).toFixed(2)}%`;
55+
};

src/helpers/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { readFile } from "fs-extra";
2+
import { safeLoad } from "js-yaml";
3+
import { join } from "path";
4+
import { UpptimeConfig } from "../interfaces";
5+
6+
let __config: UpptimeConfig | undefined = undefined;
7+
export const getConfig = async (): Promise<UpptimeConfig> => {
8+
if (__config) return __config;
9+
const config = safeLoad(await readFile(join(".", ".upptimerc.yml"), "utf8")) as UpptimeConfig;
10+
__config = config;
11+
return config;
12+
};

src/helpers/github.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Octokit } from "@octokit/rest";
2+
import { getConfig } from "./config";
3+
4+
export const getOctokit = async (): Promise<Octokit> => {
5+
const config = await getConfig();
6+
return new Octokit({
7+
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
8+
userAgent: config["user-agent"] || process.env.USER_AGENT || "KojBot",
9+
});
10+
};

0 commit comments

Comments
 (0)