Skip to content

Instantly share code, notes, and snippets.

@snowkidind
Created September 17, 2019 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowkidind/270ec3ece3cdb10e0a3851cedb3b158e to your computer and use it in GitHub Desktop.
Save snowkidind/270ec3ece3cdb10e0a3851cedb3b158e to your computer and use it in GitHub Desktop.
module.exports = {
// given an array of timestamps only, returns a set of relevant timestamps
// to previous hours, only allowing one timestamp per hour
hourlyTimestamps: function (array){
const now = new Date().getTime();
let msh = 3.6e+6; // milliseconds per hour
let hoursGone = 0;
let theseHours = []; // array of representative data on an hourly basis, 0 if no data
let analysisDuration = 168; // hours in a given week.
let calc = [];
for (let i = 0; i < analysisDuration; i++) {
const lastHour = now - hoursGone; // iterated hour. new entries must be > lastHour - msh
let found = false;
for (let j = 0; j < array.length; j++) {
if (array[j] <= lastHour && array[j] > lastHour - msh && !found) {
theseHours[i] = array[j]; // add most recent only
calc[i] = {l: lastHour, ts: array[j], d: lastHour - msh};
found = true;
}
}
if (!found) {
calc[i] = {l: lastHour, ts: 0, d: lastHour - msh};
theseHours[i] = 0;
}
hoursGone += msh;
}
return ({set: theseHours, audit: calc})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment