-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
111 lines (99 loc) · 2.34 KB
/
generate.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var fs = require('fs'),
csv = require('csv'),
nano = require('nano')(process.env['CLOUDANT']),
_ = require('underscore')
db = nano.use('d3demo'),
clusterSize = {};
function addSizes (doc, info) {
var cg = doc.ClusterGroup;
if (!info[cg]) {
info[cg] = {
clusterGroup: cg,
name: cg,
size: 0,
male: 0,
female: 0,
income: {
unknown: 0,
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
i: 0
},
language: {
"English": 0,
"Afrikaans": 0
},
culturalProfile: {
"1": 0,
"2": 0,
"3": 0,
"4": 0
},
favouredProduct: {
"Null": 0,
"Product1":0,
"Product2":0,
"Product3":0,
"Product4":0,
"Product5":0,
"Product6":0,
"Product7":0,
"Product8":0,
"Product9":0,
"Product10":0
}
};
info[cg].joinYear = _.reduce(_.range(1985, 2014), function (group, year) {
group[year.toString()] = 0;
return group;
}, {});
}
var group = info[cg];
group.size += 1;
var income = doc.income === "" ? "unknown" : doc.income.toLowerCase() ;
group.income[income] += 1;
group.joinYear[doc.joinYear] += 1;
group.language[doc.language] += 1;
group.culturalProfile[doc.culturalProfile] += 1;
group.favouredProduct[doc.favouredProduct] += 1;
if (doc.gender === "Female") {
group.female = group.female + 1;
} else {
group.male = group.male + 1;
}
}
function saveSummary () {
console.log(clusterSize);
db.insert(clusterSize, 'summary', function (err, res) {
if (err) { console.log('err summary', err);}
});
}
csv().from.path(__dirname + '/ixio_jsinsa_data.csv', { delimiter: ';', escape: '"' }).on('record', function (row, i) {
if (i === 0) { return; } //skip headers
var item = {
"number": row[0],
"age": row[1],
"language": row[2],
"culturalProfile" : row[3],
"gender": row[4],
"province": row[5],
"income": row[6],
"favouredProduct": row[7],
"joinYear": row[8],
"overdue": row[9],
"currentMOP": row[10],
"ClusterGroup": row[11]
};
addSizes(item, clusterSize);
db.insert(item, function (err, resp) {
console.log('db', err, resp);
});
}).on('end', function () {
saveSummary();
});