-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.95 KB
/
index.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
var prefixes = [];
var hoard = require('./hoard');
var mkdirp = require('mkdirp');
var path = require('path');
var fldr_path;
unixTime = function() {
return parseInt(new Date().getTime() / 1000);
};
exports.hoard = hoard;
exports.save_metric_value = function(metric_name, metric_value, timestamp, callback) {
var t = timestamp ? parseInt(timestamp) : unixTime();
if (prefixes.indexOf(metric_name) >= 0) {
hoard.update(path.join(fldr_path, metric_name + ".hoard"), metric_value, t, function(err) {
if (err) throw err;
if (callback) callback();
});
} else {
callback("Invalid metric name");
}
}
exports.init = function(url_path, p, express, app, fp) {
prefixes = p;
fldr_path = fp;
app.use(url_path, express.bodyParser());
mkdirp(fldr_path, function (err) {
if (err) console.error(err)
else {
prefixes.forEach(function(prefix) {
app.get(url_path + "/" + prefix, function(req, res) {
var now = unixTime();
startTime = req.query.start ? parseInt(req.query.start) : now - 20;
endTime = req.query.end ? parseInt(req.query.end) : now;
results = hoard.fetch(path.join(fldr_path, prefix + ".hoard"), startTime, endTime, function(err, timeInfo, values) {
res.send(values);
});
});
});
app.post(url_path + "/publish", function(req, res) {
var metric_name = req.body.metric;
var value = req.body.value;
exports.save_metric_value(metric_name, value, req.body.measure_time, function(e) {
if (e) {
res.send(404, {"error": e});
} else {
res.send({"success": true});
}
});
});
}
});
}