-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.44 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
62
63
64
65
var url = require('url');
var redis = require('redis');
var express = require('express');
var router = express.Router();
var config = require('histograph-config');
var client = redis.createClient(config.redis.port, config.redis.host);
var queries = require('./queries')(config);
if (config.stats.enabled) {
// run every now and then...
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: config.stats.cronExpression,
onTick: function() {
queries.update();
}
});
job.start();
// and run on startup
queries.update();
}
router.get('/queue', function(req, res) {
client.llen(config.redis.queue, function(err, reply) {
if (err) {
res.status(400).send({
message: err
});
} else {
return res.send({
length: reply
});
}
});
});
var queryNames = queries.names.map(function(name) {
return url.resolve(config.api.baseUrl, '/stats/queries/' + name);
});
router.get('/queries', function(req, res) {
res.send(queryNames);
});
router.get('/queries/:query', function(req, res) {
if (queries.names.indexOf(req.params.query) > -1) {
queries.get(req.params.query, function(err, data) {
if (err) {
res.status(500).send({
message: 'Error getting query results'
});
} else {
res.send(data || []);
}
});
} else {
res.status(404).send({
message: 'Not found!'
});
}
});
module.exports = router;