From 95b5be4e5baed7f9140929121c99fe306c1c4f75 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Mon, 22 Aug 2022 10:26:04 +0200 Subject: [PATCH] Fix "Invalid date" error --- src/index.ts | 4 ++-- src/routes/downtime.ts | 2 +- src/routes/ping.ts | 5 +++-- src/routes/root.ts | 4 +--- views/pages/index.ejs | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0148454..48c5382 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,7 +21,7 @@ const getIP = () : Promise => { return new Promise((resolve, reject) => { connection.query("SELECT * FROM ips", async (err, results, fields) => { if(err) reject(err); - const addresses = [...results.slice()]; + const addresses = [...results.slice()].map(a => a.ip); resolve(addresses); }); @@ -54,7 +54,7 @@ const pings = new Map(); let addresses: Array = await getIP(); -addresses.forEach((a: string) => pings.set(a, -1)); +addresses.forEach((a: string) => pings.set(a, `${Date.now()}_1`)); const timeoutDelay = 60000; const routes = readdirSync(__dirname + "/routes"); diff --git a/src/routes/downtime.ts b/src/routes/downtime.ts index 8331301..af6f903 100644 --- a/src/routes/downtime.ts +++ b/src/routes/downtime.ts @@ -6,7 +6,7 @@ export default { callback: async (data: any, req: Request, res: Response) => { const ip = req.params.ip; - const json = { downSince: data.pings.get(ip) }; + const json = { downSince: data.pings.get(ip).split("_")[0] }; res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(json)); diff --git a/src/routes/ping.ts b/src/routes/ping.ts index f072f84..0a974df 100644 --- a/src/routes/ping.ts +++ b/src/routes/ping.ts @@ -4,10 +4,11 @@ export default { method: "GET", url: "/ping/:ip", callback: async (data: any, req: Request, res: Response) => { + console.log(data.pings) const ip = req.params.ip; const r = await data.ping(ip); - if(!r.alive && data.pings.get(ip) === -1) data.pings.set(ip, Date.now()); - else if(r.alive) data.pings.set(ip, -1); + if(!r.alive && data.pings.get(ip).split("_")[1] === "1") data.pings.set(ip, `${Date.now()}_0`); + else if(r.alive) data.pings.set(ip, `${Date.now()}_1`); res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(r)); } diff --git a/src/routes/root.ts b/src/routes/root.ts index caf7182..40755fe 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -6,9 +6,7 @@ export default { method: "GET", url: "/", callback: async (data: any, req: Request, res: Response) => { - const tmp = await getIP(); - //@ts-ignore - data.addresses = tmp.map(a => a.ip); + data.addresses = await getIP(); //@ts-ignore if(!req.session.user) res.redirect("/login"); else res.render('pages/index.ejs', { diff --git a/views/pages/index.ejs b/views/pages/index.ejs index fdbbe2d..15d1d27 100644 --- a/views/pages/index.ejs +++ b/views/pages/index.ejs @@ -40,7 +40,7 @@ await fetch('http://localhost:8080/ping/' + a) .then((response) => response.json()) .then(async (data) => { - b.innerHTML = data.alive ? 'UP (' + data.numeric_host + ") " + data.time + 'ms' : 'DOWN' + ((data.host == "unknown") ? " (UNKNOWN HOST)" : (" (" + data.numeric_host + ")")) + " since " + new Date((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince) + '' + b.innerHTML = data.alive ? 'UP (' + data.numeric_host + ") " + data.time + 'ms' : 'DOWN' + ((data.host == "unknown") ? " (UNKNOWN HOST)" : (" (" + data.numeric_host + ")")) + " since " + new Date(Number((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince)) + '' }); }) setTimeout(aaa, <%= timeoutDelay %>)