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

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-12-15 19:37:42 -05:00
// Average level playtimes in seconds by campaign, broken up by course and campaign levels
2015-06-11 17:32:10 -04:00
// Usage:
// mongo <address>:<port>/<database> <script file> -u <username> -p <password>
// NOTE: faster to use find() instead of aggregate()
2015-12-15 19:37:42 -05:00
// NOTE: faster to ask for one level at a time.
2015-06-11 17:32:10 -04:00
2015-12-15 19:37:42 -05:00
var courseCampaigns = ['intro', 'course-2', 'course-3', 'course-4'];
var individualCampaigns = ['dungeon', 'forest', 'desert', 'mountain'];
2015-06-11 17:32:10 -04:00
var scriptStartTime = new Date();
2015-12-15 19:37:42 -05:00
var startDay = '2015-12-06';
var endDay = '2015-12-13';
2015-06-11 17:32:10 -04:00
2015-12-15 19:37:42 -05:00
print("Dates: " + startDay + " to " + endDay);
2015-06-11 17:32:10 -04:00
// Print out playtimes for each campaign
2015-12-15 19:37:42 -05:00
var campaigns = getCampaigns(courseCampaigns);
2015-06-11 17:32:10 -04:00
for (var i = 0; i < campaigns.length; i++) {
var campaign = campaigns[i];
2015-12-15 19:37:42 -05:00
print(campaign.slug);
print("Sessions\tAverage\tSessions\tAverage\tLevel");
for (var j = 0; j < campaign.levelSlugs.length; j++) {
var levelSlug = campaign.levelSlugs[j];
var levelPlaytimes = getPlaytimes([levelSlug]);
if (levelPlaytimes[levelSlug]) {
print(levelPlaytimes[levelSlug].campaign.count,
'\t', levelPlaytimes[levelSlug].campaign.average,
'\t', levelPlaytimes[levelSlug].course.count,
'\t', levelPlaytimes[levelSlug].course.average,
'\t', levelSlug);
2015-06-11 17:32:10 -04:00
}
else {
2015-12-15 19:37:42 -05:00
print(0, '\t', 0, '\t', 0, '\t', 0, '\t', levelSlug);
2015-06-11 17:32:10 -04:00
}
}
// break;
}
log("Script runtime: " + (new Date() - scriptStartTime));
function log(str) {
print(new Date().toISOString() + " " + str);
}
function objectIdWithTimestamp(timestamp) {
// Convert string date to Date object (otherwise assume timestamp is a date)
if (typeof(timestamp) == 'string') timestamp = new Date(timestamp);
// Convert date object to hex seconds since Unix epoch
var hexSeconds = Math.floor(timestamp/1000).toString(16);
// Create an ObjectId with that hex timestamp
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
}
2015-12-15 19:37:42 -05:00
function getCampaigns(campaignSlugs) {
2015-06-11 17:32:10 -04:00
var campaigns = [];
2015-12-15 19:37:42 -05:00
var cursor = db.campaigns.find({slug: {$in: campaignSlugs}}, {slug: 1, levels: 1});
2015-06-11 17:32:10 -04:00
var allFree = 0;
var allpaid = 0;
while (cursor.hasNext()) {
var doc = cursor.next();
if (doc.slug === 'auditions') continue;
2015-12-15 19:37:42 -05:00
var campaign = {slug: doc.slug, levelSlugs: []};
2015-06-11 17:32:10 -04:00
for (var levelID in doc.levels) {
2015-12-15 19:37:42 -05:00
campaign.levelSlugs.push(doc.levels[levelID].slug);
2015-06-11 17:32:10 -04:00
}
campaigns.push(campaign);
}
2015-12-15 19:37:42 -05:00
campaigns.sort(function (a, b) {
if (campaignSlugs.indexOf(a.slug) < campaignSlugs.indexOf(b.slug)){
return -1;
}
return 1;
});
2015-06-11 17:32:10 -04:00
return campaigns;
}
function getPlaytimes(levelSlugs) {
2015-12-15 19:37:42 -05:00
// printjson(levelSlugs);
2015-06-11 17:32:10 -04:00
var startObj = objectIdWithTimestamp(ISODate(startDay + "T00:00:00.000Z"));
var endObj = objectIdWithTimestamp(ISODate(endDay + "T00:00:00.000Z"))
var cursor = db['level.sessions'].find({
$and:
[
{"state.complete": true},
{"playtime": {$gt: 0}},
{levelID: {$in: levelSlugs}},
{_id: {$gte: startObj}},
{_id: {$lt: endObj}}
]
2015-12-15 19:37:42 -05:00
}, {heroConfig: 1, levelID: 1, playtime: 1});
2015-06-11 17:32:10 -04:00
var playtimes = {};
while (cursor.hasNext()) {
var myDoc = cursor.next();
var levelID = myDoc.levelID;
2015-12-15 19:37:42 -05:00
if (!playtimes[levelID]) playtimes[levelID] = {campaign: [], course: []};
if (myDoc.heroConfig) {
playtimes[levelID].campaign.push(myDoc.playtime);
}
else {
playtimes[levelID].course.push(myDoc.playtime);
}
2015-06-11 17:32:10 -04:00
}
2015-12-15 19:37:42 -05:00
// printjson(playtimes);
2015-06-11 17:32:10 -04:00
var data = {};
for (levelID in playtimes) {
2015-12-15 19:37:42 -05:00
var campaignTotal = 0;
var courseTotal = 0;
if (playtimes[levelID].campaign.length > 0) {
campaignTotal = playtimes[levelID].campaign.reduce(function(a, b) {return a + b;});
}
if (playtimes[levelID].course.length > 0) {
courseTotal = playtimes[levelID].course.reduce(function(a, b) {return a + b;});
}
var campaignAverage = parseInt(playtimes[levelID].campaign.length > 0 ? parseInt(campaignTotal / playtimes[levelID].campaign.length): 0);
var courseAverage = parseInt(playtimes[levelID].course.length > 0 ? parseInt(courseTotal / playtimes[levelID].course.length): 0);
data[levelID] = {
campaign: {
count: playtimes[levelID].campaign.length,
total: campaignTotal,
average: campaignAverage
},
course: {
count: playtimes[levelID].course.length,
total: courseTotal,
average: courseAverage
}
};
2015-06-11 17:32:10 -04:00
}
return data;
}