2016-04-06 13:56:06 -04:00
|
|
|
UserCodeProblem = require './../models/UserCodeProblem'
|
2014-08-14 14:55:43 -04:00
|
|
|
Handler = require '../commons/Handler'
|
2015-01-14 14:09:01 -05:00
|
|
|
utils = require '../lib/utils'
|
2014-08-14 14:55:43 -04:00
|
|
|
|
|
|
|
class UserCodeProblemHandler extends Handler
|
|
|
|
modelClass: UserCodeProblem
|
|
|
|
jsonSchema: require '../../app/schemas/models/user_code_problem'
|
|
|
|
editableProperties: [
|
|
|
|
'code'
|
|
|
|
'codeSnippet'
|
|
|
|
'errHint'
|
|
|
|
'errId'
|
|
|
|
'errLevel'
|
|
|
|
'errMessage'
|
2014-10-24 17:05:51 -04:00
|
|
|
'errMessageNoLineInfo'
|
2014-08-14 14:55:43 -04:00
|
|
|
'errRange'
|
|
|
|
'errType'
|
|
|
|
'language'
|
|
|
|
'levelID'
|
|
|
|
]
|
|
|
|
|
|
|
|
makeNewInstance: (req) ->
|
|
|
|
ucp = super(req)
|
|
|
|
ucp.set('creator', req.user._id)
|
|
|
|
ucp
|
|
|
|
|
2015-01-21 16:41:34 -05:00
|
|
|
hasAccess: (req) ->
|
2015-01-22 14:30:22 -05:00
|
|
|
return true if req.user?.isAdmin()
|
|
|
|
return true if req.method.toLowerCase() is 'post'
|
|
|
|
false
|
2015-01-21 16:41:34 -05:00
|
|
|
|
2015-01-05 16:42:14 -05:00
|
|
|
getByRelationship: (req, res, args...) ->
|
2015-01-21 16:41:34 -05:00
|
|
|
return @sendForbiddenError res unless @hasAccess req
|
2015-01-05 16:42:14 -05:00
|
|
|
return @getCommonLevelProblemsBySlug(req, res) if args[1] is 'common_problems'
|
|
|
|
super(arguments...)
|
|
|
|
|
|
|
|
getCommonLevelProblemsBySlug: (req, res) ->
|
|
|
|
# Returns an ordered array of common user code problems with: language, message, hint, count
|
|
|
|
# Parameters:
|
|
|
|
# slug - level slug
|
|
|
|
# startDay - Inclusive, optional, e.g. '2014-12-14'
|
|
|
|
# endDay - Exclusive, optional, e.g. '2014-12-16'
|
|
|
|
|
|
|
|
levelSlug = req.query.slug or req.body.slug
|
|
|
|
startDay = req.query.startDay or req.body.startDay
|
|
|
|
endDay = req.query.endDay or req.body.endDay
|
|
|
|
|
|
|
|
return @sendSuccess res, [] unless levelSlug?
|
|
|
|
|
|
|
|
# Build query
|
2015-11-14 19:34:35 -05:00
|
|
|
match = if startDay? or endDay? then {$match: {$and: [levelID: levelSlug]}} else {$match: {levelID: levelSlug}}
|
2015-01-14 14:09:01 -05:00
|
|
|
match["$match"]["$and"].push _id: {$gte: utils.objectIdFromTimestamp(startDay + "T00:00:00.000Z")} if startDay?
|
|
|
|
match["$match"]["$and"].push _id: {$lt: utils.objectIdFromTimestamp(endDay + "T00:00:00.000Z")} if endDay?
|
2015-11-14 20:07:55 -05:00
|
|
|
limit = {$limit: 100000}
|
2015-01-05 16:42:14 -05:00
|
|
|
group = {"$group": {"_id": {"errMessage": "$errMessageNoLineInfo", "errHint": "$errHint", "language": "$language", "levelID": "$levelID"}, "count": {"$sum": 1}}}
|
|
|
|
sort = { $sort : { "_id.levelID": 1, count : -1, "_id.language": 1 } }
|
2015-11-14 20:07:55 -05:00
|
|
|
query = UserCodeProblem.aggregate match, limit, group, sort
|
2015-12-05 11:18:36 -05:00
|
|
|
query.cache(30 * 60 * 1000)
|
2015-01-05 16:42:14 -05:00
|
|
|
|
|
|
|
query.exec (err, data) =>
|
|
|
|
if err? then return @sendDatabaseError res, err
|
2015-11-14 19:34:35 -05:00
|
|
|
formatted = ({language: item._id.language, message: item._id.errMessage, hint: item._id.errHint, count: item.count} for item in data)
|
|
|
|
@sendSuccess res, formatted
|
2015-01-05 16:42:14 -05:00
|
|
|
|
2014-08-14 14:55:43 -04:00
|
|
|
module.exports = new UserCodeProblemHandler()
|