Skip to content

Commit

Permalink
♻️ Add helpers for uptime, config, octokit
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 21, 2020
1 parent 52c8d45 commit 45e0612
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/helpers/calculate-uptime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { readFile } from "fs-extra";
import { safeLoad } from "js-yaml";
import { getOctokit } from "./github";
import { join } from "path";
import { SiteHistory } from "../interfaces";

/**
* Get the number of seconds a website has been down
* @param slug - Slug of the site
*/
const getDowntimeSecondsForSite = async (slug: string): Promise<number> => {
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
const octokit = await getOctokit();
let msDown = 0;

// Get all the issues for this website
const { data } = await octokit.issues.listForRepo({
owner,
repo,
labels: `status,${slug}`,
filter: "all",
per_page: 100,
});

// If this issue has been closed already, calculate the difference
// between when it was closed and when it was opened
// If this issue is still open, calculate the time since it was opened
data.forEach(
(issue) =>
(msDown +=
new Date(issue.closed_at || new Date()).getTime() - new Date(issue.created_at).getTime())
);

return Math.round(msDown / 1000);
};

/**
* Get the uptime percentage for a website
* @returns Percent string, e.g., 94.43%
* @param slug - Slug of the site
*/
export const getUptimePercentForSite = async (slug: string): Promise<string> => {
const site = safeLoad(await readFile(join(".", "history", `${slug}.yml`), "utf8")) as SiteHistory;
// Time when we started tracking this website's downtime
const startDate = new Date(site.startTime ?? new Date());

// Number of seconds we have been tracking this site
const totalSeconds = (new Date().getTime() - startDate.getTime()) / 1000;

// Number of seconds the site has been down
const downtimeSeconds = await getDowntimeSecondsForSite(slug);

// Return a percentage string
return `${((downtimeSeconds / totalSeconds) * 100).toFixed(2)}%`;
};
12 changes: 12 additions & 0 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readFile } from "fs-extra";
import { safeLoad } from "js-yaml";
import { join } from "path";
import { UpptimeConfig } from "../interfaces";

let __config: UpptimeConfig | undefined = undefined;
export const getConfig = async (): Promise<UpptimeConfig> => {
if (__config) return __config;
const config = safeLoad(await readFile(join(".", ".upptimerc.yml"), "utf8")) as UpptimeConfig;
__config = config;
return config;
};
10 changes: 10 additions & 0 deletions src/helpers/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Octokit } from "@octokit/rest";
import { getConfig } from "./config";

export const getOctokit = async (): Promise<Octokit> => {
const config = await getConfig();
return new Octokit({
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
userAgent: config["user-agent"] || process.env.USER_AGENT || "KojBot",
});
};

0 comments on commit 45e0612

Please sign in to comment.