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 { 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,
});
}

View File

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

View File

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

View File

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