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 { getYesterdayISOString } = require("../utils");
// returns all critical health check entries
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);
};

View File

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

View File

@ -1,8 +1,14 @@
const db = require("../db");
const { getYesterdayISOString } = require("../utils");
// returns all verbose health check entries
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);
};

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) {
const diff = process.hrtime(start);
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 };