-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathmongo.js
100 lines (80 loc) · 2.32 KB
/
mongo.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
var _ = require('lodash');
var MongoClient = require('mongodb').MongoClient;
var etlDb;
function query(callback, bucket) {
var collection = etlDb.collection('aggregations');
var dimBucket = bucket ? bucket : 'MINUTES';
collection.find({
dimension_size: 3,
'dimensions.bucket': dimBucket,
'dimensions.timestamp': {$exists: true},
'dimensions.logType': 'apache'
})
.sort({'dimensions.timestamp': -1 })
//.limit(2000)
.toArray(callback);
}
MongoClient.connect('mongodb://localhost:27017/etl_sample2', function (err, db) {
etlDb = db;
if (err) {
return console.dir(err);
}
var collection = db.collection('aggregations');
collection.stats(function (err, stats) {
console.log(stats);
});
query(function (err, items) {
console.log(items.length);
});
});
function data(req, res) {
var metric = req.query.metric;
query(function (err, items) {
var response = _.map(items, function (item) {
return {
timestamp: item.dimensions.timestamp.getTime(),
value: item.metrics[metric]
};
});
//TODO filter in query with $exists instead
response = _.reject(response, function (item) {
return _.isUndefined(item.value);
});
res.json(response);
}, req.query.bucket);
}
function topn(req, res) {
var collection = etlDb.collection('aggregations');
var limit = req.query.limit ? parseInt(req.query.limit, 10) : 100;
var dimension = req.query.dimension;
var dimensionQuery = {};
dimensionQuery['dimension_size'] = 2;
dimensionQuery['dimensions.' + dimension] = {$exists: true};
dimensionQuery['dimensions.logType'] = 'apache';
collection.find(dimensionQuery)
.sort({'metrics.count': -1 })
.limit(limit)
.toArray(function (err, items) {
var response = _.map(items, function (item) {
var dimensionName = item.dimensions[dimension];
dimensionName = dimensionName ? dimensionName : 'Other';
return {
name: dimensionName,
value: item.metrics.count
};
});
res.json(response);
//res.json(items);
});
}
function all(req, res) {
var collection = etlDb.collection('aggregations');
collection.find({})
//.limit(2000)
.toArray(function (err, data) {
res.json(data);
});
}
exports.data = data;
exports.topn = topn;
exports.all = all;