code review changes

This commit is contained in:
Karol Wypchlo 2020-09-10 14:03:56 +02:00
parent ec4655f581
commit 6e0f387cf1
4 changed files with 33 additions and 9 deletions

View File

@ -1,8 +1,14 @@
const db = require("../db"); const db = require("../db");
const { getYesterdayISOString } = require("../utils");
// returns all critical health check entries // returns all critical health check entries
module.exports = (req, res) => { module.exports = (req, res) => {
const entries = db.get("critical").orderBy("date", "desc").value(); const yesterday = getYesterdayISOString();
const entries = db
.get("critical")
.orderBy("date", "desc")
.filter(({ date }) => date > yesterday)
.value();
res.send(entries); res.send(entries);
}; };

View File

@ -18,13 +18,12 @@ function getStatusCode() {
// grab the most recent critical entry element from DB // grab the most recent critical entry element from DB
const entry = getCurrentCriticalEntry(); const entry = getCurrentCriticalEntry();
// find out whether every check in the entry is up // in case there is no entry yet or at least one check failed in the most recent entry
if (entry && entry.checks.every(({ up }) => up)) { if (!entry || entry.checks.some(({ up }) => !up)) {
return StatusCodes.OK; return StatusCodes.SERVICE_UNAVAILABLE;
} }
// in case at least one check failed return StatusCodes.OK;
return StatusCodes.SERVICE_UNAVAILABLE;
} }
/** /**

View File

@ -1,8 +1,14 @@
const db = require("../db"); const db = require("../db");
const { getYesterdayISOString } = require("../utils");
// returns all verbose health check entries // returns all verbose health check entries
module.exports = (req, res) => { module.exports = (req, res) => {
const entries = db.get("verbose").orderBy("date", "desc").value(); const yesterday = getYesterdayISOString();
const entries = db
.get("verbose")
.orderBy("date", "desc")
.filter(({ date }) => date > yesterday)
.value();
res.send(entries); res.send(entries);
}; };

View File

@ -1,8 +1,21 @@
// return the time between start and now in milliseconds /**
* Get the time between start and now in milliseconds
*/
function calculateElapsedTime(start) { function calculateElapsedTime(start) {
const diff = process.hrtime(start); const diff = process.hrtime(start);
return Math.round((diff[0] * 1e9 + diff[1]) / 1e6); // msec return Math.round((diff[0] * 1e9 + diff[1]) / 1e6); // msec
} }
module.exports = { calculateElapsedTime }; /**
* Get the ISO string with yesterday's date set (- 24 hours)
*/
function getYesterdayISOString() {
const date = new Date();
date.setDate(date.getDate() - 1);
return date.toISOString();
}
module.exports = { calculateElapsedTime, getYesterdayISOString };