-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_stats.js
95 lines (84 loc) · 1.92 KB
/
update_stats.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
var BufferedReader = require("buffered-reader");
var fs = require("fs");
var async = require("async");
var dataModels = require('./data_models').models;
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server('localhost', 27017, {auto_reconnect: true});
var db = new Db('serelex2', server);
db.open(function(err, db) {
if(err)
{
console.log(err);
return;
}
db.collection('words', function(err, wordsCollection){
if(err)
{
console.log(err);
db.close();
return;
}
db.collection('relations', function(err, relationsCollection){
if(err)
{
console.log(err);
db.close();
return;
}
console.log("Start calculating stats, please wait....");
relationsCollection.mapReduce(function(){
emit({
model: this.model
}, {
words: 1,
relations: this.relations.length
});
}, function(key, values){
var words = 0, relations = 0;
values.forEach(function(value) {
words += value['words'];
relations += value['relations'];
});
return {words: words, relations: relations};
}, {
out: { inline: 1 },
verbose: true
}, function(err, results, stats) {
if (err)
{
console.log(err);
db.close();
return;
}
var stats = {};
async.forEach(results, function(model, callback){
wordsCollection.findOne({id: model._id.model}, function(err, item){
if (err)
{
console.log(err);
callback(err);
return;
}
stats[item.word] = {
words: model.value.words,
relations: model.value.relations
}
callback();
});
}, function(err){
if (err != null)
console.log(err);
fs.writeFile("./stats.json", JSON.stringify(stats), "utf8", function(err){
if (err)
console.log(err);
else
console.log("Done! stats.json writing!");
db.close();
});
});
});
});
});
});