mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: opentsdb ds: interpolate filter variables in expressions (#71232)
Use the same code path to interpolate both metric variables and filter/tag variables when the data source is accessed either directly or via the expressions datasource. partial fix for #48141 (opentsdb datasource only) Signed-off-by: Uri Okrent <uokrent@gmail.com>
This commit is contained in:
parent
ddc3784213
commit
27a8328d3a
@ -4219,8 +4219,8 @@ exports[`better eslint`] = {
|
|||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "49"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "49"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "50"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "50"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "51"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "51"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "52"],
|
[0, 0, 0, "Do not use any type assertions.", "52"],
|
||||||
[0, 0, 0, "Do not use any type assertions.", "53"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "53"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "54"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "54"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "55"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "55"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "56"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "56"],
|
||||||
|
@ -497,14 +497,7 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const query: any = {
|
const query = this.interpolateVariablesInQuery(target, options.scopedVars);
|
||||||
metric: this.templateSrv.replace(target.metric, options.scopedVars, 'pipe'),
|
|
||||||
aggregator: 'avg',
|
|
||||||
};
|
|
||||||
|
|
||||||
if (target.aggregator) {
|
|
||||||
query.aggregator = this.templateSrv.replace(target.aggregator);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target.shouldComputeRate) {
|
if (target.shouldComputeRate) {
|
||||||
query.rate = true;
|
query.rate = true;
|
||||||
@ -540,22 +533,6 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target.filters && target.filters.length > 0) {
|
|
||||||
query.filters = cloneDeep(target.filters);
|
|
||||||
|
|
||||||
if (query.filters) {
|
|
||||||
this.interpolateVariablesInFilters(query, options);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
query.tags = cloneDeep(target.tags);
|
|
||||||
|
|
||||||
if (query.tags) {
|
|
||||||
for (const tagKey in query.tags) {
|
|
||||||
query.tags[tagKey] = this.templateSrv.replace(query.tags[tagKey], options.scopedVars, 'pipe');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target.explicitTags) {
|
if (target.explicitTags) {
|
||||||
query.explicitTags = true;
|
query.explicitTags = true;
|
||||||
}
|
}
|
||||||
@ -563,11 +540,11 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
|
|||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
interpolateVariablesInFilters(query: OpenTsdbQuery, options: DataQueryRequest<OpenTsdbQuery>) {
|
interpolateVariablesInFilters(query: OpenTsdbQuery, scopedVars: ScopedVars) {
|
||||||
query.filters = query.filters?.map((filter: OpenTsdbFilter): OpenTsdbFilter => {
|
query.filters = query.filters?.map((filter: OpenTsdbFilter): OpenTsdbFilter => {
|
||||||
filter.tagk = this.templateSrv.replace(filter.tagk, options.scopedVars, 'pipe');
|
filter.tagk = this.templateSrv.replace(filter.tagk, scopedVars, 'pipe');
|
||||||
|
|
||||||
filter.filter = this.templateSrv.replace(filter.filter, options.scopedVars, 'pipe');
|
filter.filter = this.templateSrv.replace(filter.filter, scopedVars, 'pipe');
|
||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
});
|
});
|
||||||
@ -602,10 +579,30 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
|
|||||||
return queries;
|
return queries;
|
||||||
}
|
}
|
||||||
|
|
||||||
return queries.map((query) => ({
|
return queries.map((query) => this.interpolateVariablesInQuery(query, scopedVars));
|
||||||
...query,
|
}
|
||||||
metric: this.templateSrv.replace(query.metric, scopedVars),
|
|
||||||
}));
|
interpolateVariablesInQuery(target: OpenTsdbQuery, scopedVars: ScopedVars): any {
|
||||||
|
const query = cloneDeep(target);
|
||||||
|
|
||||||
|
query.metric = this.templateSrv.replace(target.metric, scopedVars, 'pipe');
|
||||||
|
|
||||||
|
query.aggregator = 'avg';
|
||||||
|
if (target.aggregator) {
|
||||||
|
query.aggregator = this.templateSrv.replace(target.aggregator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.filters && query.filters.length > 0) {
|
||||||
|
this.interpolateVariablesInFilters(query, scopedVars);
|
||||||
|
} else {
|
||||||
|
if (query.tags) {
|
||||||
|
for (const tagKey in query.tags) {
|
||||||
|
query.tags[tagKey] = this.templateSrv.replace(query.tags[tagKey], scopedVars, 'pipe');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
convertToTSDBTime(date: any, roundUp: any, timezone: any) {
|
convertToTSDBTime(date: any, roundUp: any, timezone: any) {
|
||||||
|
@ -128,7 +128,7 @@ describe('opentsdb', () => {
|
|||||||
expect(ds.interpolateVariablesInQueries([], {})).toHaveLength(0);
|
expect(ds.interpolateVariablesInQueries([], {})).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should replace metric variable', () => {
|
it('should replace metric and filter variable', () => {
|
||||||
const { ds, templateSrv } = getTestcontext();
|
const { ds, templateSrv } = getTestcontext();
|
||||||
const logQuery: OpenTsdbQuery = {
|
const logQuery: OpenTsdbQuery = {
|
||||||
refId: 'someRefId',
|
refId: 'someRefId',
|
||||||
@ -136,8 +136,8 @@ describe('opentsdb', () => {
|
|||||||
filters: [
|
filters: [
|
||||||
{
|
{
|
||||||
type: 'type',
|
type: 'type',
|
||||||
tagk: 'someTagk',
|
tagk: '$someTagk',
|
||||||
filter: 'someTagv',
|
filter: '$someTagv',
|
||||||
groupBy: true,
|
groupBy: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -145,8 +145,10 @@ describe('opentsdb', () => {
|
|||||||
|
|
||||||
ds.interpolateVariablesInQueries([logQuery], {});
|
ds.interpolateVariablesInQueries([logQuery], {});
|
||||||
|
|
||||||
expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {});
|
expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {}, 'pipe');
|
||||||
expect(templateSrv.replace).toHaveBeenCalledTimes(1);
|
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', {}, 'pipe');
|
||||||
|
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', {}, 'pipe');
|
||||||
|
expect(templateSrv.replace).toHaveBeenCalledTimes(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should replace filter tag key and value', () => {
|
it('should replace filter tag key and value', () => {
|
||||||
@ -210,7 +212,7 @@ describe('opentsdb', () => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
ds.interpolateVariablesInFilters(logQuery, dataQR);
|
ds.interpolateVariablesInFilters(logQuery, dataQR.scopedVars);
|
||||||
|
|
||||||
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', scopedVars, 'pipe');
|
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', scopedVars, 'pipe');
|
||||||
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', scopedVars, 'pipe');
|
expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', scopedVars, 'pipe');
|
||||||
|
Loading…
Reference in New Issue
Block a user