mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #15215 from thatsparesh/13324-mssql-pass-timerange-for-template-variable-queries
mssql: pass timerange for template variable queries
This commit is contained in:
commit
bd48408e2d
@ -8,7 +8,7 @@ export class MssqlDatasource {
|
|||||||
interval: string;
|
interval: string;
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor(instanceSettings, private backendSrv, private $q, private templateSrv) {
|
constructor(instanceSettings, private backendSrv, private $q, private templateSrv, private timeSrv) {
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
this.id = instanceSettings.id;
|
this.id = instanceSettings.id;
|
||||||
this.responseParser = new ResponseParser(this.$q);
|
this.responseParser = new ResponseParser(this.$q);
|
||||||
@ -107,13 +107,18 @@ export class MssqlDatasource {
|
|||||||
format: 'table',
|
format: 'table',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const range = this.timeSrv.timeRange();
|
||||||
|
const data = {
|
||||||
|
queries: [interpolatedQuery],
|
||||||
|
from: range.from.valueOf().toString(),
|
||||||
|
to: range.to.valueOf().toString(),
|
||||||
|
};
|
||||||
|
|
||||||
return this.backendSrv
|
return this.backendSrv
|
||||||
.datasourceRequest({
|
.datasourceRequest({
|
||||||
url: '/api/tsdb/query',
|
url: '/api/tsdb/query',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: data,
|
||||||
queries: [interpolatedQuery],
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
.then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
|
.then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { MssqlDatasource } from '../datasource';
|
import { MssqlDatasource } from '../datasource';
|
||||||
import { TemplateSrvStub } from 'test/specs/helpers';
|
import { TemplateSrvStub, TimeSrvStub } from 'test/specs/helpers';
|
||||||
import { CustomVariable } from 'app/features/templating/custom_variable';
|
import { CustomVariable } from 'app/features/templating/custom_variable';
|
||||||
import q from 'q';
|
import q from 'q';
|
||||||
|
|
||||||
@ -8,13 +8,14 @@ describe('MSSQLDatasource', () => {
|
|||||||
const ctx: any = {
|
const ctx: any = {
|
||||||
backendSrv: {},
|
backendSrv: {},
|
||||||
templateSrv: new TemplateSrvStub(),
|
templateSrv: new TemplateSrvStub(),
|
||||||
|
timeSrv: new TimeSrvStub(),
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.$q = q;
|
ctx.$q = q;
|
||||||
ctx.instanceSettings = { name: 'mssql' };
|
ctx.instanceSettings = { name: 'mssql' };
|
||||||
|
|
||||||
ctx.ds = new MssqlDatasource(ctx.instanceSettings, ctx.backendSrv, ctx.$q, ctx.templateSrv);
|
ctx.ds = new MssqlDatasource(ctx.instanceSettings, ctx.backendSrv, ctx.$q, ctx.templateSrv, ctx.timeSrv);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When performing annotationQuery', () => {
|
describe('When performing annotationQuery', () => {
|
||||||
@ -188,6 +189,49 @@ describe('MSSQLDatasource', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('When performing metricFindQuery', () => {
|
||||||
|
let results;
|
||||||
|
const query = 'select * from atable';
|
||||||
|
const response = {
|
||||||
|
results: {
|
||||||
|
tempvar: {
|
||||||
|
meta: {
|
||||||
|
rowCount: 1,
|
||||||
|
},
|
||||||
|
refId: 'tempvar',
|
||||||
|
tables: [
|
||||||
|
{
|
||||||
|
columns: [{ text: 'title' }],
|
||||||
|
rows: [['aTitle']],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const time = {
|
||||||
|
from: moment(1521545610656),
|
||||||
|
to: moment(1521546251185)
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
ctx.timeSrv.setTime(time);
|
||||||
|
|
||||||
|
ctx.backendSrv.datasourceRequest = options => {
|
||||||
|
results = options.data;
|
||||||
|
return ctx.$q.when({ data: response, status: 200 });
|
||||||
|
};
|
||||||
|
|
||||||
|
return ctx.ds.metricFindQuery(query);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass timerange to datasourceRequest', () => {
|
||||||
|
expect(results.from).toBe(time.from.valueOf().toString());
|
||||||
|
expect(results.to).toBe(time.to.valueOf().toString());
|
||||||
|
expect(results.queries.length).toBe(1);
|
||||||
|
expect(results.queries[0].rawSql).toBe(query);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('When interpolating variables', () => {
|
describe('When interpolating variables', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.variable = new CustomVariable({}, {});
|
ctx.variable = new CustomVariable({}, {});
|
||||||
|
@ -143,7 +143,7 @@ export function DashboardViewStateStub(this: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function TimeSrvStub(this: any) {
|
export function TimeSrvStub(this: any) {
|
||||||
this.init = sinon.spy();
|
this.init = () => {};
|
||||||
this.time = { from: 'now-1h', to: 'now' };
|
this.time = { from: 'now-1h', to: 'now' };
|
||||||
this.timeRange = function(parse) {
|
this.timeRange = function(parse) {
|
||||||
if (parse === false) {
|
if (parse === false) {
|
||||||
|
Loading…
Reference in New Issue
Block a user