2016-02-02 15:58:37 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
2020-06-22 15:03:34 -05:00
|
|
|
import {
|
|
|
|
dateMath,
|
|
|
|
DataSourceInstanceSettings,
|
|
|
|
ScopedVars,
|
|
|
|
DataQueryRequest,
|
|
|
|
DataQueryResponse,
|
|
|
|
dateTime,
|
|
|
|
LoadingState,
|
2020-06-24 15:52:46 -05:00
|
|
|
QueryResultMeta,
|
2020-09-03 16:11:39 -05:00
|
|
|
MetricFindValue,
|
|
|
|
AnnotationQueryRequest,
|
|
|
|
AnnotationEvent,
|
2020-10-27 05:35:59 -05:00
|
|
|
DataQueryError,
|
2020-06-22 15:03:34 -05:00
|
|
|
} from '@grafana/data';
|
2020-08-26 04:38:39 -05:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2016-02-02 15:58:37 -06:00
|
|
|
import InfluxSeries from './influx_series';
|
2019-06-10 07:39:53 -05:00
|
|
|
import InfluxQueryModel from './influx_query_model';
|
2016-03-22 14:23:27 -05:00
|
|
|
import ResponseParser from './response_parser';
|
2017-12-20 05:33:33 -06:00
|
|
|
import { InfluxQueryBuilder } from './query_builder';
|
2020-06-22 15:03:34 -05:00
|
|
|
import { InfluxQuery, InfluxOptions, InfluxVersion } from './types';
|
2020-10-01 12:51:23 -05:00
|
|
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
|
|
|
import { getBackendSrv, DataSourceWithBackend, frameToMetricFindValue } from '@grafana/runtime';
|
2020-10-27 05:35:59 -05:00
|
|
|
import { Observable, throwError, of } from 'rxjs';
|
2020-09-11 10:09:44 -05:00
|
|
|
import { FluxQueryEditor } from './components/FluxQueryEditor';
|
2020-10-27 05:35:59 -05:00
|
|
|
import { catchError, map } from 'rxjs/operators';
|
2016-09-21 01:46:59 -05:00
|
|
|
|
2020-06-10 14:26:24 -05:00
|
|
|
export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery, InfluxOptions> {
|
2016-03-23 08:42:18 -05:00
|
|
|
type: string;
|
2020-06-10 14:26:24 -05:00
|
|
|
urls: string[];
|
2016-03-23 08:42:18 -05:00
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
name: string;
|
|
|
|
database: any;
|
|
|
|
basicAuth: any;
|
2016-09-13 01:57:39 -05:00
|
|
|
withCredentials: any;
|
2016-03-23 08:42:18 -05:00
|
|
|
interval: any;
|
|
|
|
responseParser: any;
|
2019-05-02 08:30:37 -05:00
|
|
|
httpMode: string;
|
2020-09-03 16:11:39 -05:00
|
|
|
isFlux: boolean;
|
2016-03-23 08:42:18 -05:00
|
|
|
|
2020-10-01 12:51:23 -05:00
|
|
|
constructor(
|
|
|
|
instanceSettings: DataSourceInstanceSettings<InfluxOptions>,
|
|
|
|
private readonly templateSrv: TemplateSrv = getTemplateSrv()
|
|
|
|
) {
|
2019-06-10 07:39:53 -05:00
|
|
|
super(instanceSettings);
|
2020-07-08 04:05:20 -05:00
|
|
|
|
2016-03-23 08:42:18 -05:00
|
|
|
this.type = 'influxdb';
|
2021-01-20 00:59:48 -06:00
|
|
|
this.urls = (instanceSettings.url ?? '').split(',').map((url) => {
|
2016-03-23 08:42:18 -05:00
|
|
|
return url.trim();
|
|
|
|
});
|
|
|
|
|
2020-06-10 14:26:24 -05:00
|
|
|
this.username = instanceSettings.username ?? '';
|
|
|
|
this.password = instanceSettings.password ?? '';
|
2016-03-23 08:42:18 -05:00
|
|
|
this.name = instanceSettings.name;
|
|
|
|
this.database = instanceSettings.database;
|
|
|
|
this.basicAuth = instanceSettings.basicAuth;
|
2016-09-13 01:57:39 -05:00
|
|
|
this.withCredentials = instanceSettings.withCredentials;
|
2019-06-10 07:39:53 -05:00
|
|
|
const settingsData = instanceSettings.jsonData || ({} as InfluxOptions);
|
|
|
|
this.interval = settingsData.timeInterval;
|
|
|
|
this.httpMode = settingsData.httpMode || 'GET';
|
2016-03-23 08:42:18 -05:00
|
|
|
this.responseParser = new ResponseParser();
|
2020-09-03 16:11:39 -05:00
|
|
|
this.isFlux = settingsData.version === InfluxVersion.Flux;
|
2020-09-11 10:09:44 -05:00
|
|
|
|
|
|
|
if (this.isFlux) {
|
|
|
|
// When flux, use an annotation processor rather than the `annotationQuery` lifecycle
|
|
|
|
this.annotations = {
|
|
|
|
QueryEditor: FluxQueryEditor,
|
|
|
|
};
|
|
|
|
}
|
2016-03-23 08:42:18 -05:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:26:24 -05:00
|
|
|
query(request: DataQueryRequest<InfluxQuery>): Observable<DataQueryResponse> {
|
2020-09-03 16:11:39 -05:00
|
|
|
if (this.isFlux) {
|
2021-02-26 03:27:07 -06:00
|
|
|
// for not-flux queries we call `this.classicQuery`, and that
|
|
|
|
// handles the is-hidden situation.
|
|
|
|
// for the flux-case, we do the filtering here
|
|
|
|
const filteredRequest = {
|
|
|
|
...request,
|
|
|
|
targets: request.targets.filter((t) => t.hide !== true),
|
|
|
|
};
|
|
|
|
return super.query(filteredRequest);
|
2020-06-10 14:26:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to classic query support
|
2020-10-27 05:35:59 -05:00
|
|
|
return this.classicQuery(request);
|
2020-06-10 14:26:24 -05:00
|
|
|
}
|
|
|
|
|
2020-07-10 12:07:36 -05:00
|
|
|
getQueryDisplayText(query: InfluxQuery) {
|
2020-09-03 16:11:39 -05:00
|
|
|
if (this.isFlux) {
|
2020-07-10 12:07:36 -05:00
|
|
|
return query.query;
|
|
|
|
}
|
|
|
|
return new InfluxQueryModel(query).render(false);
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:09:44 -05:00
|
|
|
/**
|
|
|
|
* Returns false if the query should be skipped
|
|
|
|
*/
|
|
|
|
filterQuery(query: InfluxQuery): boolean {
|
|
|
|
if (this.isFlux) {
|
|
|
|
return !!query.query;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:26:24 -05:00
|
|
|
/**
|
|
|
|
* Only applied on flux queries
|
|
|
|
*/
|
|
|
|
applyTemplateVariables(query: InfluxQuery, scopedVars: ScopedVars): Record<string, any> {
|
|
|
|
return {
|
|
|
|
...query,
|
2020-10-01 12:51:23 -05:00
|
|
|
query: this.templateSrv.replace(query.query ?? '', scopedVars), // The raw query text
|
2020-06-10 14:26:24 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The unchanged pre 7.1 query implementation
|
|
|
|
*/
|
2020-10-27 05:35:59 -05:00
|
|
|
classicQuery(options: any): Observable<DataQueryResponse> {
|
2018-08-30 02:03:11 -05:00
|
|
|
let timeFilter = this.getTimeFilter(options);
|
2018-08-29 07:27:29 -05:00
|
|
|
const scopedVars = options.scopedVars;
|
|
|
|
const targets = _.cloneDeep(options.targets);
|
2019-07-05 00:52:23 -05:00
|
|
|
const queryTargets: any[] = [];
|
2020-07-08 04:05:20 -05:00
|
|
|
|
2018-08-30 02:03:11 -05:00
|
|
|
let i, y;
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2021-01-20 00:59:48 -06:00
|
|
|
let allQueries = _.map(targets, (target) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (target.hide) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '';
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
|
|
|
queryTargets.push(target);
|
|
|
|
|
2018-04-13 12:48:37 -05:00
|
|
|
// backward compatibility
|
2017-01-09 08:31:19 -06:00
|
|
|
scopedVars.interval = scopedVars.__interval;
|
2016-09-14 10:36:28 -05:00
|
|
|
|
2020-10-01 12:51:23 -05:00
|
|
|
return new InfluxQueryModel(target, this.templateSrv, scopedVars).render(true);
|
2016-05-13 06:26:57 -05:00
|
|
|
}).reduce((acc, current) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (current !== '') {
|
|
|
|
acc += ';' + current;
|
2016-05-13 06:26:57 -05:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
});
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2016-09-20 09:29:48 -05:00
|
|
|
if (allQueries === '') {
|
2020-10-27 05:35:59 -05:00
|
|
|
return of({ data: [] });
|
2016-09-20 09:29:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// add global adhoc filters to timeFilter
|
2020-10-01 12:51:23 -05:00
|
|
|
const adhocFilters = this.templateSrv.getAdhocFilters(this.name);
|
2017-12-19 09:06:54 -06:00
|
|
|
if (adhocFilters.length > 0) {
|
2020-10-01 12:51:23 -05:00
|
|
|
const tmpQuery = new InfluxQueryModel({ refId: 'A' }, this.templateSrv, scopedVars);
|
2020-07-08 04:05:20 -05:00
|
|
|
timeFilter += ' AND ' + tmpQuery.renderAdhocFilters(adhocFilters);
|
2016-09-20 09:29:48 -05:00
|
|
|
}
|
|
|
|
|
2016-02-02 15:58:37 -06:00
|
|
|
// replace grafana variables
|
2017-12-19 09:06:54 -06:00
|
|
|
scopedVars.timeFilter = { value: timeFilter };
|
2016-02-02 15:58:37 -06:00
|
|
|
|
|
|
|
// replace templated variables
|
2020-10-01 12:51:23 -05:00
|
|
|
allQueries = this.templateSrv.replace(allQueries, scopedVars);
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
return this._seriesQuery(allQueries, options).pipe(
|
|
|
|
map((data: any) => {
|
|
|
|
if (!data || !data.results) {
|
|
|
|
return { data: [] };
|
2019-11-19 07:59:39 -06:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
const seriesList = [];
|
|
|
|
for (i = 0; i < data.results.length; i++) {
|
|
|
|
const result = data.results[i];
|
|
|
|
if (!result || !result.series) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-19 08:41:35 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
const target = queryTargets[i];
|
|
|
|
let alias = target.alias;
|
|
|
|
if (alias) {
|
|
|
|
alias = this.templateSrv.replace(target.alias, options.scopedVars);
|
|
|
|
}
|
2020-06-24 15:52:46 -05:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
const meta: QueryResultMeta = {
|
|
|
|
executedQueryString: data.executedQueryString,
|
|
|
|
};
|
2019-02-19 08:41:35 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
const influxSeries = new InfluxSeries({
|
|
|
|
refId: target.refId,
|
|
|
|
series: data.results[i].series,
|
|
|
|
alias: alias,
|
|
|
|
meta,
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (target.resultFormat) {
|
|
|
|
case 'logs':
|
|
|
|
meta.preferredVisualisationType = 'logs';
|
|
|
|
case 'table': {
|
|
|
|
seriesList.push(influxSeries.getTable());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
const timeSeries = influxSeries.getTimeSeries();
|
|
|
|
for (y = 0; y < timeSeries.length; y++) {
|
|
|
|
seriesList.push(timeSeries[y]);
|
|
|
|
}
|
|
|
|
break;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 07:59:39 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
return { data: seriesList };
|
|
|
|
})
|
|
|
|
);
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-09-03 16:11:39 -05:00
|
|
|
async annotationQuery(options: AnnotationQueryRequest<any>): Promise<AnnotationEvent[]> {
|
|
|
|
if (this.isFlux) {
|
|
|
|
return Promise.reject({
|
2020-09-11 10:09:44 -05:00
|
|
|
message: 'Flux requires the standard annotation query',
|
2020-09-03 16:11:39 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfluxQL puts a query string on the annotation
|
2016-02-22 07:54:19 -06:00
|
|
|
if (!options.annotation.query) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.reject({
|
2017-12-20 05:33:33 -06:00
|
|
|
message: 'Query missing in annotation definition',
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-02-22 07:54:19 -06:00
|
|
|
}
|
|
|
|
|
2020-09-03 16:11:39 -05:00
|
|
|
const timeFilter = this.getTimeFilter({ rangeRaw: options.rangeRaw, timezone: options.dashboard.timezone });
|
2018-08-30 02:03:11 -05:00
|
|
|
let query = options.annotation.query.replace('$timeFilter', timeFilter);
|
2020-10-01 12:51:23 -05:00
|
|
|
query = this.templateSrv.replace(query, undefined, 'regex');
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
return this._seriesQuery(query, options)
|
|
|
|
.toPromise()
|
|
|
|
.then((data: any) => {
|
|
|
|
if (!data || !data.results || !data.results[0]) {
|
|
|
|
throw { message: 'No results in response from InfluxDB' };
|
|
|
|
}
|
|
|
|
return new InfluxSeries({
|
|
|
|
series: data.results[0].series,
|
|
|
|
annotation: options.annotation,
|
|
|
|
}).getAnnotations();
|
|
|
|
});
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
targetContainsTemplate(target: any) {
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const group of target.groupBy) {
|
|
|
|
for (const param of group.params) {
|
2020-10-01 12:51:23 -05:00
|
|
|
if (this.templateSrv.variableExists(param)) {
|
2016-10-11 09:00:59 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const i in target.tags) {
|
2020-10-01 12:51:23 -05:00
|
|
|
if (this.templateSrv.variableExists(target.tags[i].value)) {
|
2016-10-11 09:00:59 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-10-11 09:00:59 -05:00
|
|
|
|
2020-01-24 02:50:09 -06:00
|
|
|
interpolateVariablesInQueries(queries: InfluxQuery[], scopedVars: ScopedVars): InfluxQuery[] {
|
2019-10-08 10:01:20 -05:00
|
|
|
if (!queries || queries.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let expandedQueries = queries;
|
|
|
|
if (queries && queries.length > 0) {
|
2021-01-20 00:59:48 -06:00
|
|
|
expandedQueries = queries.map((query) => {
|
2019-10-08 10:01:20 -05:00
|
|
|
const expandedQuery = {
|
|
|
|
...query,
|
|
|
|
datasource: this.name,
|
2020-10-01 12:51:23 -05:00
|
|
|
measurement: this.templateSrv.replace(query.measurement ?? '', scopedVars, 'regex'),
|
|
|
|
policy: this.templateSrv.replace(query.policy ?? '', scopedVars, 'regex'),
|
2019-10-08 10:01:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (query.rawQuery) {
|
2020-10-01 12:51:23 -05:00
|
|
|
expandedQuery.query = this.templateSrv.replace(query.query ?? '', scopedVars, 'regex');
|
2019-10-08 10:01:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (query.tags) {
|
2021-01-20 00:59:48 -06:00
|
|
|
expandedQuery.tags = query.tags.map((tag) => {
|
2020-10-27 05:35:59 -05:00
|
|
|
return {
|
2019-10-08 10:01:20 -05:00
|
|
|
...tag,
|
2020-10-01 12:51:23 -05:00
|
|
|
value: this.templateSrv.replace(tag.value, undefined, 'regex'),
|
2019-10-08 10:01:20 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return expandedQuery;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return expandedQueries;
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:11:39 -05:00
|
|
|
async metricFindQuery(query: string, options?: any): Promise<MetricFindValue[]> {
|
|
|
|
if (this.isFlux) {
|
|
|
|
const target: InfluxQuery = {
|
|
|
|
refId: 'metricFindQuery',
|
|
|
|
query,
|
|
|
|
};
|
|
|
|
return super
|
|
|
|
.query({
|
|
|
|
...options, // includes 'range'
|
|
|
|
targets: [target],
|
|
|
|
} as DataQueryRequest)
|
|
|
|
.toPromise()
|
2021-01-20 00:59:48 -06:00
|
|
|
.then((rsp) => {
|
2020-09-03 16:11:39 -05:00
|
|
|
if (rsp.data?.length) {
|
|
|
|
return frameToMetricFindValue(rsp.data[0]);
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:51:23 -05:00
|
|
|
const interpolated = this.templateSrv.replace(query, undefined, 'regex');
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
return this._seriesQuery(interpolated, options)
|
|
|
|
.toPromise()
|
2021-01-20 00:59:48 -06:00
|
|
|
.then((resp) => {
|
2020-10-27 05:35:59 -05:00
|
|
|
return this.responseParser.parse(query, resp);
|
|
|
|
});
|
2016-09-14 10:36:28 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:13:06 -05:00
|
|
|
getTagKeys(options: any = {}) {
|
|
|
|
const queryBuilder = new InfluxQueryBuilder({ measurement: options.measurement || '', tags: [] }, this.database);
|
2018-08-29 07:27:29 -05:00
|
|
|
const query = queryBuilder.buildExploreQuery('TAG_KEYS');
|
2017-04-18 11:27:25 -05:00
|
|
|
return this.metricFindQuery(query, options);
|
2016-09-14 10:36:28 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 03:13:06 -05:00
|
|
|
getTagValues(options: any = {}) {
|
|
|
|
const queryBuilder = new InfluxQueryBuilder({ measurement: options.measurement || '', tags: [] }, this.database);
|
2018-08-29 07:27:29 -05:00
|
|
|
const query = queryBuilder.buildExploreQuery('TAG_VALUES', options.key);
|
2017-04-18 11:27:25 -05:00
|
|
|
return this.metricFindQuery(query, options);
|
2016-09-14 10:36:28 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2017-04-12 11:02:06 -05:00
|
|
|
_seriesQuery(query: string, options?: any) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!query) {
|
2020-10-27 05:35:59 -05:00
|
|
|
return of({ results: [] });
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2016-06-21 09:13:31 -05:00
|
|
|
|
2018-07-13 06:24:56 -05:00
|
|
|
if (options && options.range) {
|
2019-02-11 06:11:56 -06:00
|
|
|
const timeFilter = this.getTimeFilter({ rangeRaw: options.range, timezone: options.timezone });
|
2018-07-13 06:24:56 -05:00
|
|
|
query = query.replace('$timeFilter', timeFilter);
|
|
|
|
}
|
|
|
|
|
2019-05-02 08:30:37 -05:00
|
|
|
return this._influxRequest(this.httpMode, '/query', { q: query, epoch: 'ms' }, options);
|
2016-03-23 08:42:18 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
serializeParams(params: any) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!params) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '';
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2016-04-09 10:00:35 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
return _.reduce(
|
|
|
|
params,
|
|
|
|
(memo, value, key) => {
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
return memo;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
memo.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
|
2017-12-19 09:06:54 -06:00
|
|
|
return memo;
|
|
|
|
},
|
2020-07-08 04:05:20 -05:00
|
|
|
[] as string[]
|
2017-12-20 05:33:33 -06:00
|
|
|
).join('&');
|
2016-04-09 10:00:35 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 08:42:18 -05:00
|
|
|
testDatasource() {
|
2020-09-03 16:11:39 -05:00
|
|
|
if (this.isFlux) {
|
2020-06-22 15:03:34 -05:00
|
|
|
// TODO: eventually use the real /health endpoint
|
|
|
|
const request: DataQueryRequest<InfluxQuery> = {
|
|
|
|
targets: [{ refId: 'test', query: 'buckets()' }],
|
2020-08-26 04:38:39 -05:00
|
|
|
requestId: `${this.id}-health-${uuidv4()}`,
|
2020-06-22 15:03:34 -05:00
|
|
|
dashboardId: 0,
|
|
|
|
panelId: 0,
|
|
|
|
interval: '1m',
|
|
|
|
intervalMs: 60000,
|
|
|
|
maxDataPoints: 423,
|
|
|
|
range: {
|
|
|
|
from: dateTime(1000),
|
|
|
|
to: dateTime(2000),
|
|
|
|
},
|
|
|
|
} as DataQueryRequest<InfluxQuery>;
|
|
|
|
|
|
|
|
return super
|
|
|
|
.query(request)
|
|
|
|
.toPromise()
|
2020-06-23 18:30:07 -05:00
|
|
|
.then((res: DataQueryResponse) => {
|
|
|
|
if (!res || !res.data || res.state !== LoadingState.Done) {
|
2020-07-10 09:07:04 -05:00
|
|
|
console.error('InfluxDB Error', res);
|
2020-06-23 18:30:07 -05:00
|
|
|
return { status: 'error', message: 'Error reading InfluxDB' };
|
2020-06-22 15:03:34 -05:00
|
|
|
}
|
2020-06-23 18:30:07 -05:00
|
|
|
const first = res.data[0];
|
|
|
|
if (first && first.length) {
|
|
|
|
return { status: 'success', message: `${first.length} buckets found` };
|
|
|
|
}
|
2020-07-10 09:07:04 -05:00
|
|
|
console.error('InfluxDB Error', res);
|
2020-06-22 15:03:34 -05:00
|
|
|
return { status: 'error', message: 'Error reading buckets' };
|
|
|
|
})
|
|
|
|
.catch((err: any) => {
|
2020-07-10 09:07:04 -05:00
|
|
|
console.error('InfluxDB Error', err);
|
2020-06-22 15:03:34 -05:00
|
|
|
return { status: 'error', message: err.message };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
const queryBuilder = new InfluxQueryBuilder({ measurement: '', tags: [] }, this.database);
|
|
|
|
const query = queryBuilder.buildExploreQuery('RETENTION POLICIES');
|
2017-12-19 09:06:54 -06:00
|
|
|
|
|
|
|
return this._seriesQuery(query)
|
2020-10-27 05:35:59 -05:00
|
|
|
.toPromise()
|
2019-07-05 00:52:23 -05:00
|
|
|
.then((res: any) => {
|
2018-08-26 10:14:40 -05:00
|
|
|
const error = _.get(res, 'results[0].error');
|
2017-12-19 09:06:54 -06:00
|
|
|
if (error) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return { status: 'error', message: error };
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
return { status: 'success', message: 'Data source is working' };
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
2019-07-05 00:52:23 -05:00
|
|
|
.catch((err: any) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
return { status: 'error', message: err.message };
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-03-23 08:42:18 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2017-04-12 11:02:06 -05:00
|
|
|
_influxRequest(method: string, url: string, data: any, options?: any) {
|
2020-07-08 04:05:20 -05:00
|
|
|
const currentUrl = this.urls.shift()!;
|
2017-04-11 18:30:20 -05:00
|
|
|
this.urls.push(currentUrl);
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2018-08-26 10:14:40 -05:00
|
|
|
const params: any = {};
|
2017-05-19 10:35:36 -05:00
|
|
|
|
2017-06-19 18:13:16 -05:00
|
|
|
if (this.username) {
|
2018-03-14 17:52:24 -05:00
|
|
|
params.u = this.username;
|
|
|
|
params.p = this.password;
|
2017-05-19 10:35:36 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2017-04-12 11:02:06 -05:00
|
|
|
if (options && options.database) {
|
|
|
|
params.db = options.database;
|
2017-04-11 18:30:20 -05:00
|
|
|
} else if (this.database) {
|
|
|
|
params.db = this.database;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:52:46 -05:00
|
|
|
const { q } = data;
|
|
|
|
|
2019-05-02 08:30:37 -05:00
|
|
|
if (method === 'POST' && _.has(data, 'q')) {
|
|
|
|
// verb is POST and 'q' param is defined
|
|
|
|
_.extend(params, _.omit(data, ['q']));
|
|
|
|
data = this.serializeParams(_.pick(data, ['q']));
|
|
|
|
} else if (method === 'GET' || method === 'POST') {
|
|
|
|
// verb is GET, or POST without 'q' param
|
2016-02-02 15:58:37 -06:00
|
|
|
_.extend(params, data);
|
|
|
|
data = null;
|
|
|
|
}
|
|
|
|
|
2018-08-26 10:14:40 -05:00
|
|
|
const req: any = {
|
2016-02-02 15:58:37 -06:00
|
|
|
method: method,
|
2017-12-19 09:06:54 -06:00
|
|
|
url: currentUrl + url,
|
2016-02-02 15:58:37 -06:00
|
|
|
params: params,
|
2017-12-19 09:06:54 -06:00
|
|
|
data: data,
|
2017-12-20 05:33:33 -06:00
|
|
|
precision: 'ms',
|
2016-02-02 15:58:37 -06:00
|
|
|
inspect: { type: 'influxdb' },
|
2016-04-09 10:00:35 -05:00
|
|
|
paramSerializer: this.serializeParams,
|
2016-02-02 15:58:37 -06:00
|
|
|
};
|
|
|
|
|
2017-04-12 11:02:06 -05:00
|
|
|
req.headers = req.headers || {};
|
2016-09-13 01:57:39 -05:00
|
|
|
if (this.basicAuth || this.withCredentials) {
|
2017-04-12 11:02:06 -05:00
|
|
|
req.withCredentials = true;
|
2016-09-13 01:57:39 -05:00
|
|
|
}
|
2017-04-11 18:30:20 -05:00
|
|
|
if (this.basicAuth) {
|
2017-04-12 11:02:06 -05:00
|
|
|
req.headers.Authorization = this.basicAuth;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2019-05-02 08:30:37 -05:00
|
|
|
if (method === 'POST') {
|
|
|
|
req.headers['Content-type'] = 'application/x-www-form-urlencoded';
|
|
|
|
}
|
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv()
|
2020-10-27 05:35:59 -05:00
|
|
|
.fetch(req)
|
|
|
|
.pipe(
|
|
|
|
map((result: any) => {
|
2020-06-24 15:52:46 -05:00
|
|
|
const { data } = result;
|
|
|
|
if (data) {
|
|
|
|
data.executedQueryString = q;
|
|
|
|
if (data.results) {
|
|
|
|
const errors = result.data.results.filter((elem: any) => elem.error);
|
2020-10-27 05:35:59 -05:00
|
|
|
|
2020-06-24 15:52:46 -05:00
|
|
|
if (errors.length > 0) {
|
|
|
|
throw {
|
|
|
|
message: 'InfluxDB Error: ' + errors[0].error,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
2020-05-25 10:56:16 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 15:52:46 -05:00
|
|
|
return data;
|
2020-10-27 05:35:59 -05:00
|
|
|
}),
|
2021-01-20 00:59:48 -06:00
|
|
|
catchError((err) => {
|
2020-10-27 05:35:59 -05:00
|
|
|
if (err.cancelled) {
|
|
|
|
return of(err);
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2020-10-27 05:35:59 -05:00
|
|
|
|
|
|
|
return throwError(this.handleErrors(err));
|
|
|
|
})
|
2020-01-21 03:08:07 -06:00
|
|
|
);
|
2017-04-20 04:16:37 -05:00
|
|
|
}
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-10-27 05:35:59 -05:00
|
|
|
handleErrors(err: any) {
|
|
|
|
const error: DataQueryError = {
|
|
|
|
message:
|
|
|
|
(err && err.status) ||
|
|
|
|
(err && err.message) ||
|
|
|
|
'Unknown error during query transaction. Please check JS console logs.',
|
|
|
|
};
|
|
|
|
|
|
|
|
if ((Number.isInteger(err.status) && err.status !== 0) || err.status >= 300) {
|
|
|
|
if (err.data && err.data.error) {
|
|
|
|
error.message = 'InfluxDB Error: ' + err.data.error;
|
|
|
|
error.data = err.data;
|
|
|
|
// @ts-ignore
|
|
|
|
error.config = err.config;
|
|
|
|
} else {
|
|
|
|
error.message = 'Network Error: ' + err.statusText + '(' + err.status + ')';
|
|
|
|
error.data = err.data;
|
|
|
|
// @ts-ignore
|
|
|
|
error.config = err.config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
getTimeFilter(options: any) {
|
2019-02-11 06:11:56 -06:00
|
|
|
const from = this.getInfluxTime(options.rangeRaw.from, false, options.timezone);
|
|
|
|
const until = this.getInfluxTime(options.rangeRaw.to, true, options.timezone);
|
2018-08-29 07:27:29 -05:00
|
|
|
const fromIsAbsolute = from[from.length - 1] === 'ms';
|
2016-02-02 15:58:37 -06:00
|
|
|
|
|
|
|
if (until === 'now()' && !fromIsAbsolute) {
|
2017-08-07 02:26:32 -05:00
|
|
|
return 'time >= ' + from;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2017-08-07 02:26:32 -05:00
|
|
|
return 'time >= ' + from + ' and time <= ' + until;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
getInfluxTime(date: any, roundUp: any, timezone: any) {
|
2016-02-02 15:58:37 -06:00
|
|
|
if (_.isString(date)) {
|
|
|
|
if (date === 'now') {
|
|
|
|
return 'now()';
|
|
|
|
}
|
|
|
|
|
2019-01-05 08:19:54 -06:00
|
|
|
const parts = /^now-(\d+)([dhms])$/.exec(date);
|
2016-02-02 15:58:37 -06:00
|
|
|
if (parts) {
|
2018-09-05 09:51:31 -05:00
|
|
|
const amount = parseInt(parts[1], 10);
|
2018-08-29 07:27:29 -05:00
|
|
|
const unit = parts[2];
|
2016-02-02 15:58:37 -06:00
|
|
|
return 'now() - ' + amount + unit;
|
|
|
|
}
|
2019-02-11 06:11:56 -06:00
|
|
|
date = dateMath.parse(date, roundUp, timezone);
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
2016-11-28 16:57:36 -06:00
|
|
|
|
2016-12-06 01:56:01 -06:00
|
|
|
return date.valueOf() + 'ms';
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
}
|