From 22129d5268e2cebdaf3b8cb6fd5db6c977e38fc5 Mon Sep 17 00:00:00 2001 From: Nicholas Orlowsky Date: Sat, 22 Oct 2022 16:21:03 -0500 Subject: [PATCH] service outages and use nws api --- .idea/workspace.xml | 13 +++++- src/App.css | 19 +++++++- src/App.tsx | 106 ++++++++++++++++++++----------------------- src/nws-api/calls.ts | 14 ++++++ src/nws-api/types.ts | 34 ++++++++++++++ 5 files changed, 126 insertions(+), 60 deletions(-) create mode 100644 src/nws-api/calls.ts create mode 100644 src/nws-api/types.ts diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e0e2e3b..8b411ee 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,10 @@ + + - - + + + + @@ -60,6 +68,7 @@ + diff --git a/src/App.css b/src/App.css index f7a3d61..c9a9b36 100644 --- a/src/App.css +++ b/src/App.css @@ -1,6 +1,23 @@ .App { - text-align: center; display: flex; align-items: center; flex-direction: column; } + +.low-severity { + background-color: #98fb98 +} + +.med-severity { + background-color: #eee8aa +} + +.high-severity { + background-color: #f08080 +} + +.incident { + border-radius: 20px; + padding: .75rem; +} + diff --git a/src/App.tsx b/src/App.tsx index 75e2146..ccf0fe6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,52 +2,28 @@ import React, {useEffect, useState} from 'react'; import NWSLogo from './static/images/NWS_Logo.png'; import './App.css'; import 'bootstrap/dist/css/bootstrap.min.css'; +import {Incident, UptimeResponse} from "./nws-api/types"; +import {getIncidents, getUptime} from "./nws-api/calls"; function App() { - const today = new Date(); - const setup_time = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate()); - const [monitors, setMonitors] = useState(new Array()); + const [uptime, setUptime] = useState({datacenters: [], services:[]}); + const [incidents, setIncidents] = useState([]); + + const fetchUptime = async () => { + let resp: UptimeResponse = await getUptime(); + setUptime(resp); + } + + const fetchIncidents = async () => { + let resp: Incident[] = await getIncidents(); + setIncidents(resp); + } useEffect(() => { - var myHeaders = new Headers(); - myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); - - var urlencoded = new URLSearchParams(); - urlencoded.append("api_key", "ur1612363-492fa5df2a31fab5b52171b4"); - - urlencoded.append("custom_uptime_ranges", (setup_time.valueOf() / 1000) + "_" + (today.valueOf() / 1000)); - urlencoded.append("all_time_uptime_ratio", "1"); - - var requestOptions = { - method: 'POST', - headers: myHeaders, - body: urlencoded, - redirect: 'follow' - }; - - // @ts-ignore - fetch("https://api.uptimerobot.com/v2/getMonitors", requestOptions) - .then(response => response.json().then(json => { - setMonitors(json.monitors); - })) - .then(result => console.log(result)) - .catch(error => console.log('error', error)); + fetchUptime(); + fetchIncidents(); }, []); - let diff = new Date().getTime() - setup_time.getTime(); - - let days = Math.floor(diff / (1000 * 60 * 60 * 24)); - diff -= days * (1000 * 60 * 60 * 24); - - let hours = Math.floor(diff / (1000 * 60 * 60)); - diff -= hours * (1000 * 60 * 60); - - let mins = Math.floor(diff / (1000 * 60)); - diff -= mins * (1000 * 60); - - let seconds = Math.floor(diff / (1000)); - diff -= seconds * (1000); - return (
@@ -71,17 +47,13 @@ function App() { Uptime (All Time) Current Status - {monitors.map((e) => { - let name_parts = e.friendly_name.split('.'); - if (name_parts[0] === 'datacenter') { + {uptime.datacenters.map((e) => { return ( - {name_parts[1]} - {e.custom_uptime_ranges}% - {e.all_time_uptime_ratio}% - {e.status === 2 ? 'Up' : 'Down'} + {e.name} + {e.uptimeMonth}% + {e.uptimeAllTime}% + {e.isUp ? 'Up' : 'Down'} ); - - } })}

@@ -95,21 +67,41 @@ function App() { Uptime (All Time) Current Status - {monitors.map((e) => { - let name_parts = e.friendly_name.split('.'); - if (name_parts[0] === 'service') { + {uptime.services.map((e) => { + return ( - {name_parts[1]} - {e.custom_uptime_ranges}% - {e.all_time_uptime_ratio}% - {e.status === 2 ? 'Up' : 'Down'} + {e.name} + {e.uptimeMonth}% + {e.uptimeAllTime}% + {e.isUp ? 'Up' : 'Down'} ); - } })}

+
+ +
+
+
+
+
+

Service Alerts

+ {incidents.map((e) => { + let severityClass: string = e.severity == 0 ? 'low' : (e.severity == 1 ? 'med' : 'high'); + let severityString: string = e.severity == 0 ? 'Low' : (e.severity == 1 ? 'Medium' : 'High'); + return ( +
+

{e.title}

+

{severityString} Severity

+

{e.description}

+
+ ); + })} + {incidents.length == 0 &&
+
No service alerts.
+
}