Created
January 6, 2015 19:38
-
-
Save barisusakli/a00ad1d4be70168b075b to your computer and use it in GitHub Desktop.
get user logins between dates
This file contains 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 nconf = require('nconf'); | |
var async = require('async'); | |
var fs = require('fs'); | |
nconf.file({ | |
file: 'config.json' | |
}); | |
var db = require('./src/database'), | |
batch = require('./src/batch'); | |
var data = []; | |
var count = 0; | |
var startDate = new Date(2014, 11, 1); | |
var endDate = new Date(2015, 1, 1); | |
db.init(function(err) { | |
if (err) { | |
console.log("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + err.message); | |
process.exit(); | |
} | |
batch.processSortedSet('users:joindate', function(uids, next) { | |
async.each(uids, processUid, function(err) { | |
if (err) { | |
return next(err); | |
} | |
count += uids.length; | |
console.log('finished ' + count); | |
next(); | |
}); | |
}, function(err) { | |
if (err) { | |
console.log(err); | |
process.exit(); | |
return; | |
} | |
fs.writeFile('login_data.json', JSON.stringify(data, null, 4), function(err) { | |
if (err) { | |
console.log(err); | |
process.exit(); | |
return; | |
} | |
console.log('done, total uids : ', data.length); | |
process.exit(); | |
}); | |
}); | |
}); | |
function processUid(uid, next) { | |
db.sortedSetCount('uid:' + uid + ':ip', startDate.getTime(), endDate.getTime(), function(err, count) { | |
if (err) { | |
return next(err); | |
} | |
if (!count) { | |
return next(); | |
} | |
async.parallel({ | |
userData: function(next) { | |
db.getObjectFields('user:' + uid, ['uid', 'username', 'email'], next); | |
}, | |
times: function(next) { | |
db.getSortedSetRevRangeByScoreWithScores('uid:' + uid + ':ip', 0, 1, endDate.getTime(), startDate.getTime(), next); | |
} | |
}, function(err, results) { | |
if (err) { | |
return next(err); | |
} | |
if (results.times && results.times.length) { | |
var d = new Date(); | |
d.setTime(results.times[0].score); | |
results.userData.lastLogin = d.toUTCString(); | |
} | |
data.push(results.userData); | |
next(); | |
}); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment