Commit 2acfbdb7 by Hugo Häggmark Committed by GitHub

Annotations: Fixes this.templateSrv.replace is not a function error for Grafana datasource (#21778)

parent be09722d
import _ from 'lodash'; import _ from 'lodash';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { getBackendSrv } from '@grafana/runtime'; import { getBackendSrv } from '@grafana/runtime';
import { DataSourceApi, DataSourceInstanceSettings } from '@grafana/data';
class GrafanaDatasource { import templateSrv from 'app/features/templating/template_srv';
class GrafanaDatasource extends DataSourceApi<any> {
/** @ngInject */ /** @ngInject */
constructor(private templateSrv: TemplateSrv) {} constructor(instanceSettings: DataSourceInstanceSettings) {
super(instanceSettings);
}
query(options: any) { query(options: any) {
return getBackendSrv() return getBackendSrv()
...@@ -33,7 +37,7 @@ class GrafanaDatasource { ...@@ -33,7 +37,7 @@ class GrafanaDatasource {
} }
metricFindQuery(options: any) { metricFindQuery(options: any) {
return Promise.resolve({ data: [] }); return Promise.resolve([]);
} }
annotationQuery(options: any) { annotationQuery(options: any) {
...@@ -62,7 +66,7 @@ class GrafanaDatasource { ...@@ -62,7 +66,7 @@ class GrafanaDatasource {
const delimiter = '__delimiter__'; const delimiter = '__delimiter__';
const tags = []; const tags = [];
for (const t of params.tags) { for (const t of params.tags) {
const renderedValues = this.templateSrv.replace(t, {}, (value: any) => { const renderedValues = templateSrv.replace(t, {}, (value: any) => {
if (typeof value === 'string') { if (typeof value === 'string') {
return value; return value;
} }
...@@ -78,6 +82,10 @@ class GrafanaDatasource { ...@@ -78,6 +82,10 @@ class GrafanaDatasource {
return getBackendSrv().get('/api/annotations', params); return getBackendSrv().get('/api/annotations', params);
} }
testDatasource() {
return Promise.resolve();
}
} }
export { GrafanaDatasource }; export { GrafanaDatasource };
import { dateTime } from '@grafana/data'; import { DataSourceInstanceSettings, dateTime } from '@grafana/data';
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__ import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
import { GrafanaDatasource } from '../datasource'; import { GrafanaDatasource } from '../datasource';
...@@ -7,6 +7,12 @@ jest.mock('@grafana/runtime', () => ({ ...@@ -7,6 +7,12 @@ jest.mock('@grafana/runtime', () => ({
getBackendSrv: () => backendSrv, getBackendSrv: () => backendSrv,
})); }));
jest.mock('app/features/templating/template_srv', () => ({
replace: (val: string) => {
return val.replace('$var2', 'replaced__delimiter__replaced2').replace('$var', 'replaced');
},
}));
describe('grafana data source', () => { describe('grafana data source', () => {
const getMock = jest.spyOn(backendSrv, 'get'); const getMock = jest.spyOn(backendSrv, 'get');
...@@ -16,7 +22,6 @@ describe('grafana data source', () => { ...@@ -16,7 +22,6 @@ describe('grafana data source', () => {
describe('when executing an annotations query', () => { describe('when executing an annotations query', () => {
let calledBackendSrvParams: any; let calledBackendSrvParams: any;
let templateSrvStub: any;
let ds: GrafanaDatasource; let ds: GrafanaDatasource;
beforeEach(() => { beforeEach(() => {
getMock.mockImplementation((url: string, options: any) => { getMock.mockImplementation((url: string, options: any) => {
...@@ -24,13 +29,7 @@ describe('grafana data source', () => { ...@@ -24,13 +29,7 @@ describe('grafana data source', () => {
return Promise.resolve([]); return Promise.resolve([]);
}); });
templateSrvStub = { ds = new GrafanaDatasource({} as DataSourceInstanceSettings);
replace: (val: string) => {
return val.replace('$var2', 'replaced__delimiter__replaced2').replace('$var', 'replaced');
},
};
ds = new GrafanaDatasource(templateSrvStub as any);
}); });
describe('with tags that have template variables', () => { describe('with tags that have template variables', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment