Created
October 30, 2020 20:46
-
-
Save someshkoli/7c6e4c86dbd5ed6915ecc157915f6664 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
router.get('/heatmap/:compCode/:compLocationCode/:interval', (req, res) => { | |
customerCountController.getHeatMap({...req.params}, (result, error) => { | |
if (error != null) { | |
req.status(400).json(error) | |
} | |
req.status(200).json(result) | |
}) | |
}) | |
==========newfile============== | |
const CUSTOMERCOUNT = require('../models/CustomerCount'); | |
let timeDuration = { | |
day: 1 * 60 * 60 * 24 * 1000 | |
} | |
timeDuration.week = timeDuration.day * 7 | |
timeDuration.month = timeDuration.day * 30 | |
timeDuration.year = timeDuration.month * 12 | |
// intervals: day, week, month, year | |
module.exports.getHeatMap = async ({ | |
compCode, | |
compLocationCode, | |
interval, | |
}, callback) => { | |
CUSTOMERCOUNT.find({ | |
compLocationCode, | |
compCode, | |
entryTime: { | |
$gte: new Date(new Date() - timeDuration[interval]) | |
} | |
}) | |
.exec() | |
.then(counts => { | |
let result = [] | |
let durationRecord = {} | |
counts.forEach(count => { | |
let date = count.entryTime.get; | |
let currentTime; | |
switch (interval) { | |
case 'day': | |
currentTime = date.getHour() | |
break; | |
case 'week': | |
currentTime = date.getDay() | |
break; | |
case 'month': | |
let currentHour = date.getDate() | |
break; | |
case 'year': | |
let currentHour = date.getMonth() | |
break; | |
default: | |
break; | |
} | |
durationRecord[currentTime] = durationRecord[currentTime] ? | |
durationRecord[currentTime] + count.totalPeopleCount : | |
count.totalPeopleCount | |
}) | |
Object.keys(durationRecord).forEach(timeD => { | |
result.push({ | |
time: timeD, | |
count: durationRecord[timeD] | |
}) | |
}); | |
callback(result, null) | |
}) | |
.catch(err => { | |
callback([], err) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment