Skip to content

Commit

Permalink
refactored some math into a library, and also implemented volunteer n…
Browse files Browse the repository at this point in the history
…eeds prediction (very naievely)
  • Loading branch information
cdcarter committed Jul 27, 2011
1 parent 691f138 commit 8dd9807
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions vendor/showmath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
exports.init = function(ddoc) {
var _ = require("vendor/underscore")

function avg(doc) {
doc.avg = 0;
doc.perfd = 0;
for(var i=0,_len=doc.shows.length; i < _len; ++i) {
if (typeof doc.shows[i].count !== "undefined" && doc.shows[i].count !== null){
doc.avg = doc.avg + doc.shows[i].count;
doc.perfd = doc.perfd + 1
}
doc.shows[i].idx = i

doc.shows[i].time = ddoc.data.slots[doc.shows[i].slot]
}

if (doc.perfd == 0){
doc.avg = "None"
doc.latest = "None"
doc.perfd = "None"
} else {
doc.avg = doc.avg/doc.perfd
doc.latest = doc.shows[doc.perfd-1].count
}

return doc;
}

function guess(doc) {
if(typeof doc.perfd === "undefined") {
doc = avg(doc)
}
var venue = _.detect(ddoc.data.venues["by-name"],function(v){return v.name==doc.venue})
var counts = _.detect(ddoc.data.venues.counts,function(v){return v.short==venue.short})

var needs = {"side":counts["side-min"],"box":counts["box-min"],"usher":counts["ush-min"]}

if(doc.perfd === "None" ) {
return needs;
}
if(doc.avg >= (venue.capacity*0.4)) {
needs = addVol(needs,counts);
}
if(doc.avg >= (venue.capacity*0.5)) {
needs = addVol(needs,counts);
}
if(doc.avg >= (venue.capacity*0.6)) {
needs = addVol(needs,counts);
}
if(doc.avg >= (venue.capacity*0.7)) {
needs = addVol(needs,counts);
}
return needs;
}

function addVol(current,counts) {
if ((current["side"]===counts["side-max"])&&(current["box"]===counts["box-max"])&&(current["usher"]===counts["ush-max"])) {
return current;
}
if (current["box"] !== counts["box-max"]) {
current.box++;
return current
}
if (current["side"] !== counts["side-max"]) {
current.side++;
return current
}
if (current["usher"] !== counts["ush-max"]) {
current.ush++;
return current
}
}

return {
"avg": avg,
"guess":guess
}
}

0 comments on commit 8dd9807

Please sign in to comment.