Skip to content

Commit

Permalink
♻️ Get uptime percent for day, month, week, year
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Dec 2, 2020
1 parent d3dae25 commit 24ee219
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
46 changes: 34 additions & 12 deletions src/helpers/calculate-uptime.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import dayjs from "dayjs";
import { readFile } from "fs-extra";
import { safeLoad } from "js-yaml";
import { getOctokit } from "./github";
import { join } from "path";
import { SiteHistory } from "../interfaces";
import { DownPecentages, Downtimes, SiteHistory } from "../interfaces";
import { getOctokit } from "./github";

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

// Get all the issues for this website
const { data } = await octokit.issues.listForRepo({
Expand All @@ -26,21 +31,32 @@ const getDowntimeSecondsForSite = async (slug: string): Promise<number> => {
// 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())
);
data.forEach((issue) => {
const issueDowntime =
new Date(issue.closed_at || new Date()).getTime() - new Date(issue.created_at).getTime();
const issueCloseTime = dayjs(issue.closed_at);
if (issueCloseTime.isAfter(dayjs().subtract(1, "day"))) day += issueDowntime;
if (issueCloseTime.isAfter(dayjs().subtract(1, "week"))) week += issueDowntime;
if (issueCloseTime.isAfter(dayjs().subtract(1, "month"))) month += issueDowntime;
if (issueCloseTime.isAfter(dayjs().subtract(1, "year"))) year += issueDowntime;
all += issueDowntime;
});

return Math.round(msDown / 1000);
return {
day: Math.round(day / 1000),
week: Math.round(week / 1000),
month: Math.round(month / 1000),
year: Math.round(year / 1000),
all: Math.round(all / 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> => {
export const getUptimePercentForSite = async (slug: string): Promise<DownPecentages> => {
const site = safeLoad(
(await readFile(join(".", "history", `${slug}.yml`), "utf8"))
.split("\n")
Expand All @@ -57,5 +73,11 @@ export const getUptimePercentForSite = async (slug: string): Promise<string> =>
const downtimeSeconds = await getDowntimeSecondsForSite(slug);

// Return a percentage string
return `${Math.max(0, 100 - (downtimeSeconds / totalSeconds) * 100).toFixed(2)}%`;
return {
day: `${Math.max(0, 100 - (downtimeSeconds.day / 86400) * 100).toFixed(2)}%`,
week: `${Math.max(0, 100 - (downtimeSeconds.week / 604800) * 100).toFixed(2)}%`,
month: `${Math.max(0, 100 - (downtimeSeconds.month / 2628288) * 100).toFixed(2)}%`,
year: `${Math.max(0, 100 - (downtimeSeconds.year / 31536000) * 100).toFixed(2)}%`,
all: `${Math.max(0, 100 - (downtimeSeconds.all / totalSeconds) * 100).toFixed(2)}%`,
};
};
19 changes: 19 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,23 @@ export interface SiteStatus {
time: number;
/** Total uptime percentage */
uptime: string;
uptimeDay: string;
uptimeWeek: string;
uptimeMonth: string;
uptimeYear: string;
}

export interface Downtimes {
day: number;
week: number;
month: number;
year: number;
all: number;
}
export interface DownPecentages {
day: string;
week: string;
month: string;
year: string;
all: string;
}
8 changes: 7 additions & 1 deletion src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ export const generateSummary = async () => {
? "degraded"
: "down";

const uptimes = await getUptimePercentForSite(slug);
console.log("Uptimes", uptimes);
pageStatuses.push({
name: site.name,
url: site.url,
slug,
status,
uptime: await getUptimePercentForSite(slug),
uptime: uptimes.all,
uptimeDay: uptimes.day,
uptimeWeek: uptimes.week,
uptimeMonth: uptimes.month,
uptimeYear: uptimes.year,
time: Math.floor(averageTime),
});
if (status === "down") numberOfDown++;
Expand Down

0 comments on commit 24ee219

Please sign in to comment.