2021-05-05 09:46:07 -05:00
|
|
|
import { map as _map } from 'lodash';
|
|
|
|
import { of } from 'rxjs';
|
2020-09-25 05:46:28 -05:00
|
|
|
import { catchError, map, mapTo } from 'rxjs/operators';
|
2021-05-05 09:46:07 -05:00
|
|
|
import { getBackendSrv, DataSourceWithBackend, FetchResponse, BackendDataSourceResponse } from '@grafana/runtime';
|
|
|
|
import { DataSourceInstanceSettings, ScopedVars, MetricFindValue, AnnotationEvent } from '@grafana/data';
|
|
|
|
import MySQLQueryModel from 'app/plugins/datasource/mysql/mysql_query_model';
|
|
|
|
import ResponseParser from './response_parser';
|
|
|
|
import { MysqlQueryForInterpolation, MySQLOptions, MySQLQuery } from './types';
|
2020-10-01 12:51:23 -05:00
|
|
|
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
|
2020-06-04 06:44:48 -05:00
|
|
|
import { getSearchFilterScopedVar } from '../../../features/variables/utils';
|
2021-05-05 09:46:07 -05:00
|
|
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2017-03-29 13:43:20 -05:00
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOptions> {
|
2017-03-29 15:54:07 -05:00
|
|
|
id: any;
|
|
|
|
name: any;
|
2017-08-10 07:39:36 -05:00
|
|
|
responseParser: ResponseParser;
|
2021-05-05 09:46:07 -05:00
|
|
|
queryModel: MySQLQueryModel;
|
2018-09-05 05:02:57 -05:00
|
|
|
interval: string;
|
2017-03-29 13:43:20 -05:00
|
|
|
|
2020-10-01 12:51:23 -05:00
|
|
|
constructor(
|
2021-05-05 09:46:07 -05:00
|
|
|
instanceSettings: DataSourceInstanceSettings<MySQLOptions>,
|
2020-10-01 12:51:23 -05:00
|
|
|
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
|
|
|
|
private readonly timeSrv: TimeSrv = getTimeSrv()
|
|
|
|
) {
|
2021-05-05 09:46:07 -05:00
|
|
|
super(instanceSettings);
|
2017-03-29 15:54:07 -05:00
|
|
|
this.name = instanceSettings.name;
|
|
|
|
this.id = instanceSettings.id;
|
2019-12-05 03:04:03 -06:00
|
|
|
this.responseParser = new ResponseParser();
|
2021-05-05 09:46:07 -05:00
|
|
|
this.queryModel = new MySQLQueryModel({});
|
|
|
|
const settingsData = instanceSettings.jsonData || ({} as MySQLOptions);
|
|
|
|
this.interval = settingsData.timeInterval || '1m';
|
2017-03-29 13:43:20 -05:00
|
|
|
}
|
|
|
|
|
2020-09-25 05:46:28 -05:00
|
|
|
interpolateVariable = (value: string | string[] | number, variable: any) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (typeof value === 'string') {
|
2017-10-31 06:23:50 -05:00
|
|
|
if (variable.multi || variable.includeAll) {
|
2020-01-24 02:50:09 -06:00
|
|
|
const result = this.queryModel.quoteLiteral(value);
|
|
|
|
return result;
|
2017-10-31 06:23:50 -05:00
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
2017-04-23 07:22:47 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (typeof value === 'number') {
|
2017-10-10 04:59:44 -05:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-04-21 02:38:00 -05:00
|
|
|
const quotedValues = _map(value, (v: any) => {
|
2018-08-31 09:27:48 -05:00
|
|
|
return this.queryModel.quoteLiteral(v);
|
2017-04-23 07:22:47 -05:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
return quotedValues.join(',');
|
2018-10-19 03:19:33 -05:00
|
|
|
};
|
2017-04-23 07:22:47 -05:00
|
|
|
|
2020-01-24 02:50:09 -06:00
|
|
|
interpolateVariablesInQueries(
|
|
|
|
queries: MysqlQueryForInterpolation[],
|
|
|
|
scopedVars: ScopedVars
|
|
|
|
): MysqlQueryForInterpolation[] {
|
2019-10-08 10:01:20 -05:00
|
|
|
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-01-24 02:50:09 -06:00
|
|
|
rawSql: this.templateSrv.replace(query.rawSql, scopedVars, this.interpolateVariable),
|
2020-05-28 07:28:56 -05:00
|
|
|
rawQuery: true,
|
2019-10-08 10:01:20 -05:00
|
|
|
};
|
|
|
|
return expandedQuery;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return expandedQueries;
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
filterQuery(query: MySQLQuery): boolean {
|
2021-06-17 03:13:57 -05:00
|
|
|
return !query.hide;
|
2021-05-05 09:46:07 -05:00
|
|
|
}
|
2017-03-30 06:46:46 -05:00
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
applyTemplateVariables(target: MySQLQuery, scopedVars: ScopedVars): Record<string, any> {
|
|
|
|
const queryModel = new MySQLQueryModel(target, this.templateSrv, scopedVars);
|
|
|
|
return {
|
|
|
|
refId: target.refId,
|
|
|
|
datasourceId: this.id,
|
|
|
|
rawSql: queryModel.render(this.interpolateVariable as any),
|
|
|
|
format: target.format,
|
|
|
|
};
|
2017-04-21 08:07:43 -05:00
|
|
|
}
|
2017-04-20 10:10:09 -05:00
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
async annotationQuery(options: any): Promise<AnnotationEvent[]> {
|
2017-05-22 15:18:20 -05:00
|
|
|
if (!options.annotation.rawQuery) {
|
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
|
|
|
});
|
2017-05-22 15:18:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
refId: options.annotation.name,
|
|
|
|
datasourceId: this.id,
|
2017-12-21 01:39:31 -06:00
|
|
|
rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
|
2017-12-20 05:33:33 -06:00
|
|
|
format: 'table',
|
2017-05-22 15:18:20 -05:00
|
|
|
};
|
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv()
|
2021-05-05 09:46:07 -05:00
|
|
|
.fetch<BackendDataSourceResponse>({
|
|
|
|
url: '/api/ds/query',
|
2017-12-20 05:33:33 -06:00
|
|
|
method: 'POST',
|
2017-12-19 09:06:54 -06:00
|
|
|
data: {
|
|
|
|
from: options.range.from.valueOf().toString(),
|
|
|
|
to: options.range.to.valueOf().toString(),
|
2017-12-20 05:33:33 -06:00
|
|
|
queries: [query],
|
|
|
|
},
|
2021-05-05 09:46:07 -05:00
|
|
|
requestId: options.annotation.name,
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
2021-05-05 09:46:07 -05:00
|
|
|
.pipe(
|
|
|
|
map(
|
|
|
|
async (res: FetchResponse<BackendDataSourceResponse>) =>
|
|
|
|
await this.responseParser.transformAnnotationResponse(options, res.data)
|
|
|
|
)
|
|
|
|
)
|
2020-09-25 05:46:28 -05:00
|
|
|
.toPromise();
|
2017-05-22 15:18:20 -05:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
metricFindQuery(query: string, optionalOptions: any): Promise<MetricFindValue[]> {
|
2017-12-20 05:33:33 -06:00
|
|
|
let refId = 'tempvar';
|
2017-12-21 01:39:31 -06:00
|
|
|
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
|
2017-08-09 11:47:11 -05:00
|
|
|
refId = optionalOptions.variable.name;
|
|
|
|
}
|
|
|
|
|
2019-11-03 23:43:03 -06:00
|
|
|
const rawSql = this.templateSrv.replace(
|
|
|
|
query,
|
|
|
|
getSearchFilterScopedVar({ query, wildcardChar: '%', options: optionalOptions }),
|
|
|
|
this.interpolateVariable
|
|
|
|
);
|
2019-10-18 04:40:08 -05:00
|
|
|
|
2017-08-09 11:47:11 -05:00
|
|
|
const interpolatedQuery = {
|
|
|
|
refId: refId,
|
|
|
|
datasourceId: this.id,
|
2019-10-18 04:40:08 -05:00
|
|
|
rawSql,
|
2017-12-20 05:33:33 -06:00
|
|
|
format: 'table',
|
2017-08-09 11:47:11 -05:00
|
|
|
};
|
|
|
|
|
2018-08-31 11:24:09 -05:00
|
|
|
const range = this.timeSrv.timeRange();
|
2017-12-07 04:18:36 -06:00
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv()
|
2021-05-05 09:46:07 -05:00
|
|
|
.fetch<BackendDataSourceResponse>({
|
|
|
|
url: '/api/ds/query',
|
2017-12-20 05:33:33 -06:00
|
|
|
method: 'POST',
|
2021-05-05 09:46:07 -05:00
|
|
|
data: {
|
|
|
|
from: range.from.valueOf().toString(),
|
|
|
|
to: range.to.valueOf().toString(),
|
|
|
|
queries: [interpolatedQuery],
|
|
|
|
},
|
|
|
|
requestId: refId,
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
2021-05-05 09:46:07 -05:00
|
|
|
.pipe(
|
|
|
|
map((rsp) => {
|
|
|
|
return this.responseParser.transformMetricFindResponse(rsp);
|
|
|
|
})
|
|
|
|
)
|
2020-09-25 05:46:28 -05:00
|
|
|
.toPromise();
|
2017-08-09 11:47:11 -05:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:46:07 -05:00
|
|
|
testDatasource(): Promise<any> {
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv()
|
2020-09-25 05:46:28 -05:00
|
|
|
.fetch({
|
2021-05-05 09:46:07 -05:00
|
|
|
url: '/api/ds/query',
|
2017-12-20 05:33:33 -06:00
|
|
|
method: 'POST',
|
2017-12-19 09:06:54 -06:00
|
|
|
data: {
|
2017-12-20 05:33:33 -06:00
|
|
|
from: '5m',
|
|
|
|
to: 'now',
|
2017-12-19 09:06:54 -06:00
|
|
|
queries: [
|
|
|
|
{
|
2017-12-20 05:33:33 -06:00
|
|
|
refId: 'A',
|
2017-12-19 09:06:54 -06:00
|
|
|
intervalMs: 1,
|
|
|
|
maxDataPoints: 1,
|
|
|
|
datasourceId: this.id,
|
2017-12-20 05:33:33 -06:00
|
|
|
rawSql: 'SELECT 1',
|
|
|
|
format: 'table',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
2020-09-25 05:46:28 -05:00
|
|
|
.pipe(
|
|
|
|
mapTo({ status: 'success', message: 'Database Connection OK' }),
|
2021-01-20 00:59:48 -06:00
|
|
|
catchError((err) => {
|
2020-09-25 05:46:28 -05:00
|
|
|
console.error(err);
|
|
|
|
if (err.data && err.data.message) {
|
|
|
|
return of({ status: 'error', message: err.data.message });
|
|
|
|
} else {
|
|
|
|
return of({ status: 'error', message: err.status });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.toPromise();
|
2017-05-11 03:50:24 -05:00
|
|
|
}
|
2019-09-24 13:46:07 -05:00
|
|
|
|
|
|
|
targetContainsTemplate(target: any) {
|
|
|
|
let rawSql = '';
|
|
|
|
|
|
|
|
if (target.rawQuery) {
|
|
|
|
rawSql = target.rawSql;
|
|
|
|
} else {
|
2021-05-05 09:46:07 -05:00
|
|
|
const query = new MySQLQueryModel(target);
|
2019-09-24 13:46:07 -05:00
|
|
|
rawSql = query.buildQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
rawSql = rawSql.replace('$__', '');
|
|
|
|
|
|
|
|
return this.templateSrv.variableExists(rawSql);
|
|
|
|
}
|
2017-03-29 13:43:20 -05:00
|
|
|
}
|