From db5b9255d7dd18bc0449bce635b3240ef27a1ab8 Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Mon, 15 Apr 2019 16:27:33 +0300 Subject: [PATCH] Endpoint for getting timeseries of analytic unit #528 (#564) --- .../src/controllers/analytics_controller.ts | 11 +--- server/src/index.ts | 2 + server/src/routes/data_router.ts | 56 +++++++++++++++++++ server/src/services/data_puller.ts | 11 +--- server/src/utils/grafana.ts | 5 ++ 5 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 server/src/routes/data_router.ts create mode 100644 server/src/utils/grafana.ts diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index d51f478..4298059 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -6,8 +6,9 @@ import * as Threshold from '../models/threshold_model'; import * as AnalyticUnit from '../models/analytic_unit_model'; import { AnalyticsService } from '../services/analytics_service'; import { AlertService } from '../services/alert_service'; -import { HASTIC_API_KEY, GRAFANA_URL } from '../config'; +import { HASTIC_API_KEY } from '../config'; import { DataPuller } from '../services/data_puller'; +import { getGrafanaUrl } from '../utils/grafana'; import { queryByMetric, GrafanaUnavailable, DatasourceUnavailable } from 'grafana-datasource-kit'; @@ -126,13 +127,7 @@ async function query(analyticUnit: AnalyticUnit.AnalyticUnit, detector: Analytic } console.log(`query time range: from ${new Date(range.from)} to ${new Date(range.to)}`); - let grafanaUrl; - if(GRAFANA_URL !== null) { - grafanaUrl = GRAFANA_URL; - } else { - grafanaUrl = analyticUnit.grafanaUrl; - } - + const grafanaUrl = getGrafanaUrl(analyticUnit.grafanaUrl); let data; try { diff --git a/server/src/index.ts b/server/src/index.ts index db217ac..5618d3c 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,6 +1,7 @@ import { router as analyticUnitsRouter } from './routes/analytic_units_router'; import { router as segmentsRouter } from './routes/segments_router'; import { router as thresholdRouter } from './routes/threshold_router'; +import { router as dataRouter } from './routes/data_router'; import * as AnalyticsController from './controllers/analytics_controller'; @@ -55,6 +56,7 @@ async function init() { rootRouter.use('/analyticUnits', analyticUnitsRouter.routes(), analyticUnitsRouter.allowedMethods()); rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods()); rootRouter.use('/threshold', thresholdRouter.routes(), thresholdRouter.allowedMethods()); + rootRouter.use('/query', dataRouter.routes(), dataRouter.allowedMethods()); rootRouter.get('/', async (ctx) => { const activeWebhooks = await AnalyticsController.getActiveWebhooks(); diff --git a/server/src/routes/data_router.ts b/server/src/routes/data_router.ts new file mode 100644 index 0000000..98e63df --- /dev/null +++ b/server/src/routes/data_router.ts @@ -0,0 +1,56 @@ +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); diff --git a/server/src/services/data_puller.ts b/server/src/services/data_puller.ts index 13ef936..8f6d0c3 100644 --- a/server/src/services/data_puller.ts +++ b/server/src/services/data_puller.ts @@ -2,9 +2,10 @@ import { AnalyticsTask, AnalyticsTaskType } from '../models/analytics_task_model import * as AnalyticUnit from '../models/analytic_unit_model'; import * as AnalyticUnitCache from '../models/analytic_unit_cache_model'; import { AnalyticsService } from './analytics_service'; -import { HASTIC_API_KEY, GRAFANA_URL } from '../config'; +import { HASTIC_API_KEY } from '../config'; import { availableReporter } from '../utils/reporter'; import { AlertService } from './alert_service'; +import { getGrafanaUrl } from '../utils/grafana'; import { queryByMetric, GrafanaUnavailable, DatasourceUnavailable } from 'grafana-datasource-kit'; @@ -65,13 +66,7 @@ export class DataPuller { throw Error(`data puller: can't pull undefined unit`); } - let grafanaUrl; - if(GRAFANA_URL !== null) { - grafanaUrl = GRAFANA_URL; - } else { - grafanaUrl = unit.grafanaUrl; - } - + const grafanaUrl = getGrafanaUrl(unit.grafanaUrl); let data = queryByMetric(unit.metric, grafanaUrl, from, to, HASTIC_API_KEY); return data; diff --git a/server/src/utils/grafana.ts b/server/src/utils/grafana.ts new file mode 100644 index 0000000..a0d9a09 --- /dev/null +++ b/server/src/utils/grafana.ts @@ -0,0 +1,5 @@ +import { GRAFANA_URL } from '../config'; + +export function getGrafanaUrl(browserGrafanaUrl: string): string { + return (GRAFANA_URL !== null) ? GRAFANA_URL : browserGrafanaUrl; +}