Skip to content

Instantly share code, notes, and snippets.

@grabbou
Created May 3, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grabbou/904203acd8a94ea0f8a9 to your computer and use it in GitHub Desktop.
Save grabbou/904203acd8a94ea0f8a9 to your computer and use it in GitHub Desktop.
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