Skip to content

Instantly share code, notes, and snippets.

@cedrickvstheworld
Last active September 22, 2019 09:34
Show Gist options
  • Save cedrickvstheworld/af768a79d36dddf2aea45bb6f45ed34c to your computer and use it in GitHub Desktop.
Save cedrickvstheworld/af768a79d36dddf2aea45bb6f45ed34c to your computer and use it in GitHub Desktop.
function getPeakHours(feeds, consideration) {
const feedCount = feeds.length
// check if all feeds has a length of 12
for (let i in feeds) {
if (feeds[i].length !== 24) {
return 'rejected: feed must have a length of 24'
}
}
// get hour averages
let hourAverages = []
for (let i in [...Array(24).keys()]) {
i = parseInt(i)
let iHourSum = 0
for (let ii in feeds) {
iHourSum += feeds[ii][i]
}
let hourAvg = iHourSum/feedCount
hourAverages.push(hourAvg)
}
// get the highest count
let highestCount = Math.max(...hourAverages)
let considerationFromMax = highestCount * consideration
// collect peak hours
let peakHours = []
for (let i in hourAverages) {
if (hourAverages[i] >= considerationFromMax) {
peakHours.push({hour: i, count: hourAverages[i]})
}
}
// group peak hours to create peak hour ranges
let peakHourRanges = []
let i = 0
let hourRange = []
while (i < peakHours.length) {
if (peakHours.length === 1) {
peakHourRanges.push([peakHours[i]])
break
}
if (hourRange.length === 0) {
hourRange.push(peakHours[i])
i++
continue
}
if (parseInt(hourRange[hourRange.length - 1].hour) + 1 === parseInt(peakHours[i].hour)) {
hourRange.push(peakHours[i])
if (i === peakHours.length - 1) {
peakHourRanges.push(hourRange)
}
}
else {
peakHourRanges.push(hourRange)
hourRange = []
}
if (hourRange.length === 0) {
if (hourRange[hourRange.length -1] === peakHours[i]) {
i++
continue
}
hourRange.push(peakHours[i])
peakHourRanges.push(hourRange)
}
i++
}
peakHourRanges = [...new Set(peakHourRanges)]
function convertToStandard(militaryHour) {
if (militaryHour > 24 || militaryHour < 0) {
return false
}
let suffix = militaryHour < 12 || militaryHour === 24 ? 'AM' : 'PM'
let standardTime = militaryHour
if (militaryHour === 0) {
standardTime = 12
}
if (militaryHour > 12) {
standardTime = militaryHour % 12
standardTime = standardTime === 0 ? 12 : standardTime
}
return `${standardTime}${suffix}`
}
let output = []
for (let i in peakHourRanges) {
let x = {}
if (peakHourRanges[i].length === 1) {
x.hour = `${convertToStandard(parseInt(peakHourRanges[i][0].hour))} - ` +
`${convertToStandard(parseInt(peakHourRanges[i][0].hour) + 1)}`
x.countAvg = peakHourRanges[i][0].count
output.push(x)
continue
}
x.hour = `${convertToStandard(parseInt(peakHourRanges[i][0].hour))} - ` +
`${convertToStandard(parseInt(peakHourRanges[i][peakHourRanges[i].length -1].hour) + 1)}`
x.countAvg = 0
for (let ii in peakHourRanges[i]) {
x.countAvg += peakHourRanges[i][ii].count
}
output.push(x)
}
return output
}
// sample feed
feeds = [
[0, 45, 22, 45, 60, 65, 78, 22, 0, 11, 34, 40, 2, 45, 21, 62, 40, 55, 32, 65, 69, 54, 4, 60],
[2, 35, 21, 71, 50, 55, 22, 65, 29, 14, 4, 60, 22, 35, 71, 41, 50, 25, 72, 35, 29, 44, 24, 40]
]
console.log(getPeakHours(feeds, .79))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment