From a90f37f35ca098351607dc39a04f20f62441ea40 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Wed, 30 Oct 2013 18:58:09 -0700 Subject: [PATCH] more migration fixes, fix up metrics.js --- .../20131028_task_subdocs_tags_invites.js | 5 ++ migrations/metrics.js | 59 ++++++++++++++++--- package.json | 3 +- src/models/user.js | 19 ++---- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/migrations/20131028_task_subdocs_tags_invites.js b/migrations/20131028_task_subdocs_tags_invites.js index 63470036ec..122da3f9c7 100644 --- a/migrations/20131028_task_subdocs_tags_invites.js +++ b/migrations/20131028_task_subdocs_tags_invites.js @@ -40,6 +40,11 @@ db.users.find().forEach(function(user){ _.defaults(user.auth.timestamps, {created: new Date(user.lastCron), loggedin: new Date(user.lastCron)}); } + // Fix missing history + // ------------------------- + _.defaults(user, {history:{}}); + _.defaults(user.history,{exp:[], todos:[]}); + // Add username // ------------------------- if (!user.profile) user.profile = {name:undefined}; diff --git a/migrations/metrics.js b/migrations/metrics.js index e424cec117..2661342af4 100644 --- a/migrations/metrics.js +++ b/migrations/metrics.js @@ -1,9 +1,51 @@ -// mongo habitrpg ./migrations/metrics.js +// node habitrpg ./migrations/metrics.js +var EXPORT_EMAILS = true; +var mongo = require('mongoskin'); +var csv = require('csv'); +var _ = require('lodash'); +var moment = require('moment'); +var db = mongo.db('localhost:27017/habitrpg2?auto_reconnect'); +var twoWeeksAgo = moment().subtract(14, 'days'); +var angularRewrite = moment('07/09/2013'); + +var query = {auth: {'$exists':1}}; +var fields = {lastCron:1, 'history.exp':1, 'auth.timestamps':1, 'auth.local.email':1}; +db.collection('users').find(query, fields).toArray(function(err, items) { + if (err) return console.error({err:err}); + var stats = {total: _.size(items), lostToDerby: 0, isActive: 0}; + var emails = []; + _.each(items, function(item) { + //if (!item.history || !item.history.exp) console.log(item._id) + + //var hasBeenActive = item.history && item.history.exp && item.history.exp.length > 7; + var hasBeenActive = item.auth.timestamps && item.auth.timestamps.created && + (Math.abs(moment(item.lastCron).diff(item.auth.timestamps.created, 'd')) > 14); + + if (hasBeenActive && moment(item.lastCron).isBefore(angularRewrite)) + stats.lostToDerby++; + if (hasBeenActive && moment(item.lastCron).isAfter(twoWeeksAgo)) + stats.isActive++; + + if (item.auth.local) { + emails.push([item.auth.local.email]); + // Facebook emails. Kinda dirty, and there's only ~30 available fb emails anyway. +// } else if (item.auth.facebook && item.auth.facebook.email) { +// emails.push([item.auth.facebook.email]) +// } else if (item.auth.facebook && item.auth.facebook.emails && item.auth.facebook.emails[0] && !!item.auth.facebook.emails[0].value) { +// emails.push([item.auth.facebook.emails[0].value]) + } + }) + stats.emails = _.size(emails); + console.log(stats); + + if (EXPORT_EMAILS) + csv().from.array(emails).to.path(__dirname+'/emails.csv') +}); + +/* load('./node_modules/moment/moment.js'); - -var - today = +new Date, +var today = +new Date, twoWeeksAgo = +moment().subtract(14, 'days'); corrupt = { @@ -30,13 +72,14 @@ var { 'auth.local': { $exists: true }}, { 'auth.facebook': { $exists: true }} ], -// $where: function(){ -// return this.history && this.history.exp && this.history.exp.length > 7; -// }, + $where: function(){ + return this.history && this.history.exp && this.history.exp.length > 7; + }, 'lastCron': {$gt: twoWeeksAgo} }; print('corrupt: ' + db.users.count(corrupt)); print('unregistered: ' + db.users.count(un_registered)); print('registered: ' + db.users.count(registered)); -print('active: ' + db.users.count(active)); \ No newline at end of file +print('active: ' + db.users.count(active)); +*/ \ No newline at end of file diff --git a/package.json b/package.json index 5335c82458..15d13a6fe3 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "karma-ng-html2js-preprocessor": "~0.1.0", "karma-chai-plugins": "~0.1.0", "mocha": "~1.12.1", - "karma-mocha": "~0.1.0" + "karma-mocha": "~0.1.0", + "csv": "~0.3.6" } } diff --git a/src/models/user.js b/src/models/user.js index 728748d676..78d2276fb3 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -81,21 +81,11 @@ var UserSchema = new Schema({ rest: {type: Boolean, 'default': false} // fixme - change to preferences.resting once we're off derby }, history: { - exp: [ - { - date: Date, - value: Number - } - ], - todos: [ - { - data: Date, - value: Number - } - ] + exp: [{date: Date, value: Number}], + todos: [{data: Date, value: Number}] }, - /* FIXME remove?*/ + /* FIXME remove?*/ invitations: { guilds: {type: Array, 'default': []}, party: Schema.Types.Mixed @@ -153,14 +143,13 @@ var UserSchema = new Schema({ pets: Array }, - // FIXME store as Date? lastCron: { type: Date, 'default': Date.now }, - // FIXME remove? + // FIXME remove? party: { //party._id // FIXME make these populate docs? current: String, // party._id