|
|
|
@ -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'); |
|
|
|
|
} |
|
|
|
|