mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Transformations: filter results by refId (#20261)
This commit is contained in:
@@ -4,4 +4,5 @@ export * from './matchers';
|
||||
export * from './transformers';
|
||||
export * from './fieldReducer';
|
||||
export { FilterFieldsByNameTransformerOptions } from './transformers/filterByName';
|
||||
export { FilterFramesByRefIdTransformerOptions } from './transformers/filterByRefId';
|
||||
export { ReduceTransformerOptions } from './transformers/reduce';
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import { DataFrame } from '../../types/dataFrame';
|
||||
import { FrameMatcherID } from './ids';
|
||||
import { FrameMatcherInfo } from '../../types/transformations';
|
||||
import { stringToJsRegex } from '../../text';
|
||||
|
||||
// General Field matcher
|
||||
const refIdMacher: FrameMatcherInfo<string> = {
|
||||
@@ -10,8 +11,9 @@ const refIdMacher: FrameMatcherInfo<string> = {
|
||||
defaultOptions: 'A',
|
||||
|
||||
get: (pattern: string) => {
|
||||
const regex = stringToJsRegex(pattern);
|
||||
return (frame: DataFrame) => {
|
||||
return pattern === frame.refId;
|
||||
return regex.test(frame.refId || '');
|
||||
};
|
||||
},
|
||||
|
||||
|
@@ -8,6 +8,7 @@ import { filterFieldsTransformer, filterFramesTransformer } from './transformers
|
||||
import { filterFieldsByNameTransformer, FilterFieldsByNameTransformerOptions } from './transformers/filterByName';
|
||||
import { noopTransformer } from './transformers/noop';
|
||||
import { DataTransformerInfo, DataTransformerConfig } from '../types/transformations';
|
||||
import { filterFramesByRefIdTransformer } from './transformers/filterByRefId';
|
||||
|
||||
/**
|
||||
* Apply configured transformations to the input data
|
||||
@@ -64,6 +65,7 @@ export const transformersRegistry = new TransformerRegistry(() => [
|
||||
filterFieldsTransformer,
|
||||
filterFieldsByNameTransformer,
|
||||
filterFramesTransformer,
|
||||
filterFramesByRefIdTransformer,
|
||||
appendTransformer,
|
||||
reduceTransformer,
|
||||
]);
|
||||
|
@@ -0,0 +1,44 @@
|
||||
import { DataTransformerID } from './ids';
|
||||
import { transformDataFrame } from '../transformers';
|
||||
import { toDataFrame } from '../../dataframe/processDataFrame';
|
||||
|
||||
export const allSeries = [
|
||||
toDataFrame({
|
||||
refId: 'A',
|
||||
fields: [],
|
||||
}),
|
||||
toDataFrame({
|
||||
refId: 'B',
|
||||
fields: [],
|
||||
}),
|
||||
toDataFrame({
|
||||
refId: 'C',
|
||||
fields: [],
|
||||
}),
|
||||
];
|
||||
|
||||
describe('filterByRefId transformer', () => {
|
||||
it('returns all series if no options provided', () => {
|
||||
const cfg = {
|
||||
id: DataTransformerID.filterByRefId,
|
||||
options: {},
|
||||
};
|
||||
|
||||
const filtered = transformDataFrame([cfg], allSeries);
|
||||
expect(filtered.length).toBe(3);
|
||||
});
|
||||
|
||||
describe('respects', () => {
|
||||
it('inclusion', () => {
|
||||
const cfg = {
|
||||
id: DataTransformerID.filterByRefId,
|
||||
options: {
|
||||
include: 'A|B',
|
||||
},
|
||||
};
|
||||
|
||||
const filtered = transformDataFrame([cfg], allSeries);
|
||||
expect(filtered.map(f => f.refId)).toEqual(['A', 'B']);
|
||||
});
|
||||
});
|
||||
});
|
@@ -0,0 +1,38 @@
|
||||
import { DataTransformerID } from './ids';
|
||||
import { FilterOptions, filterFramesTransformer } from './filter';
|
||||
import { DataTransformerInfo } from '../../types/transformations';
|
||||
import { FrameMatcherID } from '../matchers/ids';
|
||||
|
||||
export interface FilterFramesByRefIdTransformerOptions {
|
||||
include?: string;
|
||||
exclude?: string;
|
||||
}
|
||||
|
||||
export const filterFramesByRefIdTransformer: DataTransformerInfo<FilterFramesByRefIdTransformerOptions> = {
|
||||
id: DataTransformerID.filterByRefId,
|
||||
name: 'Filter data by query refId',
|
||||
description: 'select a subset of results',
|
||||
defaultOptions: {},
|
||||
|
||||
/**
|
||||
* Return a modified copy of the series. If the transform is not or should not
|
||||
* be applied, just return the input series
|
||||
*/
|
||||
transformer: (options: FilterFramesByRefIdTransformerOptions) => {
|
||||
const filterOptions: FilterOptions = {};
|
||||
if (options.include) {
|
||||
filterOptions.include = {
|
||||
id: FrameMatcherID.byRefId,
|
||||
options: options.include,
|
||||
};
|
||||
}
|
||||
if (options.exclude) {
|
||||
filterOptions.exclude = {
|
||||
id: FrameMatcherID.byRefId,
|
||||
options: options.exclude,
|
||||
};
|
||||
}
|
||||
|
||||
return filterFramesTransformer.transformer(filterOptions);
|
||||
},
|
||||
};
|
@@ -7,5 +7,6 @@ export enum DataTransformerID {
|
||||
filterFields = 'filterFields', // Pick some fields (keep all frames)
|
||||
filterFieldsByName = 'filterFieldsByName', // Pick fields with name matching regex (keep all frames)
|
||||
filterFrames = 'filterFrames', // Pick some frames (keep all fields)
|
||||
filterByRefId = 'filterByRefId', // Pick some frames by RefId
|
||||
noop = 'noop', // Does nothing to the dataframe
|
||||
}
|
||||
|
Reference in New Issue
Block a user