-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (39 loc) · 1.1 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
var express = require('express');
var timeago = require('timeago');
/**
* Expose metrics serving app.
*
* @param {metrics} metrics
* @return {ExpressApp}
*/
module.exports = function (metrics) {
var app = express();
app.get('/', function (req, res, next) {
res.status(200).jsonp(metrics.keys());
});
app.get('/:name', get, function (req, res, next) {
var human = {};
var values = req.metric.values();
Object.keys(values).forEach(function (t) {
human[timeago(new Date(parseInt(t)))] = values[t];
});
res.status(200).jsonp(human);
});
app.get('/:name/:timestamp', get, function (req, res, next) {
var timestamp = req.param('timestamp');
res.status(200).jsonp(req.metric.from(timestamp));
});
// middleware to parse the metric from the :name parameter.
function get (req, res, next) {
var name = req.param('name');
var metric = metrics.get(name);
if (!metric) {
res.status(404)
.jsonp({ error: { message: 'Failed to find metric "' + name + '"'} });
} else {
req.metric = metric;
next();
}
}
return app;
};