Skip to content

Commit b483738

Browse files
committed
wip scores
1 parent 8401db7 commit b483738

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

lib/node-mongodb-native/lib/mongodb/collection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ Collection.prototype.group = function(keys, condition, initial, reduce, command,
434434
var reduceFunction = reduce != null && reduce instanceof this.db.bson_serializer.Code ? reduce : new this.db.bson_serializer.Code(reduce);
435435
var selector = {'group': {
436436
'ns':this.collectionName,
437-
'$reduce': reduce,
437+
'$reduce': reduceFunction,
438438
'key':hash,
439439
'cond':condition,
440440
'initial': initial}};

models/models.coffee

+45-1
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,53 @@ class Team
9999
generateDeploySlugs: ->
100100
@joyentSlug or= @slug.replace(/^(\d)/, 'ko-$1').replace(/_/g, '-').substring(0, 30)
101101
@herokuSlug or= 'nko-' + @slug.replace(/_/g, '-').substring(0, 26)
102-
103102
nko.Team = Team
104103

104+
class ScoreCalculator
105+
calculate: (fn) ->
106+
@select => @merge fn
107+
108+
select: (fn) ->
109+
threads = 4
110+
@where { confirmed: false }, (error, unconfirmed) =>
111+
@unconfirmed = unconfirmed
112+
fn() if --threads is 0
113+
@where { confirmed: true }, (error, confirmed) =>
114+
@confirmed = confirmed
115+
fn() if --threads is 0
116+
117+
Person.all { type: 'Judge' }, (error, judges) =>
118+
judge_ids = _.pluck(judges, '_id');
119+
@where { 'person._id': { $in: judge_ids }}, (error, judged) =>
120+
@judged = judged
121+
fn() if --threads is 0
122+
123+
merge: (fn) ->
124+
scores = {}
125+
for type in ['unconfirmed', 'confirmed', 'judged']
126+
for score in this[type]
127+
scores[score['team._id']] ||= {}
128+
scores[score['team._id']][type] = score
129+
fn(scores)
130+
131+
where: (cond, fn) ->
132+
Vote.group {
133+
cond: cond
134+
keys: ['team._id']
135+
initial: { popularity: 0, utility: 0, design: 0, innovation: 0, completeness: 0 }
136+
reduce: (row, memo) ->
137+
memo.popularity += 1
138+
for dimension in ['utility', 'design', 'innovation', 'completeness']
139+
memo[dimension] += parseInt row[dimension]
140+
}, fn
141+
142+
_.extend ScoreCalculator, {
143+
calculate: (fn) ->
144+
(new ScoreCalculator).calculate fn
145+
}
146+
147+
nko.ScoreCalculator = ScoreCalculator
148+
105149
class Person
106150
constructor: (options) ->
107151
@name = options?.name or ''

models/mongo.coffee

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ _.extend Mongo,
113113
return fn error if error?
114114
collection.update Mongo.queryify(query), cmd, {upsert: false, multi: true}, fn
115115

116+
group: (opts, fn) ->
117+
@prototype.collection (error, collection) ->
118+
return fn error if error?
119+
collection.group(opts.keys, opts.cond, (opts.initial || {}), opts.reduce, true, fn)
120+
116121
InstanceMethods:
117122
collection: (fn) ->
118123
Mongo.db.collection @serializer.name, fn

nodeko.coffee

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ connect = require 'connect'
77
express = require 'express'
88

99
models = require './models/models'
10-
[Team, Person, Vote] = [models.Team, models.Person, models.Vote]
10+
[Team, Person, Vote, ScoreCalculator] = [models.Team, models.Person, models.Vote, models.ScoreCalculator]
1111

1212
pub = __dirname + '/public'
1313
app = express.createServer(
@@ -130,6 +130,15 @@ get '/register', ->
130130
get '/error', ->
131131
throw new Error('Foo')
132132

133+
get '/scores', ->
134+
ScoreCalculator.calculate (scores) =>
135+
Team.all (error, teams) =>
136+
for team in teams
137+
team.score = scores[team.id()]
138+
sys.log sys.inspect teams
139+
@teams = teams
140+
@render 'teams/scores.html.haml'
141+
133142
# list teams
134143
get '/teams', ->
135144
q = if @req.param('invalid') then { url: /\w/, validDeploy: false } else { validDeploy: true }

0 commit comments

Comments
 (0)