From ccda13d7e8178d63c7b58f11c91287d8b45441a3 Mon Sep 17 00:00:00 2001 From: sanke1 <22073083+sankerust@users.noreply.github.com> Date: Tue, 15 Jan 2019 21:10:24 +0300 Subject: [PATCH] Grafana url to config #114 (#337) --- config.example.json | 3 ++- server/src/config.ts | 1 + server/src/controllers/analytics_controller.ts | 12 ++++++++++-- server/src/services/data_puller.ts | 12 ++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/config.example.json b/config.example.json index 7866455..56ebf01 100644 --- a/config.example.json +++ b/config.example.json @@ -3,5 +3,6 @@ "HASTIC_API_KEY": "eyJrIjoiVjZqMHY0dHk4UEE3eEN4MzgzRnd2aURlMWlIdXdHNW4iLCJuIjoiaGFzdGljIiwiaWQiOjF9", "HASTIC_WEBHOOK_URL": "http://localhost:8080", "HASTIC_WEBHOOK_TYPE": "application/x-www-form-urlencoded", - "HASTIC_WEBHOOK_SECRET": "mysecret" + "HASTIC_WEBHOOK_SECRET": "mysecret", + "GRAFANA_URL": "http://localhost:3000/" } diff --git a/server/src/config.ts b/server/src/config.ts index adf2ea3..6c3f573 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -23,6 +23,7 @@ export const ZMQ_IPC_PATH = getConfigField('ZMQ_IPC_PATH', path.join(os.tmpdir() export const ZMQ_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002'); export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1'); export const HASTIC_API_KEY = getConfigField('HASTIC_API_KEY'); +export const GRAFANA_URL = getConfigField('GRAFANA_URL', null); export const HASTIC_WEBHOOK_URL = getConfigField('HASTIC_WEBHOOK_URL', null); export const HASTIC_WEBHOOK_TYPE = getConfigField('HASTIC_WEBHOOK_TYPE', 'application/x-www-form-urlencoded'); export const HASTIC_WEBHOOK_SECRET = getConfigField('HASTIC_WEBHOOK_SECRET', null); diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index a9d4733..84fb038 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -6,7 +6,7 @@ import * as Threshold from '../models/threshold_model'; import * as AnalyticUnit from '../models/analytic_unit_model'; import { AnalyticsService } from '../services/analytics_service'; import { sendWebhook } from '../services/notification_service'; -import { HASTIC_API_KEY } from '../config' +import { HASTIC_API_KEY, GRAFANA_URL } from '../config'; import { DataPuller } from '../services/data_puller'; import { queryByMetric } from 'grafana-datasource-kit'; @@ -106,9 +106,17 @@ async function query(analyticUnit: AnalyticUnit.AnalyticUnit, detector: Analytic }; } console.debug(`query time range: from ${new Date(range.from)} to ${new Date(range.to)}`); + + let panelUrl; + if(GRAFANA_URL !== null) { + panelUrl = GRAFANA_URL; + } else { + panelUrl = analyticUnit.panelUrl; + } + const queryResult = await queryByMetric( analyticUnit.metric, - analyticUnit.panelUrl, + panelUrl, range.from, range.to, HASTIC_API_KEY diff --git a/server/src/services/data_puller.ts b/server/src/services/data_puller.ts index 24fe746..70745ea 100644 --- a/server/src/services/data_puller.ts +++ b/server/src/services/data_puller.ts @@ -2,7 +2,7 @@ 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 } from '../config'; +import { HASTIC_API_KEY, GRAFANA_URL } from '../config'; import { queryByMetric } from 'grafana-datasource-kit'; @@ -36,7 +36,15 @@ export class DataPuller { throw Error(`data puller: can't pull undefined unit`); } - return queryByMetric(unit.metric, unit.panelUrl, from, to, HASTIC_API_KEY); + let panelUrl; + if(GRAFANA_URL !== null) { + panelUrl = GRAFANA_URL; + } else { + panelUrl = unit.panelUrl; + } + + return queryByMetric(unit.metric, panelUrl, from, to, HASTIC_API_KEY); + } private pushData(unit: AnalyticUnit.AnalyticUnit, data: any) {