How to use the level.sessions function in level

To help you get started, we’ve selected a few level examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codecombat / codecombat / scripts / analytics / mongodb / queries / averageLevelPlaytimes.js View on Github external
function getPlaytimes(levelSlugs) {
  // printjson(levelSlugs);
  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}}
    ]
  }, {heroConfig: 1, levelID: 1, playtime: 1});

  var playtimes = {};
  while (cursor.hasNext()) {
    var myDoc = cursor.next();
    var levelID = myDoc.levelID;

    if (!playtimes[levelID]) playtimes[levelID] = {campaign: [], course: []};
github codecombat / codecombat / scripts / analytics / mongodb / queries / LevelPlayedBeforeSub.js View on Github external
]
  });
} else {
  print("No date range specified");
  paymentsCursor = db.payments.find({"stripe.subscriptionID": { "$exists" : true }});
}

while (paymentsCursor.hasNext()) {
  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]++;
    }
    else {
      if (!lastLevelCompleted['unknown']) lastLevelCompleted['unknown'] = 0;
      lastLevelCompleted['unknown']++;
    }
github codecombat / codecombat / scripts / analytics / mongodb / queries / abTestHelpers.js View on Github external
doc.purchased.gems &&
          !doc.stripe.free
        ) {
          countedSubscriberMap[doc._id + ''] = true;
          groupSubscribedMap[group] = (groupSubscribedMap[group] || 0) + 1;
        }
    }
    log("Fetched", Math.min(userOffset, users.length), "users");
  }
  // printjson(userGroupMap);

  log("Fetching level sessions..");
  var lsBrowserMap = {};
  var userBrowserMap = {};
  for (var sessionOffset = 0; sessionOffset < levelSessions.length; sessionOffset += 1000) {
    cursor = db['level.sessions'].find({_id : {$in: levelSessions.slice(sessionOffset, sessionOffset + 1000)}});
    while (cursor.hasNext()) {
      var doc = cursor.next();
      var user = doc._id.valueOf();
      var browser = doc.browser;
      var browserInfo = '';
      if (browser && browser.platform) {
        browserInfo += browser.platform;
      }
      if (browser && browser.name) {
        browserInfo += browser.name;
      }
      if (browserInfo.length > 0) {
        lsBrowserMap[doc._id.valueOf()] = browserInfo;
        userBrowserMap[user] = browserInfo;
      }
    }
github codecombat / codecombat / scripts / analytics / mongodb / queries / levelRates.js View on Github external
"_id" : 0,
    "levelID" : 1,
    "day": {"$substr" :  ["$created", 0, 10]}
  }};

  var group = {"$group" : {
    "_id" : {
      "day" : "$day",
      "level": "$levelID"
    },
    "count" : {
      "$sum" : 1
    }
  }};

  var cursor = db['level.sessions'].aggregate(match, proj0, group);

  var levelPlaytimeData = {};
  while (cursor.hasNext()) {
    var doc = cursor.next();
    var day = doc._id.day;
    var level = doc._id.level;
    if (!levelPlaytimeData[level]) levelPlaytimeData[level] = {};
    levelPlaytimeData[level][day] = doc.count;
  }

  for (levelIndex in levelRates) {
    for (dateIndex in levelRates[levelIndex]) {
      var level = levelRates[levelIndex][dateIndex].level;
      var day = levelRates[levelIndex][dateIndex].day;
      if (levelPlaytimeData[level] && levelPlaytimeData[level][day]) {
        levelRates[levelIndex][dateIndex].codeProblems = levelPlaytimeData[level][day];