diff --git a/scripts/recalculateStatistics.coffee b/scripts/recalculateStatistics.coffee new file mode 100644 index 000000000..144d3bea0 --- /dev/null +++ b/scripts/recalculateStatistics.coffee @@ -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 diff --git a/server/users/user_handler.coffee b/server/users/user_handler.coffee index f8acab746..e2e2d8b93 100644 --- a/server/users/user_handler.coffee +++ b/server/users/user_handler.coffee @@ -407,7 +407,7 @@ UserHandler = class UserHandler extends Handler doneWithUser() ), done - statHandlers: + statRecalculators: gamesCompleted: (done) -> LevelSession = require '../levels/sessions/LevelSession' @@ -442,14 +442,15 @@ UserHandler = class UserHandler extends Handler ThangType = require '../levels/thangs/ThangType' countEdits ThangType, done + recalculateAsync: (statName, done) => + return new Error 'Recalculation handler not found' unless statName of @statRecalculators + @statRecalculators[statName] done recalculate: (req, res, statName) -> return @sendForbiddenError(res) unless req.user.isAdmin() - - if statName of @statHandlers - @statHandlers[statName]() - return @sendAccepted res, {} - else return @sendNotFoundError(res) - + log.debug 'recalculate' + return @sendNotFoundError(res) unless statName of @statRecalculators + @recalculateAsync statName + @sendAccepted res, {} module.exports = new UserHandler() diff --git a/test/server/functional/admin.spec.coffee b/test/server/functional/admin.spec.coffee index 87a390de5..96be3ef77 100644 --- a/test/server/functional/admin.spec.coffee +++ b/test/server/functional/admin.spec.coffee @@ -15,7 +15,7 @@ describe 'recalculate statistics', -> expect(res.statusCode).toBe 202 done() - xit 'responds with a 404 if handler not found', (done) -> + it 'responds with a 404 if handler not found', (done) -> loginAdmin -> request.post {uri:getURL '/admin/blobfish/swim'}, (err, res, body) -> expect(res.statusCode).toBe 404 @@ -32,3 +32,8 @@ describe 'recalculate statistics', -> request.post {uri:url + 'ballsKicked'}, (err, res, body) -> expect(res.statusCode).toBe 404 done() + + + + + diff --git a/test/server/functional/user.spec.coffee b/test/server/functional/user.spec.coffee index ec6cc84a0..f711e5b50 100644 --- a/test/server/functional/user.spec.coffee +++ b/test/server/functional/user.spec.coffee @@ -364,7 +364,7 @@ describe 'Statistics', -> expect(err).toBeNull() expect(guy.get 'stats.gamesCompleted').toBeUndefined() - UserHandler.statHandlers.gamesCompleted -> + UserHandler.statRecalculators.gamesCompleted -> User.findById joe.get('id'), (err, guy) -> expect(err).toBeNull() expect(guy.get 'stats.gamesCompleted').toBe 1 @@ -406,7 +406,7 @@ describe 'Statistics', -> expect(err).toBeNull() expect(guy.get User.statsMapping.edits.article).toBeUndefined() - UserHandler.statHandlers.articleEdits -> + UserHandler.statRecalculators.articleEdits -> User.findById carl.get('id'), (err, guy) -> expect(err).toBeNull() expect(guy.get User.statsMapping.edits.article).toBe 2 @@ -439,7 +439,7 @@ describe 'Statistics', -> expect(err).toBeNull() expect(guy.get User.statsMapping.edits.level).toBeUndefined() - UserHandler.statHandlers.levelEdits -> + UserHandler.statRecalculators.levelEdits -> User.findById jose.get('id'), (err, guy) -> expect(err).toBeNull() expect(guy.get User.statsMapping.edits.level).toBe 1