Created
September 17, 2019 05:56
-
-
Save snowkidind/270ec3ece3cdb10e0a3851cedb3b158e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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