Created
May 3, 2015 14:01
-
-
Save grabbou/904203acd8a94ea0f8a9 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
var _ = require('lodash'); | |
Statuses | |
.findAll() | |
.sort({ | |
'$date': -1 //check if $ should be here and if -1 is the order you want | |
}) | |
.then(function(statuses) { | |
var timeOn = 0; | |
var previousStatus = void 0; | |
_.each(statuses, function(record) { | |
var date = record.date.getTime(); //epoch millis | |
// If first recorded event is falsy and there are no truthy events before it, do nothing as there's nothing to calculate | |
if (record.status === 'away' && !previousStatus) return; | |
// If status is away - let's calculate the difference between the previousStatus (that was `active`) to | |
// know how long user was using the app | |
// and make previousStatus undefined again | |
if (record.status === 'away') { | |
timeOn += (date - previousState); | |
previousState = void 0; | |
// otherwise, set previousState to currentDate so we know when user started using the app | |
} else { | |
previousState = date; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment