mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Dashboard: Support Variables in "Filter by Name" Transformation (#51804)
This commit is contained in:
parent
9b1e1d67cc
commit
46004037e2
@ -22,9 +22,23 @@ export const filterFieldsTransformer: DataTransformerInfo<FilterOptions> = {
|
|||||||
* Return a modified copy of the series. If the transform is not or should not
|
* Return a modified copy of the series. If the transform is not or should not
|
||||||
* be applied, just return the input series
|
* be applied, just return the input series
|
||||||
*/
|
*/
|
||||||
operator: (options: FilterOptions) => (source) => {
|
operator: (options: FilterOptions, replace) => (source) => {
|
||||||
if (!options.include && !options.exclude) {
|
if (!options.include && !options.exclude) {
|
||||||
return source.pipe(noopTransformer.operator({}));
|
return source.pipe(noopTransformer.operator({}, replace));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (replace) {
|
||||||
|
if (typeof options.include?.options === 'string') {
|
||||||
|
options.include.options = replace(options.include?.options);
|
||||||
|
} else if (typeof options.include?.options?.pattern === 'string') {
|
||||||
|
options.include.options.pattern = replace(options.include?.options.pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof options.exclude?.options === 'string') {
|
||||||
|
options.exclude.options = replace(options.exclude?.options);
|
||||||
|
} else if (typeof options.exclude?.options?.pattern === 'string') {
|
||||||
|
options.exclude.options.pattern = replace(options.exclude?.options.pattern);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.pipe(
|
return source.pipe(
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { toDataFrame } from '../../dataframe/processDataFrame';
|
import { toDataFrame } from '../../dataframe/processDataFrame';
|
||||||
|
import { ScopedVars } from '../../types';
|
||||||
import { FieldType } from '../../types/dataFrame';
|
import { FieldType } from '../../types/dataFrame';
|
||||||
import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry';
|
import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry';
|
||||||
import { transformDataFrame } from '../transformDataFrame';
|
import { transformDataFrame } from '../transformDataFrame';
|
||||||
@ -195,5 +196,38 @@ describe('filterByName transformer', () => {
|
|||||||
expect(filtered.fields[0].name).toBe('B');
|
expect(filtered.fields[0].name).toBe('B');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('uses template variable substituion', async () => {
|
||||||
|
const cfg = {
|
||||||
|
id: DataTransformerID.filterFieldsByName,
|
||||||
|
options: {
|
||||||
|
include: {
|
||||||
|
pattern: '/^$var1/',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
replace: (target: string | undefined, scopedVars?: ScopedVars, format?: string | Function): string => {
|
||||||
|
if (!target) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
const variables: ScopedVars = {
|
||||||
|
var1: {
|
||||||
|
value: 'startsWith',
|
||||||
|
text: 'Test',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
for (const key of Object.keys(variables)) {
|
||||||
|
return target.replace(`$${key}`, variables[key].value);
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
await expect(transformDataFrame([cfg], [seriesWithNamesToMatch])).toEmitValuesWith((received) => {
|
||||||
|
const data = received[0];
|
||||||
|
const filtered = data[0];
|
||||||
|
expect(filtered.fields.length).toBe(2);
|
||||||
|
expect(filtered.fields[0].name).toBe('startsWithA');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -20,12 +20,15 @@ export const filterFieldsByNameTransformer: DataTransformerInfo<FilterFieldsByNa
|
|||||||
* Return a modified copy of the series. If the transform is not or should not
|
* Return a modified copy of the series. If the transform is not or should not
|
||||||
* be applied, just return the input series
|
* be applied, just return the input series
|
||||||
*/
|
*/
|
||||||
operator: (options) => (source) =>
|
operator: (options, replace) => (source) =>
|
||||||
source.pipe(
|
source.pipe(
|
||||||
filterFieldsTransformer.operator({
|
filterFieldsTransformer.operator(
|
||||||
include: getMatcherConfig(options.include),
|
{
|
||||||
exclude: getMatcherConfig(options.exclude),
|
include: getMatcherConfig(options.include),
|
||||||
})
|
exclude: getMatcherConfig(options.exclude),
|
||||||
|
},
|
||||||
|
replace
|
||||||
|
)
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user