This is a little net health monitoring project I made in NodeJS. It pings Google every second and stores the result (true/false) along with the date in a database (MongoDB). I've collected enough data and I want to represent it for the past week.
I created a 2x2 array of 7 days and 24 hours each. I go through each record and map it to the appropriate day and hour of the week. And I give that hour a little score: if true +1, if false -5. And it creates a little matrix like this:
[
[2015, 1724, 1733, 1679, ...
[1818, 1909, 1614, 1829, ...
[1632, 1778, 1726, 1657, ...
...
]
The only problem is computation takes ~10-15 seconds and lots of CPU for going through ~200k records of just 3 days.
Is there some scope for improvement here?
days = days || 3;
var counter = 0
this.find({
date: {
// Date greater than (Now - X days)
$gt: new Date(Date.now() - days * 24 * 3600 * 1000),
},
}, function(err, datums) {
var week = new Array(days);
for (var i = 0; i < week.length; i++)
week[i] = new Array(24);
// Empty Week Matrix (7x24)
datums.forEach(function(data) {
counter++;
var date1 = data.date;
var date2 = new Date();
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
//stackoverflow.com/q/3224834/1266650
var day = days - diffDays;
if (day < 0) return;
var hour = data.date.getHours();
if (!week[day][hour]) week[day][hour] = 0;
if (data.isNetAlive)
week[day][hour]++;
else
week[day][hour] -= 5;
});
console.debug(week);
console.debug(counter, 'records');
});