Skip to content

Instantly share code, notes, and snippets.

@reinpk

reinpk/cohort.js Secret

Created November 26, 2013 23:53
Show Gist options
  • Save reinpk/18bee9ca87c01d245ab0 to your computer and use it in GitHub Desktop.
Save reinpk/18bee9ca87c01d245ab0 to your computer and use it in GitHub Desktop.
var _ = require('underscore')
, common = require('../../common')
, db = require('../../db')
, utils = require('../../utils')
, async = require('async')
, es = require('event-stream');
common.init(function () {
var start = new Date(process.argv[2]),
end = new Date(process.argv[3]);
// Users
// -----
var signups = 0,
signupsWithProjects = 0;
// create the user stream
var userStream = db.users.getByCreated(start, end);
// listen on the stream
userStream.on('data', function (user) {
signups++;
if (_.size(user.projects) > 0) signupsWithProjects++;
});
userStream.on('error', console.log);
// when the stream finishes, print results
userStream.on('end', function () {
console.log('Signups: ' + signups);
console.log('Signups with projects: ' + signupsWithProjects);
});
// Projects
// --------
// create the project to project state map
var projectToState = function (project, callback) {
db.projects.daysWithEventsCount(project.id, { start : project.created }, callback);
};
// create the projects stream
var projectStream = db.projects.getByCreated(start, end).pipe(es.map(projectToState));
// listen on the stream
var projectStates = [];
projectStream.on('data', function (projectState) {
projectStates.push(projectState);
});
projectStream.on('error', console.log);
// when the stream finishes, print results
projectStream.on('end', function () {
console.log('Projects: ' + projectStates.length);
var active = _.filter(projectStates, function (state) {
return state.sentToday;
});
var noData = _.filter(projectStates, function (state) {
return (!state.daysWithData);
});
var churnedTest = _.filter(projectStates, function (state) {
return (!state.sentToday && state.daysWithData <= 3 && state.daysWithData > 0);
});
var churnedSerious = _.filter(projectStates, function (state) {
return (!state.sentToday && state.daysWithData > 3);
});
var setupDays = _.chain(projectStates)
.filter(function (state) {
return (state.daysUntilFirstEvent !== undefined);
})
.map(function (state) {
return state.daysUntilFirstEvent;
})
.value();
var idMapper = function (state) {
return state.projectId;
};
console.log({
active : {
count : active.length,
slugs : JSON.stringify(_.map(active, idMapper))
},
noData : {
count : noData.length,
slugs : JSON.stringify(_.map(noData, idMapper))
},
churnedTest : {
count : churnedTest.length,
slugs : JSON.stringify(_.map(churnedTest, idMapper))
},
churnedSerious : {
count : churnedSerious.length,
slugs : JSON.stringify(_.map(churnedSerious, idMapper))
},
setupDays : JSON.stringify(setupDays)
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment