annotations: allows template variables to be used in tag filter

When filtering built in annotations by tag, interpolates the tag with
template variables. Fixes #9587
This commit is contained in:
Daniel Lee
2017-12-12 10:07:33 +01:00
parent 5d70a36ad8
commit 3a3272e225
2 changed files with 71 additions and 1 deletions
@@ -3,7 +3,7 @@ import _ from 'lodash';
class GrafanaDatasource {
/** @ngInject */
constructor(private backendSrv, private $q) {}
constructor(private backendSrv, private $q, private templateSrv) {}
query(options) {
return this.backendSrv
@@ -58,6 +58,11 @@ class GrafanaDatasource {
if (!_.isArray(options.annotation.tags) || options.annotation.tags.length === 0) {
return this.$q.when([]);
}
const tags = [];
for (let t of params.tags) {
tags.push(this.templateSrv.replace(t));
}
params.tags = tags;
}
return this.backendSrv.get('/api/annotations', params);
@@ -0,0 +1,65 @@
import {GrafanaDatasource} from "../datasource";
import q from 'q';
import moment from 'moment';
describe('grafana data source', () => {
describe('when executing an annotations query', () => {
let calledBackendSrvParams;
const backendSrvStub = {
get: (url, options) => {
calledBackendSrvParams = options;
return q.resolve([]);
}
};
const templateSrvStub = {
replace: val => val.replace('$var', 'replaced')
};
const ds = new GrafanaDatasource(backendSrvStub, q, templateSrvStub);
describe('with tags that have template variables', () => {
const options = setupAnnotationQueryOptions(
{tags: ['tag1:$var']}
);
beforeEach(() => {
return ds.annotationQuery(options);
});
it('should interpolate template variables in tags in query options', () => {
expect(calledBackendSrvParams.tags[0]).toBe('tag1:replaced');
});
});
describe('with type dashboard', () => {
const options = setupAnnotationQueryOptions(
{
type: 'dashboard',
tags: ['tag1']
},
{id: 1}
);
beforeEach(() => {
return ds.annotationQuery(options);
});
it('should remove tags from query options', () => {
expect(calledBackendSrvParams.tags).toBe(undefined);
});
});
});
});
function setupAnnotationQueryOptions(annotation, dashboard?) {
return {
annotation: annotation,
dashboard: dashboard,
range: {
from: moment(1432288354),
to: moment(1432288401)
},
rangeRaw: {from: "now-24h", to: "now"}
};
}