Templating: Makes so __searchFilter can be used as part of regular expression (#20103)

* Fix: Fixes searchfilter wildcard char in regular expressions
Fixes: #20006

* Refactor: Uses TemplateSrv and ScopedVars instead

* Docs: Updates docs with new format

* Tests: Corrects test description
This commit is contained in:
Hugo Häggmark
2019-11-04 06:43:03 +01:00
committed by GitHub
parent fc5cc4cbf9
commit 7aeed4d987
12 changed files with 224 additions and 132 deletions

View File

@@ -7,7 +7,7 @@ import { TemplateSrv } from 'app/features/templating/template_srv';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
//Types
import { PostgresQueryForInterpolation } from './types';
import { interpolateSearchFilter } from '../../../features/templating/variable';
import { getSearchFilterScopedVar } from '../../../features/templating/variable';
export class PostgresDatasource {
id: any;
@@ -127,18 +127,17 @@ export class PostgresDatasource {
.then((data: any) => this.responseParser.transformAnnotationResponse(options, data));
}
metricFindQuery(query: string, optionalOptions: { variable?: any }) {
metricFindQuery(query: string, optionalOptions: { variable?: any; searchFilter?: string }) {
let refId = 'tempvar';
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
refId = optionalOptions.variable.name;
}
const rawSql = interpolateSearchFilter({
query: this.templateSrv.replace(query, {}, this.interpolateVariable),
options: optionalOptions,
wildcardChar: '%',
quoteLiteral: true,
});
const rawSql = this.templateSrv.replace(
query,
getSearchFilterScopedVar({ query, wildcardChar: '%', options: optionalOptions }),
this.interpolateVariable
);
const interpolatedQuery = {
refId: refId,

View File

@@ -131,7 +131,7 @@ describe('PostgreSQLDatasource', () => {
describe('When performing metricFindQuery with $__searchFilter and a searchFilter is given', () => {
let results: any;
let calledWith: any = {};
const query = 'select title from atable where title LIKE $__searchFilter';
const query = "select title from atable where title LIKE '$__searchFilter'";
const response = {
results: {
tempvar: {
@@ -169,7 +169,7 @@ describe('PostgreSQLDatasource', () => {
describe('When performing metricFindQuery with $__searchFilter but no searchFilter is given', () => {
let results: any;
let calledWith: any = {};
const query = 'select title from atable where title LIKE $__searchFilter';
const query = "select title from atable where title LIKE '$__searchFilter'";
const response = {
results: {
tempvar: {