-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateUsersPerLocation_US.js
49 lines (48 loc) · 1.52 KB
/
calculateUsersPerLocation_US.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var async = require('async'),
db = require('./db')
module.exports.run = function(callback) {
async.waterfall([
function (callback) {
db.relativePopulation.where('type').equals('state').remove(function(err) {
callback(err)
})
}, function(callback) {
db.interest_locations.find({type: 'state', location_parent: 'US'},function(err, interest_locations) {
callback(err, interest_locations)
})
}, function (interest_locations, callback) {
var total=0, states = []
async.forEachSeries(interest_locations, function(document, callback) {
added = 0;
if (document) {
total+=document.count
for (var i=0;i<states.length;i++) {
if (states[i].name == document.location) {
added = 1;
states[i].count+=document.count
callback()
break;
}
}
if (added == 0) states.push({count: document.count, name: document.location})
}
if (added == 0) callback()
}, function(err) {
callback(err, states,total)
})
}, function (states, total, callback) {
async.forEachSeries(states, function(state, callback) {
var x = new db.relativePopulation({type: 'state', location: state.name, percentage: ((state.count)/total).toFixed(3)})
x.save(function(err) {
callback(err)
})
}, function(err) {
console.log(states.length + " state percentages have been added to the database")
callback(err)
})
}
], function(err) {
if (err) console.log(err)
callback()
})
}