Browse Source

Grafana url to config #114 (#337)

pull/1/head
sanke1 6 years ago committed by rozetko
parent
commit
ccda13d7e8
  1. 3
      config.example.json
  2. 1
      server/src/config.ts
  3. 12
      server/src/controllers/analytics_controller.ts
  4. 12
      server/src/services/data_puller.ts

3
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/"
}

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

12
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

12
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) {

Loading…
Cancel
Save