Browse Source

WIP: Query grafana from node (#109)

* Fix: analytic_unit_worker get_model -> get_predictor

* Fix analyticUnit.fromObject arguments order

* Make Grafana querying work
pull/1/head
rozetko 6 years ago committed by Alexey Velikiy
parent
commit
a8a8220cf7
  1. 4
      analytics/analytic_unit_worker.py
  2. 5
      server/src/controllers/analytics_controller.ts
  3. 4
      server/src/models/analytic_unit_model.ts
  4. 12
      server/src/models/metric_model.ts
  5. 23
      server/src/services/grafana_service.ts

4
analytics/analytic_unit_worker.py

@ -45,7 +45,7 @@ class AnalyticUnitWorker(object):
pattern = payload['pattern']
segments = payload['segments']
model = self.get_model(analytic_unit_id, pattern)
model = self.get_detector(analytic_unit_id, pattern)
model.synchronize_data()
last_prediction_time = await model.learn(segments)
# TODO: we should not do predict before labeling in all models, not just in drops
@ -68,7 +68,7 @@ class AnalyticUnitWorker(object):
pattern = payload['pattern']
last_prediction_time = payload['lastPredictionTime']
model = self.get_model(analytic_unit_id, pattern)
model = self.get_detector(analytic_unit_id, pattern)
model.synchronize_data()
segments, last_prediction_time = await model.predict(last_prediction_time)
return {

5
server/src/controllers/analytics_controller.ts

@ -81,7 +81,7 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) {
let analyticUnit = await AnalyticUnit.findById(id);
let segmentObjs = segments.map(s => s.toObject());
let data = await queryByMetric(analyticUnit.metric);
let data = await queryByMetric(analyticUnit.metric, analyticUnit.panelUrl);
if(data.length === 0) {
throw new Error('Empty data to learn on');
@ -175,6 +175,9 @@ export function isAnalyticReady(): boolean {
}
export async function createAnalyticUnitFromObject(obj: any): Promise<AnalyticUnit.AnalyticUnitId> {
if(obj.datasource !== undefined) {
obj.metric.datasource = obj.datasource;
}
let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.AnalyticUnit.fromObject(obj);
let id = await AnalyticUnit.create(unit);
return id;

4
server/src/models/analytic_unit_model.ts

@ -61,9 +61,9 @@ export class AnalyticUnit {
obj.panelUrl,
obj.type,
Metric.fromObject(obj.metric),
obj.status as AnalyticUnitStatus,
obj.lastPredictionTime,
obj._id,
obj.lastPredictionTime,
obj.status as AnalyticUnitStatus,
obj.error,
);
}

12
server/src/models/metric_model.ts

@ -1,8 +1,18 @@
type Datasource = {
url: string,
type: string,
params: {
db: string,
q: string,
epoch: string
}
}
export type MetricId = string;
export class Metric {
constructor(
public datasource: string,
public datasource: Datasource,
public targets: any[],
public id?: MetricId
) {

23
server/src/services/grafana_service.ts

@ -2,6 +2,8 @@ import { Metric } from '../models/metric_model';
import { HASTIC_API_KEY } from '../config';
import { URL } from 'url';
import { stringify } from 'querystring';
import axios from 'axios';
@ -10,14 +12,23 @@ export type Timestamp = number;
* @param metric to query to Grafana
* @returns [time, value][] array
*/
export async function queryByMetric(metric: Metric): Promise<[number, number][]> {
var params = {} + '';
export async function queryByMetric(metric: Metric, panelUrl: string): Promise<[number, number][]> {
let datasource = metric.datasource;
if(datasource.type !== 'influxdb') {
throw new Error(`${datasource.type} queries are not supported yet`);
}
var params = {};
let origin = new URL(panelUrl).origin;
let url = `${origin}/${datasource.url}?${stringify(params)}`;
console.log(url)
let headers = { 'Authorization': 'Bearer ' + HASTIC_API_KEY };
let url = metric.datasource['origin'] + '/' +
metric.datasource['url'] + '?' +
encodeURIComponent(params);
var res = await axios.get(url, { headers });
let res = await axios.get(url, { headers });
let results = res.data['results'];
console.log(results)
if(results === undefined) {
throw new Error('reuslts field is undefined in response');
}

Loading…
Cancel
Save