You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
63 lines
1.8 KiB
2 years ago
|
import { DatasourceConnector, Datasource, DatasourceQuery, DataTable } from '.';
|
||
5 years ago
|
import { processSQLLimitOffset } from './utils';
|
||
|
|
||
|
|
||
|
const INFLUX_QUERY_TIME_REGEX = /time ?[><=]+ ?[^A-Z]+(AND ?time ?[><=]+ ?[^A-Z]+)?/;
|
||
|
|
||
2 years ago
|
export class InfluxdbMetric extends DatasourceConnector {
|
||
5 years ago
|
|
||
|
private _queryParts: string[];
|
||
|
|
||
2 years ago
|
constructor(datasource: Datasource, targets: any[]) {
|
||
|
super(datasource, targets);
|
||
5 years ago
|
|
||
|
var queryStr = datasource.params.q;
|
||
|
this._queryParts = queryStr.split(INFLUX_QUERY_TIME_REGEX);
|
||
|
if(this._queryParts.length == 1) {
|
||
|
throw new Error(
|
||
|
`Query "${queryStr}" is not replaced with LIMIT/OFFSET oeprators. Missing time clause.`
|
||
|
);
|
||
|
}
|
||
|
if(this._queryParts.length > 3) {
|
||
|
throw new Error(`Query "${queryStr}" has multiple time clauses. Can't parse.`);
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
getQuery(from: number, to: number, limit: number, offset: number): DatasourceQuery {
|
||
5 years ago
|
let timeClause = `time >= ${from}ms AND time <= ${to}ms`;
|
||
|
let q = `${this._queryParts[0]} ${timeClause} ${this._queryParts[2]}`;
|
||
|
q = processSQLLimitOffset(q, limit, offset);
|
||
|
return {
|
||
|
url: this.datasource.url,
|
||
|
method: 'GET',
|
||
|
schema: {
|
||
|
params: {
|
||
|
q,
|
||
|
db: this.datasource.params.db,
|
||
|
epoch: this.datasource.params.epoch
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
parseResponse(res): DataTable {
|
||
5 years ago
|
let emptyResult = {
|
||
|
columns: ['timestamp', 'target'],
|
||
|
values: []
|
||
|
};
|
||
|
|
||
|
if(res.data === undefined || res.data.results.length < 1) {
|
||
|
console.log('datasource return empty response, no data');
|
||
|
return emptyResult;
|
||
|
}
|
||
|
|
||
|
// TODO: support more than 1 metric (each res.data.results item is a metric)
|
||
|
let results = res.data.results[0];
|
||
|
if (results.series === undefined) {
|
||
|
return emptyResult;
|
||
|
}
|
||
|
|
||
|
return results.series[0];
|
||
|
}
|
||
|
}
|