diff --git a/packages/health-check/src/api/disabled.js b/packages/health-check/src/api/disabled.js new file mode 100644 index 00000000..6471ad2f --- /dev/null +++ b/packages/health-check/src/api/disabled.js @@ -0,0 +1,8 @@ +const db = require("../db"); + +// returns a disabled flag status +module.exports = (req, res) => { + const disabled = db.get("disabled").value(); + + res.send({ disabled }); +}; diff --git a/packages/health-check/src/checks/critical.js b/packages/health-check/src/checks/critical.js index f737995f..d8e41251 100644 --- a/packages/health-check/src/checks/critical.js +++ b/packages/health-check/src/checks/critical.js @@ -1,6 +1,6 @@ const superagent = require("superagent"); const { StatusCodes } = require("http-status-codes"); -const { getTimeDiff } = require("../utils"); +const { calculateElapsedTime } = require("../utils"); // uploadCheck returns the result of uploading a sample file async function uploadCheck(done) { @@ -16,7 +16,7 @@ async function uploadCheck(done) { name: "upload_file", up: statusCode === StatusCodes.OK, statusCode, - time: getTimeDiff(time), + time: calculateElapsedTime(time), critical: true, }); }); @@ -40,7 +40,7 @@ async function downloadCheck(done) { name: "download_file", up: statusCode === StatusCodes.OK, statusCode, - time: getTimeDiff(time), + time: calculateElapsedTime(time), critical: true, }); } diff --git a/packages/health-check/src/checks/verbose.js b/packages/health-check/src/checks/verbose.js index 26c5ab71..3374cd0a 100644 --- a/packages/health-check/src/checks/verbose.js +++ b/packages/health-check/src/checks/verbose.js @@ -1,8 +1,8 @@ const superagent = require("superagent"); const hash = require("object-hash"); const { detailedDiff } = require("deep-object-diff"); -const { isEqual, isEmpty } = require("lodash"); -const { getTimeDiff } = require("../utils"); +const { isEqual } = require("lodash"); +const { calculateElapsedTime } = require("../utils"); // audioExampleCheck returns the result of trying to download the skylink // for the Example audio file on siasky.net @@ -564,30 +564,31 @@ function skylinkVerification(done, { name, skylink, bodyHash, metadata }) { .responseType("blob") .then( (response) => { - const entry = { name, up: true, statusCode: response.statusCode, time: getTimeDiff(time) }; + const entry = { name, up: true, statusCode: response.statusCode, time: calculateElapsedTime(time) }; const info = {}; // Check if the response body is valid by checking against the known hash - if (hash(response.body) !== bodyHash) { + const currentBodyHash = hash(response.body); + if (currentBodyHash !== bodyHash) { entry.up = false; - info.body = { valid: false, hash: { expected: bodyHash, current: hash(response.body) } }; + info.bodyHash = { expected: bodyHash, current: currentBodyHash }; } // Check if the metadata is valid by deep comparing expected value with response - const expectedMetadata = + const currentMetadata = response.header["skynet-file-metadata"] && JSON.parse(response.header["skynet-file-metadata"]); - if (!isEqual(expectedMetadata, metadata)) { + if (!isEqual(currentMetadata, metadata)) { entry.up = false; - info.metadata = { valid: false, diff: detailedDiff(expectedMetadata, metadata) }; + info.metadata = detailedDiff(currentMetadata, metadata); } - if (!isEmpty(info)) entry.info = info; // add info only if it exists + if (Object.keys(info).length) entry.info = info; // add info only if it exists done(entry); // Return the entry information }, (error) => { const statusCode = error.statusCode || error.status; - const entry = { name, up: false, statusCode, time: getTimeDiff(time) }; + const entry = { name, up: false, statusCode, time: calculateElapsedTime(time) }; done(entry); // Return the entry information } diff --git a/packages/health-check/src/index.js b/packages/health-check/src/index.js index f39c3308..28838845 100644 --- a/packages/health-check/src/index.js +++ b/packages/health-check/src/index.js @@ -20,6 +20,7 @@ server.use(bodyparser.json()); server.get("/health-check", require("./api/index")); server.get("/health-check/recent", require("./api/recent")); server.get("/health-check/all", require("./api/all")); +server.get("/health-check/disabled", require("./api/disabled")); server.listen(port, host, (error) => { if (error) throw error; diff --git a/packages/health-check/src/utils.js b/packages/health-check/src/utils.js index e6cc72f4..d95880c1 100644 --- a/packages/health-check/src/utils.js +++ b/packages/health-check/src/utils.js @@ -1,8 +1,8 @@ // return the time between start and now in milliseconds -function getTimeDiff(start) { +function calculateElapsedTime(start) { const diff = process.hrtime(start); return Math.round((diff[0] * 1e9 + diff[1]) / 1e6); // msec } -module.exports = { getTimeDiff }; +module.exports = { calculateElapsedTime };