Skip to content

Commit

Permalink
🐛 Use overlapping time for downtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Dec 2, 2020
1 parent 9668652 commit 88e26a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/helpers/calculate-uptime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { safeLoad } from "js-yaml";
import { join } from "path";
import { DownPecentages, Downtimes, SiteHistory } from "../interfaces";
import { getOctokit } from "./github";
import { checkOverlap } from "./overlap";

/**
* Get the number of seconds a website has been down
Expand Down Expand Up @@ -34,12 +35,29 @@ const getDowntimeSecondsForSite = async (slug: string): Promise<Downtimes> => {
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;
const issueOverlap = {
start: new Date(issue.created_at).getTime(),
end: new Date(issue.closed_at || new Date()).getTime(),
};
const end = dayjs().toDate().getTime();

day += checkOverlap(issueOverlap, {
start: dayjs().subtract(1, "day").toDate().getTime(),
end,
});
week += checkOverlap(issueOverlap, {
start: dayjs().subtract(1, "week").toDate().getTime(),
end,
});
month += checkOverlap(issueOverlap, {
start: dayjs().subtract(1, "month").toDate().getTime(),
end,
});
year += checkOverlap(issueOverlap, {
start: dayjs().subtract(1, "year").toDate().getTime(),
end,
});
});

return {
Expand Down
19 changes: 19 additions & 0 deletions src/helpers/overlap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { checkOverlap } from "./overlap";

describe("checkOverlap", () => {
it("partial overlap", () => {
expect(checkOverlap({ start: 14, end: 17 }, { start: 16, end: 19 })).toEqual(1);
});

it("partial overlap (opposite)", () => {
expect(checkOverlap({ start: 16, end: 19 }, { start: 14, end: 17 })).toEqual(1);
});

it("full overlap", () => {
expect(checkOverlap({ start: 14, end: 17 }, { start: 13, end: 18 })).toEqual(3);
});

it("no overlap", () => {
expect(checkOverlap({ start: 14, end: 17 }, { start: 19, end: 21 })).toEqual(0);
});
});
14 changes: 14 additions & 0 deletions src/helpers/overlap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
interface Range {
start: number;
end: number;
}

/**
* Get the overlap between two numbers
*/
export const checkOverlap = (a: Range, b: Range): number => {
const min = a.start < b.start ? a : b;
const max = min.start === a.start && min.end === a.end ? b : a;
if (min.end < max.start) return 0;
return (min.end < max.end ? min.end : max.end) - max.start;
};

0 comments on commit 88e26a6

Please sign in to comment.