Files
grafana/public/app/plugins/datasource/mssql/datasource.ts
T

197 lines
5.5 KiB
TypeScript
Raw Normal View History

2017-12-02 14:40:12 +03:00
import _ from 'lodash';
import { Observable, of } from 'rxjs';
import { catchError, map, mapTo } from 'rxjs/operators';
import { getBackendSrv } from '@grafana/runtime';
import { ScopedVars } from '@grafana/data';
import ResponseParser, { MssqlResponse } from './response_parser';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { MssqlQueryForInterpolation } from './types';
2017-12-02 14:40:12 +03:00
export class MssqlDatasource {
id: any;
name: any;
responseParser: ResponseParser;
2018-09-05 12:02:57 +02:00
interval: string;
2017-12-02 14:40:12 +03:00
constructor(
instanceSettings: any,
private readonly templateSrv: TemplateSrv = getTemplateSrv(),
private readonly timeSrv: TimeSrv = getTimeSrv()
) {
2017-12-02 14:40:12 +03:00
this.name = instanceSettings.name;
this.id = instanceSettings.id;
this.responseParser = new ResponseParser();
this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
2017-12-02 14:40:12 +03:00
}
interpolateVariable(value: any, variable: any) {
2017-12-02 14:40:12 +03:00
if (typeof value === 'string') {
if (variable.multi || variable.includeAll) {
return "'" + value.replace(/'/g, `''`) + "'";
2017-12-02 14:40:12 +03:00
} else {
return value;
}
}
if (typeof value === 'number') {
return value;
}
const quotedValues = _.map(value, val => {
2017-12-02 14:40:12 +03:00
if (typeof value === 'number') {
return value;
}
return "'" + val.replace(/'/g, `''`) + "'";
2017-12-02 14:40:12 +03:00
});
2018-03-13 16:03:02 +01:00
return quotedValues.join(',');
2017-12-02 14:40:12 +03:00
}
interpolateVariablesInQueries(
queries: MssqlQueryForInterpolation[],
scopedVars: ScopedVars
): MssqlQueryForInterpolation[] {
let expandedQueries = queries;
if (queries && queries.length > 0) {
expandedQueries = queries.map(query => {
const expandedQuery = {
...query,
datasource: this.name,
rawSql: this.templateSrv.replace(query.rawSql, scopedVars, this.interpolateVariable),
rawQuery: true,
};
return expandedQuery;
});
}
return expandedQueries;
}
query(options: any): Observable<MssqlResponse> {
2018-08-29 14:27:29 +02:00
const queries = _.filter(options.targets, item => {
2017-12-02 14:40:12 +03:00
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 of({ data: [] });
2017-12-02 14:40:12 +03:00
}
return getBackendSrv()
.fetch({
2018-03-13 16:03:02 +01:00
url: '/api/tsdb/query',
method: 'POST',
data: {
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
queries: queries,
},
})
.pipe(map(this.responseParser.processQueryResult));
2017-12-02 14:40:12 +03:00
}
annotationQuery(options: any) {
2017-12-02 14:40:12 +03:00
if (!options.annotation.rawQuery) {
return Promise.reject({ message: 'Query missing in annotation definition' });
2017-12-02 14:40:12 +03:00
}
const query = {
refId: options.annotation.name,
datasourceId: this.id,
rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable),
format: 'table',
};
return getBackendSrv()
.fetch({
2018-03-13 16:03:02 +01:00
url: '/api/tsdb/query',
method: 'POST',
data: {
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
queries: [query],
},
})
.pipe(map((data: any) => this.responseParser.transformAnnotationResponse(options, data)))
.toPromise();
2017-12-02 14:40:12 +03:00
}
metricFindQuery(query: string, optionalOptions: { variable: { name: string } }) {
2017-12-02 14:40:12 +03: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),
format: 'table',
};
2019-02-09 14:57:20 -06:00
const range = this.timeSrv.timeRange();
const data = {
queries: [interpolatedQuery],
2019-02-09 14:57:20 -06:00
from: range.from.valueOf().toString(),
to: range.to.valueOf().toString(),
};
return getBackendSrv()
.fetch({
2018-03-13 16:03:02 +01:00
url: '/api/tsdb/query',
method: 'POST',
data: data,
2018-03-13 16:03:02 +01:00
})
.pipe(map((data: any) => this.responseParser.parseMetricFindQueryResult(refId, data)))
.toPromise();
2017-12-02 14:40:12 +03:00
}
testDatasource() {
return getBackendSrv()
.fetch({
2018-03-13 16:03:02 +01:00
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',
},
],
},
})
.pipe(
mapTo({ status: 'success', message: 'Database Connection OK' }),
catchError(err => {
console.error(err);
if (err.data && err.data.message) {
return of({ status: 'error', message: err.data.message });
}
return of({ status: 'error', message: err.status });
})
)
.toPromise();
2017-12-02 14:40:12 +03:00
}
targetContainsTemplate(target: any) {
const rawSql = target.rawSql.replace('$__', '');
return this.templateSrv.variableExists(rawSql);
}
2017-12-02 14:40:12 +03:00
}