Browse Source

Endpoint for getting timeseries of analytic unit #528 (#564)

pull/1/head
Evgeny Smyshlyaev 6 years ago committed by rozetko
parent
commit
db5b9255d7
  1. 11
      server/src/controllers/analytics_controller.ts
  2. 2
      server/src/index.ts
  3. 56
      server/src/routes/data_router.ts
  4. 11
      server/src/services/data_puller.ts
  5. 5
      server/src/utils/grafana.ts

11
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 {

2
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();

56
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);

11
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;

5
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;
}
Loading…
Cancel
Save