Skip to content

Commit

Permalink
Fix NaN uptime percentage
Browse files Browse the repository at this point in the history
This commit fixes an issue if there's no data for a region, it results in a `NaN` uptime percentage

* Return `0` when no regions with timeseries are present.
  • Loading branch information
matsimitsu committed Aug 16, 2024
1 parent 1e1b863 commit fcea542
Show file tree
Hide file tree
Showing 6 changed files with 1,056 additions and 1 deletion.
6 changes: 6 additions & 0 deletions components/Outages/Outages.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PropTypes from "prop-types";
import OutagesByDay from "../OutagesByDay";
import { timeseriesByDay } from "../../utils";

Expand All @@ -23,4 +24,9 @@ const Outages = ({ timeseries, regions }) => {
}
};

Outages.propTypes = {
timeseries: PropTypes.array.isRequired,
regions: PropTypes.array.isRequired,
};

export default Outages;
8 changes: 8 additions & 0 deletions components/UptimeMonitor/UptimeMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const calculateUptime = (timeseries, regions) => {
const downtimePerRegion = [];

regions.map((region) => {
if (timeSeriesLast30Days.length == 0) {
return 0;
}

const downtimeInMinutes = timeSeriesLast30Days.reduce((acc, item) => {
return acc + item.values[region];
}, 0);
Expand All @@ -63,6 +67,10 @@ export const calculateUptime = (timeseries, regions) => {
};

export const averageDowntimeOverRegions = (downtimePerRegion) => {
if (Object.keys(downtimePerRegion).length == 0) {
return 0;
}

const average =
Object.values(downtimePerRegion).reduce((acc, item) => {
return (acc += item.percentage);
Expand Down
11 changes: 10 additions & 1 deletion components/UptimeMonitor/UptimeMonitor.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { render, screen, waitFor } from "@testing-library/react";

import UptimeMonitor, { UptimeMonitorLoading } from "./UptimeMonitor";
import UptimeMonitor, {
UptimeMonitorLoading,
averageDowntimeOverRegions,
} from "./UptimeMonitor";
import statusPageMock from "../../mocks/status_pages/appsignal.json";

const build = (props = {}) => {
Expand Down Expand Up @@ -67,4 +70,10 @@ describe("UptimeMonitorLoading", () => {
const loadingDots = screen.getAllByTestId("loadingDot");
expect(loadingDots.length).toEqual(30);
});

describe("averageDowntimeOverRegions", () => {
test("returns 0 if no regions are present", () => {
expect(averageDowntimeOverRegions([])).toEqual(0);
});
});
});
2 changes: 2 additions & 0 deletions mocks/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { rest } from "msw";
const alwaysDown = require("./monitors/always-down.json");
const blog = require("./monitors/blog.json");
const homepage = require("./monitors/homepage.json");
const onlyOneMetric = require("./monitors/only-one-metric.json");

const monitorMapping = {
"always-down": alwaysDown,
blog,
homepage,
"only-one-metric": onlyOneMetric,
};

export const handlers = [
Expand Down
Loading

0 comments on commit fcea542

Please sign in to comment.