codecombat/scripts/analytics/mongodb/queries/LevelPlayedBeforeSub.js

75 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-12-16 17:08:16 -05:00
// Find completed level counts immediately before subscription purchase
// Usage:
// mongo <address>:<port>/<database> <script file> -u <username> -p <password>
// Usage restricted to HoC Dates:
// mongo <address>:<port>/<database> -eval "var startDate='2014-12-08T00:00:00.000Z', endDate='2014-12-14T00:00:00.000Z';" <script file> -u <username> -p <password>
2015-01-28 23:04:47 -05:00
// TODO: query against _id instead of created
2014-12-16 17:08:16 -05:00
// TODO: Must be a better way to query for this data all at once.
2015-01-28 23:04:47 -05:00
var startTime = new Date();
2014-12-16 17:08:16 -05:00
var lastLevelCompleted = {};
var paymentsCursor;
if (typeof startDate !== "undefined" && startDate !== null && typeof endDate !== "undefined" && endDate !== null) {
print("Using dates " + startDate + " to " + endDate);
2015-01-28 23:04:47 -05:00
paymentsCursor = db.payments.find({
2014-12-16 17:08:16 -05:00
$and: [
{"created": { $gte: startDate}},
{"created": { $lt: endDate}},
{"stripe.subscriptionID" : { "$exists" : true }}
]
2015-01-28 23:04:47 -05:00
});
2014-12-16 17:08:16 -05:00
} else {
2015-01-28 23:04:47 -05:00
print("No date range specified");
paymentsCursor = db.payments.find({"stripe.subscriptionID": { "$exists" : true }});
2014-12-16 17:08:16 -05:00
}
while (paymentsCursor.hasNext()) {
2015-01-28 23:04:47 -05:00
var doc = paymentsCursor.next();
var purchaseDate = doc.created;
var user = doc.purchaser.valueOf();
// print("Processing purchase on " + purchaseDate + " for " + user);
// Find last level session completed
var levelSessionCursor = db['level.sessions'].find({
$and: [{"state.complete" : true}, {creator : user}, {changed: {$lt: ISODate(purchaseDate)}}]
}).sort({created: -1});
if (levelSessionCursor.hasNext()) {
var lastLevelSessionCompleted = levelSessionCursor.next();
// Find last level completed
var levelCursor = db.levels.find({"original" : ObjectId(lastLevelSessionCompleted.level.original), "version.isLatestMajor": true, "version.isLatestMinor": true})
if (levelCursor.hasNext()) {
var lastLevel = levelCursor.next();
if (!lastLevelCompleted[lastLevel.name]) lastLevelCompleted[lastLevel.name] = 0;
lastLevelCompleted[lastLevel.name]++;
2014-12-16 17:08:16 -05:00
}
else {
2015-01-28 23:04:47 -05:00
if (!lastLevelCompleted['unknown']) lastLevelCompleted['unknown'] = 0;
lastLevelCompleted['unknown']++;
2014-12-16 17:08:16 -05:00
}
2015-01-28 23:04:47 -05:00
}
else {
if (!lastLevelCompleted['unknown']) lastLevelCompleted['unknown'] = 0;
lastLevelCompleted['unknown']++;
}
2014-12-16 17:08:16 -05:00
}
// Sort descending count and print
var sorted = [];
2015-01-28 23:04:47 -05:00
var total = 0;
2014-12-16 17:08:16 -05:00
for (key in lastLevelCompleted) {
2015-01-28 23:04:47 -05:00
sorted.push({name: key, count: lastLevelCompleted[key]});
total += lastLevelCompleted[key];
2014-12-16 17:08:16 -05:00
}
sorted.sort(function(a,b) { return b.count - a.count});
for (var i = 0; i < sorted.length; i++) {
2015-01-28 23:04:47 -05:00
print(sorted[i].count + "\t" + (sorted[i].count / total * 100).toFixed(2) + "%\t" + sorted[i].name);
2014-12-16 17:08:16 -05:00
}
2015-01-28 23:04:47 -05:00
print("Runtime: " + (new Date() - startTime) + "ms");