-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateUsersPerLocation.js
48 lines (48 loc) · 1.63 KB
/
calculateUsersPerLocation.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
var async = require('async'),
db = require('./db')
async.waterfall([
function (callback) {
require('./calculateUsersPerLocation_US').run(callback) //separate file calculates states percentages
}, function (callback) {
db.relativePopulation.where('type').equals('country').remove(function(err) {
callback(err)
})
}, function(callback) {
db.interest_locations.find({type: 'country'},function(err, interest_locations) {
callback(err, interest_locations)
})
}, function (interest_locations, callback) {
var total=0, countries = []
async.forEachSeries(interest_locations, function(document, callback) {
added = 0;
if (document) {
total+=document.count
for (var i=0;i<countries.length;i++) {
if (countries[i].name == document.location_short) {
added = 1;
countries[i].count+=document.count
callback()
break;
}
}
if (added == 0) countries.push({count: document.count, name: document.location_short})
}
if (added == 0) callback()
}, function(err) {
callback(err, countries,total)
})
}, function (countries, total, callback) {
async.forEachSeries(countries, function(country, callback) {
var x = new db.relativePopulation({type: 'country', location: country.name, percentage: ((country.count)/total).toFixed(3)})
x.save(function(err) {
callback(err)
})
}, function(err) {
console.log(countries.length + " country percentages have been added to the database")
callback(err)
})
}
], function(err) {
if (err) console.log(err)
process.exit()
})