From 6e0f387cf113010133c2cf1ed8689022c9fea2fc Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Thu, 10 Sep 2020 14:03:56 +0200 Subject: [PATCH] code review changes --- packages/health-check/src/api/critical.js | 8 +++++++- packages/health-check/src/api/index.js | 9 ++++----- packages/health-check/src/api/verbose.js | 8 +++++++- packages/health-check/src/utils.js | 17 +++++++++++++++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/health-check/src/api/critical.js b/packages/health-check/src/api/critical.js index 6837d17e..a84b4d25 100644 --- a/packages/health-check/src/api/critical.js +++ b/packages/health-check/src/api/critical.js @@ -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); }; diff --git a/packages/health-check/src/api/index.js b/packages/health-check/src/api/index.js index 0d1ac900..387a1e12 100644 --- a/packages/health-check/src/api/index.js +++ b/packages/health-check/src/api/index.js @@ -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; } /** diff --git a/packages/health-check/src/api/verbose.js b/packages/health-check/src/api/verbose.js index 01a3a666..12acaa97 100644 --- a/packages/health-check/src/api/verbose.js +++ b/packages/health-check/src/api/verbose.js @@ -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); }; diff --git a/packages/health-check/src/utils.js b/packages/health-check/src/utils.js index d95880c1..bd6bc79c 100644 --- a/packages/health-check/src/utils.js +++ b/packages/health-check/src/utils.js @@ -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 };