Fix "Invalid date" error

This commit is contained in:
Aelita4 2022-08-22 10:26:04 +02:00
parent 30865c6bd0
commit 95b5be4e5b
Signed by: Aelita4
GPG Key ID: F8EC95519509D1D5
5 changed files with 8 additions and 9 deletions

View File

@ -21,7 +21,7 @@ const getIP = () : Promise<string[]> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
connection.query("SELECT * FROM ips", async (err, results, fields) => { connection.query("SELECT * FROM ips", async (err, results, fields) => {
if(err) reject(err); if(err) reject(err);
const addresses = [...results.slice()]; const addresses = [...results.slice()].map(a => a.ip);
resolve(addresses); resolve(addresses);
}); });
@ -54,7 +54,7 @@ const pings = new Map();
let addresses: Array<string> = await getIP(); let addresses: Array<string> = await getIP();
addresses.forEach((a: string) => pings.set(a, -1)); addresses.forEach((a: string) => pings.set(a, `${Date.now()}_1`));
const timeoutDelay = 60000; const timeoutDelay = 60000;
const routes = readdirSync(__dirname + "/routes"); const routes = readdirSync(__dirname + "/routes");

View File

@ -6,7 +6,7 @@ export default {
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {
const ip = req.params.ip; 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.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(json)); res.end(JSON.stringify(json));

View File

@ -4,10 +4,11 @@ export default {
method: "GET", method: "GET",
url: "/ping/:ip", url: "/ping/:ip",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {
console.log(data.pings)
const ip = req.params.ip; const ip = req.params.ip;
const r = await data.ping(ip); const r = await data.ping(ip);
if(!r.alive && data.pings.get(ip) === -1) data.pings.set(ip, Date.now()); 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, -1); else if(r.alive) data.pings.set(ip, `${Date.now()}_1`);
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(r)); res.end(JSON.stringify(r));
} }

View File

@ -6,9 +6,7 @@ export default {
method: "GET", method: "GET",
url: "/", url: "/",
callback: async (data: any, req: Request, res: Response) => { callback: async (data: any, req: Request, res: Response) => {
const tmp = await getIP(); data.addresses = await getIP();
//@ts-ignore
data.addresses = tmp.map(a => a.ip);
//@ts-ignore //@ts-ignore
if(!req.session.user) res.redirect("/login"); if(!req.session.user) res.redirect("/login");
else res.render('pages/index.ejs', { else res.render('pages/index.ejs', {

View File

@ -40,7 +40,7 @@
await fetch('http://localhost:8080/ping/' + a) await fetch('http://localhost:8080/ping/' + a)
.then((response) => response.json()) .then((response) => response.json())
.then(async (data) => { .then(async (data) => {
b.innerHTML = data.alive ? '<span style="color:green;">UP (' + data.numeric_host + ") " + data.time + 'ms</span>' : '<span style="color:red;">DOWN' + ((data.host == "unknown") ? " (UNKNOWN HOST)" : (" (" + data.numeric_host + ")")) + " since " + new Date((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince) + '</span>' b.innerHTML = data.alive ? '<span style="color:green;">UP (' + data.numeric_host + ") " + data.time + 'ms</span>' : '<span style="color:red;">DOWN' + ((data.host == "unknown") ? " (UNKNOWN HOST)" : (" (" + data.numeric_host + ")")) + " since " + new Date(Number((await (await fetch('http://localhost:8080/downtime/' + a)).json()).downSince)) + '</span>'
}); });
}) })
setTimeout(aaa, <%= timeoutDelay %>) setTimeout(aaa, <%= timeoutDelay %>)