This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/health-check/src/utils.js

48 lines
1021 B
JavaScript
Raw Normal View History

2020-09-10 12:03:56 +00:00
/**
* Get the time between start and now in milliseconds
*/
2020-09-09 12:53:05 +00:00
function calculateElapsedTime(start) {
2020-09-09 12:25:00 +00:00
const diff = process.hrtime(start);
return Math.round((diff[0] * 1e9 + diff[1]) / 1e6); // msec
}
2020-09-10 12:03:56 +00:00
/**
* 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();
}
/**
* Get response from response object if available
*/
function getResponseContent(response) {
try {
2021-04-27 16:30:51 +00:00
return JSON.parse(response?.body || response?.text);
} catch {
2021-04-27 16:30:51 +00:00
return response?.body || response?.text;
}
}
2020-10-10 14:04:13 +00:00
/**
* Ensures that the object serializes to JSON properly
*/
function ensureValidJSON(object) {
const replacer = (key, value) => (value === undefined ? "--undefined--" : value);
const stringified = JSON.stringify(object, replacer);
return JSON.parse(stringified);
}
module.exports = {
calculateElapsedTime,
getYesterdayISOString,
getResponseContent,
2020-10-10 14:04:13 +00:00
ensureValidJSON,
};