Added the recalculation script for a couple of statistics

This commit is contained in:
Ruben Vereecken 2014-07-23 15:22:53 +02:00
parent ebfe1fc1c0
commit 2394bd8129
4 changed files with 52 additions and 11 deletions

View file

@ -0,0 +1,35 @@
database = require '../server/commons/database'
mongoose = require 'mongoose'
log = require 'winston'
async = require 'async'
### SET UP ###
do (setupLodash = this) ->
GLOBAL._ = require 'lodash'
_.str = require 'underscore.string'
_.mixin _.str.exports()
database.connect()
### USER STATS ###
UserHandler = require '../server/users/user_handler'
report = (func, name, done) ->
log.info 'Started ' + name + '...'
func name, (err) ->
log.warn err if err?
log.info 'Finished ' + name
done err if done?
whenAllFinished = ->
log.info 'All recalculations finished.'
process.exit()
async.series [
(c) -> report UserHandler.recalculateAsync, 'gamesCompleted', c
(c) -> report UserHandler.recalculateAsync, 'articleEdits', c
(c) -> report UserHandler.recalculateAsync, 'levelEdits', c
(c) -> report UserHandler.recalculateAsync, 'levelComponentEdits', c
(c) -> report UserHandler.recalculateAsync, 'levelSystemEdits', c
(c) -> report UserHandler.recalculateAsync, 'thangTypeEdits', c
], whenAllFinished

View file

@ -407,7 +407,7 @@ UserHandler = class UserHandler extends Handler
doneWithUser() doneWithUser()
), done ), done
statHandlers: statRecalculators:
gamesCompleted: (done) -> gamesCompleted: (done) ->
LevelSession = require '../levels/sessions/LevelSession' LevelSession = require '../levels/sessions/LevelSession'
@ -442,14 +442,15 @@ UserHandler = class UserHandler extends Handler
ThangType = require '../levels/thangs/ThangType' ThangType = require '../levels/thangs/ThangType'
countEdits ThangType, done countEdits ThangType, done
recalculateAsync: (statName, done) =>
return new Error 'Recalculation handler not found' unless statName of @statRecalculators
@statRecalculators[statName] done
recalculate: (req, res, statName) -> recalculate: (req, res, statName) ->
return @sendForbiddenError(res) unless req.user.isAdmin() return @sendForbiddenError(res) unless req.user.isAdmin()
log.debug 'recalculate'
if statName of @statHandlers return @sendNotFoundError(res) unless statName of @statRecalculators
@statHandlers[statName]() @recalculateAsync statName
return @sendAccepted res, {} @sendAccepted res, {}
else return @sendNotFoundError(res)
module.exports = new UserHandler() module.exports = new UserHandler()

View file

@ -15,7 +15,7 @@ describe 'recalculate statistics', ->
expect(res.statusCode).toBe 202 expect(res.statusCode).toBe 202
done() done()
xit 'responds with a 404 if handler not found', (done) -> it 'responds with a 404 if handler not found', (done) ->
loginAdmin -> loginAdmin ->
request.post {uri:getURL '/admin/blobfish/swim'}, (err, res, body) -> request.post {uri:getURL '/admin/blobfish/swim'}, (err, res, body) ->
expect(res.statusCode).toBe 404 expect(res.statusCode).toBe 404
@ -32,3 +32,8 @@ describe 'recalculate statistics', ->
request.post {uri:url + 'ballsKicked'}, (err, res, body) -> request.post {uri:url + 'ballsKicked'}, (err, res, body) ->
expect(res.statusCode).toBe 404 expect(res.statusCode).toBe 404
done() done()

View file

@ -364,7 +364,7 @@ describe 'Statistics', ->
expect(err).toBeNull() expect(err).toBeNull()
expect(guy.get 'stats.gamesCompleted').toBeUndefined() expect(guy.get 'stats.gamesCompleted').toBeUndefined()
UserHandler.statHandlers.gamesCompleted -> UserHandler.statRecalculators.gamesCompleted ->
User.findById joe.get('id'), (err, guy) -> User.findById joe.get('id'), (err, guy) ->
expect(err).toBeNull() expect(err).toBeNull()
expect(guy.get 'stats.gamesCompleted').toBe 1 expect(guy.get 'stats.gamesCompleted').toBe 1
@ -406,7 +406,7 @@ describe 'Statistics', ->
expect(err).toBeNull() expect(err).toBeNull()
expect(guy.get User.statsMapping.edits.article).toBeUndefined() expect(guy.get User.statsMapping.edits.article).toBeUndefined()
UserHandler.statHandlers.articleEdits -> UserHandler.statRecalculators.articleEdits ->
User.findById carl.get('id'), (err, guy) -> User.findById carl.get('id'), (err, guy) ->
expect(err).toBeNull() expect(err).toBeNull()
expect(guy.get User.statsMapping.edits.article).toBe 2 expect(guy.get User.statsMapping.edits.article).toBe 2
@ -439,7 +439,7 @@ describe 'Statistics', ->
expect(err).toBeNull() expect(err).toBeNull()
expect(guy.get User.statsMapping.edits.level).toBeUndefined() expect(guy.get User.statsMapping.edits.level).toBeUndefined()
UserHandler.statHandlers.levelEdits -> UserHandler.statRecalculators.levelEdits ->
User.findById jose.get('id'), (err, guy) -> User.findById jose.get('id'), (err, guy) ->
expect(err).toBeNull() expect(err).toBeNull()
expect(guy.get User.statsMapping.edits.level).toBe 1 expect(guy.get User.statsMapping.edits.level).toBe 1