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