You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.4 KiB
57 lines
1.4 KiB
5 years ago
|
import * as AnalyticUnit from '../models/analytic_unit_model';
|
||
|
import { HASTIC_API_KEY } from '../config';
|
||
|
import { getGrafanaUrl } from '../utils/grafana';
|
||
|
|
||
|
import { queryByMetric } from 'grafana-datasource-kit';
|
||
|
|
||
|
import * as Router from 'koa-router';
|
||
|
|
||
|
|
||
|
async function query(ctx: Router.IRouterContext) {
|
||
|
|
||
|
let from = ctx.request.query.from;
|
||
|
let to = ctx.request.query.to;
|
||
|
const analyticUnitId = ctx.request.query.analyticUnitId;
|
||
|
|
||
|
if(analyticUnitId === undefined) {
|
||
|
throw new Error(`data router error: request must contain analyticUnitId`);
|
||
|
}
|
||
|
|
||
|
if(from === undefined) {
|
||
|
throw new Error(`data router error: request must contain 'from'`)
|
||
|
}
|
||
|
|
||
|
if(to === undefined) {
|
||
|
throw new Error(`data router error: request must contain 'to'`)
|
||
|
}
|
||
|
|
||
|
from = +from;
|
||
|
to = +to;
|
||
|
|
||
|
if(from === NaN) {
|
||
|
throw new Error(`from must be not NaN`);
|
||
|
}
|
||
|
|
||
|
if(to === NaN) {
|
||
|
throw new Error(`to must be not NaN`);
|
||
|
}
|
||
|
|
||
|
if(to <= from) {
|
||
|
throw new Error(`data router error: 'to' must be greater than 'from' (from:${from} to:${to})`);
|
||
|
}
|
||
|
|
||
|
const analyticUnit = await AnalyticUnit.findById(analyticUnitId);
|
||
|
|
||
|
if(analyticUnit === undefined) {
|
||
|
throw new Error(`can't find analytic unit ${analyticUnitId}`);
|
||
|
}
|
||
|
|
||
|
const grafanaUrl = getGrafanaUrl(analyticUnit.grafanaUrl);
|
||
|
const results = await queryByMetric(analyticUnit.metric, grafanaUrl, from, to, HASTIC_API_KEY);
|
||
|
ctx.response.body = { results };
|
||
|
}
|
||
|
|
||
|
export const router = new Router();
|
||
|
|
||
|
router.get('/', query);
|