fixed code review issues

This commit is contained in:
Karol Wypchlo 2020-09-09 14:53:05 +02:00
parent ca73a2f61f
commit 19b8caf81a
5 changed files with 25 additions and 15 deletions

View File

@ -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 });
};

View File

@ -1,6 +1,6 @@
const superagent = require("superagent"); const superagent = require("superagent");
const { StatusCodes } = require("http-status-codes"); const { StatusCodes } = require("http-status-codes");
const { getTimeDiff } = require("../utils"); const { calculateElapsedTime } = require("../utils");
// uploadCheck returns the result of uploading a sample file // uploadCheck returns the result of uploading a sample file
async function uploadCheck(done) { async function uploadCheck(done) {
@ -16,7 +16,7 @@ async function uploadCheck(done) {
name: "upload_file", name: "upload_file",
up: statusCode === StatusCodes.OK, up: statusCode === StatusCodes.OK,
statusCode, statusCode,
time: getTimeDiff(time), time: calculateElapsedTime(time),
critical: true, critical: true,
}); });
}); });
@ -40,7 +40,7 @@ async function downloadCheck(done) {
name: "download_file", name: "download_file",
up: statusCode === StatusCodes.OK, up: statusCode === StatusCodes.OK,
statusCode, statusCode,
time: getTimeDiff(time), time: calculateElapsedTime(time),
critical: true, critical: true,
}); });
} }

View File

@ -1,8 +1,8 @@
const superagent = require("superagent"); const superagent = require("superagent");
const hash = require("object-hash"); const hash = require("object-hash");
const { detailedDiff } = require("deep-object-diff"); const { detailedDiff } = require("deep-object-diff");
const { isEqual, isEmpty } = require("lodash"); const { isEqual } = require("lodash");
const { getTimeDiff } = require("../utils"); const { calculateElapsedTime } = require("../utils");
// audioExampleCheck returns the result of trying to download the skylink // audioExampleCheck returns the result of trying to download the skylink
// for the Example audio file on siasky.net // for the Example audio file on siasky.net
@ -564,30 +564,31 @@ function skylinkVerification(done, { name, skylink, bodyHash, metadata }) {
.responseType("blob") .responseType("blob")
.then( .then(
(response) => { (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 = {}; const info = {};
// Check if the response body is valid by checking against the known hash // 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; 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 // 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"]); response.header["skynet-file-metadata"] && JSON.parse(response.header["skynet-file-metadata"]);
if (!isEqual(expectedMetadata, metadata)) { if (!isEqual(currentMetadata, metadata)) {
entry.up = false; 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 done(entry); // Return the entry information
}, },
(error) => { (error) => {
const statusCode = error.statusCode || error.status; 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 done(entry); // Return the entry information
} }

View File

@ -20,6 +20,7 @@ server.use(bodyparser.json());
server.get("/health-check", require("./api/index")); server.get("/health-check", require("./api/index"));
server.get("/health-check/recent", require("./api/recent")); server.get("/health-check/recent", require("./api/recent"));
server.get("/health-check/all", require("./api/all")); server.get("/health-check/all", require("./api/all"));
server.get("/health-check/disabled", require("./api/disabled"));
server.listen(port, host, (error) => { server.listen(port, host, (error) => {
if (error) throw error; if (error) throw error;

View File

@ -1,8 +1,8 @@
// return the time between start and now in milliseconds // return the time between start and now in milliseconds
function getTimeDiff(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 = { getTimeDiff }; module.exports = { calculateElapsedTime };