| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import _ from 'lodash';
- import ResponseParser from './response_parser';
- export class MysqlDatasource {
- id: any;
- name: any;
- responseParser: ResponseParser;
- /** @ngInject **/
- constructor(instanceSettings, private backendSrv, private $q, private templateSrv) {
- this.name = instanceSettings.name;
- this.id = instanceSettings.id;
- this.responseParser = new ResponseParser(this.$q);
- }
- interpolateVariable(value, variable) {
- if (typeof value === 'string') {
- if (variable.multi || variable.includeAll) {
- return "'" + value.replace(/'/g, `''`) + "'";
- } else {
- return value;
- }
- }
- if (typeof value === 'number') {
- return value;
- }
- const quotedValues = _.map(value, function(val) {
- if (typeof value === 'number') {
- return value;
- }
- return "'" + val.replace(/'/g, `''`) + "'";
- });
- return quotedValues.join(',');
- }
- query(options) {
- const queries = _.filter(options.targets, item => {
- return item.hide !== true;
- }).map(item => {
- return {
- refId: item.refId,
- intervalMs: options.intervalMs,
- maxDataPoints: options.maxDataPoints,
- datasourceId: this.id,
- rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable),
- format: item.format,
- };
- });
- if (queries.length === 0) {
- return this.$q.when({ data: [] });
- }
- return this.backendSrv
- .datasourceRequest({
- url: '/api/tsdb/query',
- method: 'POST',
- data: {
- from: options.range.from.valueOf().toString(),
- to: options.range.to.valueOf().toString(),
- queries: queries,
- },
- })
- .then(this.responseParser.processQueryResult);
- }
- annotationQuery(options) {
- if (!options.annotation.rawQuery) {
- return this.$q.reject({
- 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),
- format: 'table',
- };
- return this.backendSrv
- .datasourceRequest({
- url: '/api/tsdb/query',
- method: 'POST',
- data: {
- from: options.range.from.valueOf().toString(),
- to: options.range.to.valueOf().toString(),
- queries: [query],
- },
- })
- .then(data => this.responseParser.transformAnnotationResponse(options, data));
- }
- metricFindQuery(query, optionalOptions) {
- 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),
- format: 'table',
- };
- const data = {
- queries: [interpolatedQuery],
- };
- if (optionalOptions && optionalOptions.range && optionalOptions.range.from) {
- data['from'] = optionalOptions.range.from.valueOf().toString();
- }
- if (optionalOptions && optionalOptions.range && optionalOptions.range.to) {
- data['to'] = optionalOptions.range.to.valueOf().toString();
- }
- return this.backendSrv
- .datasourceRequest({
- url: '/api/tsdb/query',
- method: 'POST',
- data: data,
- })
- .then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
- }
- testDatasource() {
- return this.backendSrv
- .datasourceRequest({
- url: '/api/tsdb/query',
- method: 'POST',
- data: {
- from: '5m',
- to: 'now',
- queries: [
- {
- refId: 'A',
- intervalMs: 1,
- maxDataPoints: 1,
- datasourceId: this.id,
- rawSql: 'SELECT 1',
- format: 'table',
- },
- ],
- },
- })
- .then(res => {
- return { status: 'success', message: 'Database Connection OK' };
- })
- .catch(err => {
- console.log(err);
- if (err.data && err.data.message) {
- return { status: 'error', message: err.data.message };
- } else {
- return { status: 'error', message: err.status };
- }
- });
- }
- }
|