diff --git a/analytics/analytic_unit_worker.py b/analytics/analytic_unit_worker.py index 294af05..a2a3501 100644 --- a/analytics/analytic_unit_worker.py +++ b/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 { diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 609a5ef..8f2f7a5 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -76,12 +76,12 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { let previousLastPredictionTime: number = undefined; try { - + let segments = await Segment.findMany(id, { labeled: true }); 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 { + 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; diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 4f22474..4c552b6 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/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, ); } diff --git a/server/src/models/metric_model.ts b/server/src/models/metric_model.ts index 406418b..d1c1936 100644 --- a/server/src/models/metric_model.ts +++ b/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 ) { diff --git a/server/src/services/grafana_service.ts b/server/src/services/grafana_service.ts index ac6d83b..c736ef4 100644 --- a/server/src/services/grafana_service.ts +++ b/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'); }