grafana/public/app/plugins/datasource/mysql/datasource.ts

169 lines
4.6 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import _ from 'lodash';
import ResponseParser from './response_parser';
2018-08-31 08:40:58 -05:00
import MysqlQuery from 'app/plugins/datasource/mysql/mysql_query';
2017-03-29 13:43:20 -05:00
export class MysqlDatasource {
2017-03-29 15:54:07 -05:00
id: any;
name: any;
responseParser: ResponseParser;
2018-08-31 08:40:58 -05:00
queryModel: MysqlQuery;
interval: string;
2017-03-29 13:43:20 -05:00
2018-10-18 13:01:40 -05:00
/** @ngInject */
2018-08-31 11:24:09 -05:00
constructor(instanceSettings, private backendSrv, private $q, private templateSrv, private timeSrv) {
2017-03-29 15:54:07 -05:00
this.name = instanceSettings.name;
this.id = instanceSettings.id;
this.responseParser = new ResponseParser(this.$q);
2018-08-31 08:40:58 -05:00
this.queryModel = new MysqlQuery({});
this.interval = (instanceSettings.jsonData || {}).timeInterval;
2017-03-29 13:43:20 -05:00
}
interpolateVariable = (value, variable) => {
2017-12-20 05:33:33 -06:00
if (typeof value === 'string') {
if (variable.multi || variable.includeAll) {
return this.queryModel.quoteLiteral(value);
} else {
return value;
}
2017-04-23 07:22:47 -05:00
}
2017-12-20 05:33:33 -06:00
if (typeof value === 'number') {
return value;
}
const quotedValues = _.map(value, v => {
return this.queryModel.quoteLiteral(v);
2017-04-23 07:22:47 -05:00
});
2017-12-20 05:33:33 -06:00
return quotedValues.join(',');
};
2017-04-23 07:22:47 -05:00
2017-03-29 13:43:20 -05:00
query(options) {
2018-08-31 08:40:58 -05:00
const queries = _.filter(options.targets, target => {
return target.hide !== true;
}).map(target => {
2018-10-18 13:01:40 -05:00
const queryModel = new MysqlQuery(target, this.templateSrv, options.scopedVars);
2018-08-31 08:40:58 -05:00
return {
2018-08-31 08:40:58 -05:00
refId: target.refId,
intervalMs: options.intervalMs,
maxDataPoints: options.maxDataPoints,
datasourceId: this.id,
2018-08-31 08:40:58 -05:00
rawSql: queryModel.render(this.interpolateVariable),
format: target.format,
};
});
if (queries.length === 0) {
return this.$q.when({ data: [] });
}
return this.backendSrv
.datasourceRequest({
2017-12-20 05:33:33 -06:00
url: '/api/tsdb/query',
method: 'POST',
data: {
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
2017-12-20 05:33:33 -06:00
queries: queries,
},
})
.then(this.responseParser.processQueryResult);
}
2017-04-20 10:10:09 -05:00
annotationQuery(options) {
if (!options.annotation.rawQuery) {
return this.$q.reject({
2017-12-20 05:33:33 -06:00
message: 'Query missing in annotation definition',
});
}
const query = {
refId: options.annotation.name,
datasourceId: this.id,
rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
2017-12-20 05:33:33 -06:00
format: 'table',
};
return this.backendSrv
.datasourceRequest({
2017-12-20 05:33:33 -06:00
url: '/api/tsdb/query',
method: 'POST',
data: {
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
2017-12-20 05:33:33 -06:00
queries: [query],
},
})
.then(data => this.responseParser.transformAnnotationResponse(options, data));
}
metricFindQuery(query, optionalOptions) {
2017-12-20 05:33:33 -06:00
let refId = 'tempvar';
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
refId = optionalOptions.variable.name;
}
const interpolatedQuery = {
refId: refId,
datasourceId: this.id,
rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable),
2017-12-20 05:33:33 -06:00
format: 'table',
};
2018-08-31 11:24:09 -05:00
const range = this.timeSrv.timeRange();
const data = {
2017-12-20 05:33:33 -06:00
queries: [interpolatedQuery],
2018-08-31 11:24:09 -05:00
from: range.from.valueOf().toString(),
to: range.to.valueOf().toString(),
};
if (optionalOptions && optionalOptions.range && optionalOptions.range.from) {
2017-12-20 05:33:33 -06:00
data['from'] = optionalOptions.range.from.valueOf().toString();
}
if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
2017-12-20 05:33:33 -06:00
data['to'] = optionalOptions.range.to.valueOf().toString();
}
return this.backendSrv
.datasourceRequest({
2017-12-20 05:33:33 -06:00
url: '/api/tsdb/query',
method: 'POST',
data: data,
})
.then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
}
testDatasource() {
return this.backendSrv
.datasourceRequest({
2017-12-20 05:33:33 -06:00
url: '/api/tsdb/query',
method: 'POST',
data: {
2017-12-20 05:33:33 -06:00
from: '5m',
to: 'now',
queries: [
{
2017-12-20 05:33:33 -06:00
refId: 'A',
intervalMs: 1,
maxDataPoints: 1,
datasourceId: this.id,
2017-12-20 05:33:33 -06:00
rawSql: 'SELECT 1',
format: 'table',
},
],
},
})
.then(res => {
2017-12-20 05:33:33 -06:00
return { status: 'success', message: 'Database Connection OK' };
})
.catch(err => {
console.log(err);
if (err.data && err.data.message) {
2017-12-20 05:33:33 -06:00
return { status: 'error', message: err.data.message };
} else {
2017-12-20 05:33:33 -06:00
return { status: 'error', message: err.status };
}
});
}
2017-03-29 13:43:20 -05:00
}