mirror of
https://github.com/grafana/grafana.git
synced 2025-01-04 13:17:16 -06:00
TemplateSrv: exposing a function to detect if a target contains a template (#45214)
* exposing a function to check if a target contains variables. * fixed tests. * renamed function * updated betterer result.
This commit is contained in:
parent
21beb88a4f
commit
9a12b3ea00
@ -359,7 +359,7 @@ exports[`no enzyme tests`] = {
|
||||
"public/app/plugins/datasource/loki/configuration/ConfigEditor.test.tsx:1661240493": [
|
||||
[1, 17, 13, "RegExp match", "2409514259"]
|
||||
],
|
||||
"public/app/plugins/datasource/loki/configuration/DebugSection.test.tsx:2317141020": [
|
||||
"public/app/plugins/datasource/loki/configuration/DebugSection.test.tsx:3844880066": [
|
||||
[1, 17, 13, "RegExp match", "2409514259"]
|
||||
],
|
||||
"public/app/plugins/datasource/loki/configuration/DerivedField.test.tsx:1527527456": [
|
||||
|
@ -17,6 +17,11 @@ export interface TemplateSrv {
|
||||
* Replace the values within the target string. See also {@link InterpolateFunction}
|
||||
*/
|
||||
replace(target?: string, scopedVars?: ScopedVars, format?: string | Function): string;
|
||||
|
||||
/**
|
||||
* Checks if a target contains template variables.
|
||||
*/
|
||||
containsTemplate(target?: string): boolean;
|
||||
}
|
||||
|
||||
let singletonInstance: TemplateSrv;
|
||||
|
@ -22,6 +22,9 @@ describe('getFieldLinksForExplore', () => {
|
||||
getVariables() {
|
||||
return [];
|
||||
},
|
||||
containsTemplate() {
|
||||
return false;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -45,4 +45,14 @@ export class TemplateSrvMock implements TemplateSrv {
|
||||
}
|
||||
return match.slice(1).find((match) => match !== undefined);
|
||||
}
|
||||
|
||||
containsTemplate(target: string | undefined): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.regex.lastIndex = 0;
|
||||
const match = this.regex.exec(target);
|
||||
return match !== null;
|
||||
}
|
||||
}
|
||||
|
@ -430,37 +430,37 @@ describe('templateSrv', () => {
|
||||
});
|
||||
|
||||
it('should return true if $test exists', () => {
|
||||
const result = _templateSrv.variableExists('$test');
|
||||
const result = _templateSrv.containsTemplate('$test');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if $test exists in string', () => {
|
||||
const result = _templateSrv.variableExists('something $test something');
|
||||
const result = _templateSrv.containsTemplate('something $test something');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if [[test]] exists in string', () => {
|
||||
const result = _templateSrv.variableExists('something [[test]] something');
|
||||
const result = _templateSrv.containsTemplate('something [[test]] something');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if [[test:csv]] exists in string', () => {
|
||||
const result = _templateSrv.variableExists('something [[test:csv]] something');
|
||||
const result = _templateSrv.containsTemplate('something [[test:csv]] something');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if ${test} exists in string', () => {
|
||||
const result = _templateSrv.variableExists('something ${test} something');
|
||||
const result = _templateSrv.containsTemplate('something ${test} something');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true if ${test:raw} exists in string', () => {
|
||||
const result = _templateSrv.variableExists('something ${test:raw} something');
|
||||
const result = _templateSrv.containsTemplate('something ${test:raw} something');
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return null if there are no variables in string', () => {
|
||||
const result = _templateSrv.variableExists('string without variables');
|
||||
const result = _templateSrv.containsTemplate('string without variables');
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -189,8 +189,11 @@ export class TemplateSrv implements BaseTemplateSrv {
|
||||
return variableName;
|
||||
}
|
||||
|
||||
variableExists(expression: string): boolean {
|
||||
const name = this.getVariableName(expression);
|
||||
containsTemplate(target: string | undefined): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
const name = this.getVariableName(target);
|
||||
const variable = name && this.getVariableAtIndex(name);
|
||||
return variable !== null && variable !== undefined;
|
||||
}
|
||||
|
@ -894,12 +894,12 @@ export class CloudWatchDatasource
|
||||
|
||||
targetContainsTemplate(target: any) {
|
||||
return (
|
||||
this.templateSrv.variableExists(target.region) ||
|
||||
this.templateSrv.variableExists(target.namespace) ||
|
||||
this.templateSrv.variableExists(target.metricName) ||
|
||||
this.templateSrv.variableExists(target.expression!) ||
|
||||
target.logGroupNames?.some((logGroup: string) => this.templateSrv.variableExists(logGroup)) ||
|
||||
find(target.dimensions, (v, k) => this.templateSrv.variableExists(k) || this.templateSrv.variableExists(v))
|
||||
this.templateSrv.containsTemplate(target.region) ||
|
||||
this.templateSrv.containsTemplate(target.namespace) ||
|
||||
this.templateSrv.containsTemplate(target.metricName) ||
|
||||
this.templateSrv.containsTemplate(target.expression!) ||
|
||||
target.logGroupNames?.some((logGroup: string) => this.templateSrv.containsTemplate(logGroup)) ||
|
||||
find(target.dimensions, (v, k) => this.templateSrv.containsTemplate(k) || this.templateSrv.containsTemplate(v))
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -871,19 +871,19 @@ export class ElasticDatasource
|
||||
}
|
||||
|
||||
targetContainsTemplate(target: any) {
|
||||
if (this.templateSrv.variableExists(target.query) || this.templateSrv.variableExists(target.alias)) {
|
||||
if (this.templateSrv.containsTemplate(target.query) || this.templateSrv.containsTemplate(target.alias)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const bucketAgg of target.bucketAggs) {
|
||||
if (this.templateSrv.variableExists(bucketAgg.field) || this.objectContainsTemplate(bucketAgg.settings)) {
|
||||
if (this.templateSrv.containsTemplate(bucketAgg.field) || this.objectContainsTemplate(bucketAgg.settings)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (const metric of target.metrics) {
|
||||
if (
|
||||
this.templateSrv.variableExists(metric.field) ||
|
||||
this.templateSrv.containsTemplate(metric.field) ||
|
||||
this.objectContainsTemplate(metric.settings) ||
|
||||
this.objectContainsTemplate(metric.meta)
|
||||
) {
|
||||
@ -912,7 +912,7 @@ export class ElasticDatasource
|
||||
|
||||
for (const key of Object.keys(obj)) {
|
||||
if (this.isPrimitive(obj[key])) {
|
||||
if (this.templateSrv.variableExists(obj[key])) {
|
||||
if (this.templateSrv.containsTemplate(obj[key])) {
|
||||
return true;
|
||||
}
|
||||
} else if (Array.isArray(obj[key])) {
|
||||
|
@ -140,7 +140,7 @@ export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDa
|
||||
}
|
||||
|
||||
targetContainsTemplate(query: AzureMonitorQuery) {
|
||||
if (query.subscription && this.templateSrv.variableExists(query.subscription)) {
|
||||
if (query.subscription && this.templateSrv.containsTemplate(query.subscription)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -153,7 +153,7 @@ export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDa
|
||||
subQuery = JSON.stringify([query.azureResourceGraph, query.subscriptions]);
|
||||
}
|
||||
|
||||
return !!subQuery && this.templateSrv.variableExists(subQuery);
|
||||
return !!subQuery && this.templateSrv.containsTemplate(subQuery);
|
||||
}
|
||||
|
||||
async annotationQuery(options: any) {
|
||||
|
@ -417,7 +417,7 @@ export class GraphiteDatasource
|
||||
}
|
||||
|
||||
targetContainsTemplate(target: GraphiteQuery) {
|
||||
return this.templateSrv.variableExists(target.target ?? '');
|
||||
return this.templateSrv.containsTemplate(target.target ?? '');
|
||||
}
|
||||
|
||||
translateTime(date: any, roundUp: any, timezone: any) {
|
||||
|
@ -373,7 +373,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
|
||||
// for influxql-mode we use InfluxQueryModel to create the text-representation
|
||||
const queryText = this.isFlux ? target.query : buildRawQuery(target);
|
||||
|
||||
return this.templateSrv.variableExists(queryText);
|
||||
return this.templateSrv.containsTemplate(queryText);
|
||||
}
|
||||
|
||||
interpolateVariablesInQueries(queries: InfluxQuery[], scopedVars: ScopedVars): InfluxQuery[] {
|
||||
|
@ -35,6 +35,9 @@ describe('DebugSection', () => {
|
||||
getVariables() {
|
||||
return [];
|
||||
},
|
||||
containsTemplate() {
|
||||
return false;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -185,6 +185,6 @@ export class MssqlDatasource extends DataSourceWithBackend<MssqlQuery, MssqlOpti
|
||||
|
||||
targetContainsTemplate(query: MssqlQuery): boolean {
|
||||
const rawSql = query.rawSql.replace('$__', '');
|
||||
return this.templateSrv.variableExists(rawSql);
|
||||
return this.templateSrv.containsTemplate(rawSql);
|
||||
}
|
||||
}
|
||||
|
@ -205,6 +205,6 @@ export class MysqlDatasource extends DataSourceWithBackend<MySQLQuery, MySQLOpti
|
||||
|
||||
rawSql = rawSql.replace('$__', '');
|
||||
|
||||
return this.templateSrv.variableExists(rawSql);
|
||||
return this.templateSrv.containsTemplate(rawSql);
|
||||
}
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
|
||||
targetContainsTemplate(target: any) {
|
||||
if (target.filters && target.filters.length > 0) {
|
||||
for (let i = 0; i < target.filters.length; i++) {
|
||||
if (this.templateSrv.variableExists(target.filters[i].filter)) {
|
||||
if (this.templateSrv.containsTemplate(target.filters[i].filter)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -170,7 +170,7 @@ export default class OpenTsDatasource extends DataSourceApi<OpenTsdbQuery, OpenT
|
||||
|
||||
if (target.tags && Object.keys(target.tags).length > 0) {
|
||||
for (const tagKey in target.tags) {
|
||||
if (this.templateSrv.variableExists(target.tags[tagKey])) {
|
||||
if (this.templateSrv.containsTemplate(target.tags[tagKey])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -213,6 +213,6 @@ export class PostgresDatasource extends DataSourceWithBackend<PostgresQuery, Pos
|
||||
|
||||
rawSql = rawSql.replace('$__', '');
|
||||
|
||||
return this.templateSrv.variableExists(rawSql);
|
||||
return this.templateSrv.containsTemplate(rawSql);
|
||||
}
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ export class PrometheusDatasource
|
||||
}
|
||||
|
||||
targetContainsTemplate(target: PromQuery) {
|
||||
return this.templateSrv.variableExists(target.expr);
|
||||
return this.templateSrv.containsTemplate(target.expr);
|
||||
}
|
||||
|
||||
prepareTargets = (options: DataQueryRequest<PromQuery>, start: number, end: number) => {
|
||||
|
@ -164,7 +164,7 @@ export function TemplateSrvStub(this: any) {
|
||||
};
|
||||
this.fillVariableValuesForUrl = () => {};
|
||||
this.updateIndex = () => {};
|
||||
this.variableExists = () => {
|
||||
this.containsTemplate = () => {
|
||||
return false;
|
||||
};
|
||||
this.variableInitialized = () => {};
|
||||
|
Loading…
Reference in New Issue
Block a user