-
Notifications
You must be signed in to change notification settings - Fork 3
/
export.js
53 lines (41 loc) · 1.27 KB
/
export.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
var csv = require('fast-csv'),
db = require('./rethinkdb'),
_ = require('lodash');
db.export(function(err, connection, database){
//your export code here
//example: see how from which countries the peers came from
database
.table("crawls")
.limit(1)
.run(connection, function(err, res){
if(err) throw err;
res.toArray(function(err, crawls){
// The following method accept an array of values to be written,
// however each value must be an array of arrays or objects.
var crawlObj = crawls[0];
var peersPerCountry = {};
_.each(crawlObj.peers, function(peer){
if(!peersPerCountry[peer.country]){
peersPerCountry[peer.country] = 1;
}else{
peersPerCountry[peer.country]++;
}
});
//tweak a bit so we have arrays in the data array, just like a table
var data = [];
var countries = _.keys(peersPerCountry);
var c = 0;
_.each(peersPerCountry, function(country){
data.push([countries[++c], country]);
});
console.log(peersPerCountry);
//Export to CSV
csv
.writeToPath("export.csv", data, {headers: true})
.on("finish", function(){
console.log("done!");
process.exit(0);
});
});
});
});