<commit_before>LevelSession = require './LevelSession'
Handler = require '../../commons/Handler'
log = require 'winston'

TIMEOUT = 1000 * 30 # no activity for 30 seconds means it's not active

class LevelSessionHandler extends Handler
  modelClass: LevelSession

  getByRelationship: (req, res, args...) ->
    return @getActiveSessions req, res if args.length is 2 and args[1] is 'active'
    return @getRecentSessions req, res if args.length is 2 and args[1] is 'recent'
    return @getCodeLanguageCounts req, res if args[1] is 'code_language_counts'
    super(arguments...)

  formatEntity: (req, document) ->
    documentObject = super(req, document)
    if req.user?.isAdmin() or
       req.user?.id is document.creator or
       ('employer' in (req.user?.get('permissions') ? [])) or
       not document.submittedCode  # TODO: only allow leaderboard access to non-top-5 solutions
      return documentObject
    else
      return _.omit documentObject, @privateProperties

  getActiveSessions: (req, res) ->
    return @sendForbiddenError(res) unless req.user?.isAdmin()
    start = new Date()
    start = new Date(start.getTime() - TIMEOUT)
    query = @modelClass.find({'changed': {$gt: start}})
    query.exec (err, documents) =>
      return @sendDatabaseError(res, err) if err
      documents = (@formatEntity(req, doc) for doc in documents)
      @sendSuccess(res, documents)

  getRecentSessions: (req, res) ->
    return @sendForbiddenError(res) unless req.user?.isAdmin()

    levelSlug = req.query.slug or req.body.slug
    limit = req.query.limit or req.body.limit or 7

    return @sendSuccess res, [] unless levelSlug?

    today = new Date()
    today.setUTCMinutes(today.getUTCMinutes() - 10)
    queryParams = {$and: [{"changed": {"$lt": today}}, {"levelID": levelSlug}]}<commit_msg>Fix analytics query finding limited number of recent sessions<commit_after>LevelSession = require './LevelSession'
Handler = require '../../commons/Handler'
log = require 'winston'

TIMEOUT = 1000 * 30 # no activity for 30 seconds means it's not active

class LevelSessionHandler extends Handler
  modelClass: LevelSession

  getByRelationship: (req, res, args...) ->
    return @getActiveSessions req, res if args.length is 2 and args[1] is 'active'
    return @getRecentSessions req, res if args.length is 2 and args[1] is 'recent'
    return @getCodeLanguageCounts req, res if args[1] is 'code_language_counts'
    super(arguments...)

  formatEntity: (req, document) ->
    documentObject = super(req, document)
    if req.user?.isAdmin() or
       req.user?.id is document.creator or
       ('employer' in (req.user?.get('permissions') ? [])) or
       not document.submittedCode  # TODO: only allow leaderboard access to non-top-5 solutions
      return documentObject
    else
      return _.omit documentObject, @privateProperties

  getActiveSessions: (req, res) ->
    return @sendForbiddenError(res) unless req.user?.isAdmin()
    start = new Date()
    start = new Date(start.getTime() - TIMEOUT)
    query = @modelClass.find({'changed': {$gt: start}})
    query.exec (err, documents) =>
      return @sendDatabaseError(res, err) if err
      documents = (@formatEntity(req, doc) for doc in documents)
      @sendSuccess(res, documents)

  getRecentSessions: (req, res) ->
    return @sendForbiddenError(res) unless req.user?.isAdmin()

    levelSlug = req.query.slug or req.body.slug
    limit = parseInt req.query.limit or req.body.limit or 7

    return @sendSuccess res, [] unless levelSlug?

    today = new Date()
    today.setUTCMinutes(today.getUTCMinutes() - 10)
    queryParams = {$and: [{"changed": {"$lt": today}}, {"levelID": levelSlug}]}
{'fraction_non_alphanumeric': 0.12319433088034887, 'fraction_numerical': 0.009266830198964295, 'mean_word_length': 3.6455696202531644, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7481828', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1091, 'n_words': 342}