-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathrun.js
56 lines (48 loc) · 1.53 KB
/
run.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
import Promise from 'bluebird';
import _ from 'lodash';
import chainRunnerFn from '../handlers/chain_runner.js';
const timelionDefaults = require('../lib/get_namespaced_settings')();
function replyWithError(e, reply) {
reply({
title: e.toString(),
message: e.toString()
}).code(500);
}
export default function (server) {
server.route({
method: ['POST', 'GET'],
path: '/api/timelion/run',
handler: async (request, reply) => {
try {
const uiSettings = await request.getUiSettingsService().getAll();
const tlConfig = require('../handlers/lib/tl_config.js')({
server,
request,
settings: _.defaults(uiSettings, timelionDefaults) // Just in case they delete some setting.
});
const chainRunner = chainRunnerFn(tlConfig);
const sheet = await Promise.all(chainRunner.processRequest(request.payload || {
sheet: [request.query.expression],
time: {
from: request.query.from,
to: request.query.to,
interval: request.query.interval,
timezone: request.query.timezone
}
}));
reply({
sheet,
stats: chainRunner.getStats()
});
} catch (err) {
server.log(['timelion', 'error'], `${err.toString()}: ${err.stack}`);
// TODO Maybe we should just replace everywhere we throw with Boom? Probably.
if (err.isBoom) {
reply(err);
} else {
replyWithError(err, reply);
}
}
}
});
}