mirror of
https://github.com/grafana/grafana.git
synced 2025-08-26 09:31:45 +08:00
Transformations: Fix filterByValue being applied to the wrong series (#90512)
This commit is contained in:
@ -46,11 +46,16 @@ const multiSeriesWithSingleField = [
|
||||
}),
|
||||
];
|
||||
|
||||
let spyConsoleWarn: jest.SpyInstance;
|
||||
describe('FilterByValue transformer', () => {
|
||||
beforeAll(() => {
|
||||
mockTransformationsRegistry([filterByValueTransformer]);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
spyConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it('should exclude values', async () => {
|
||||
const lower: MatcherConfig<BasicValueMatcherOptions<number>> = {
|
||||
id: ValueMatcherID.lower,
|
||||
@ -92,7 +97,7 @@ describe('FilterByValue transformer', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should not cross frame boundaries', async () => {
|
||||
it('should not cross frame boundaries when equals 0', async () => {
|
||||
const cfg: DataTransformerConfig<FilterByValueTransformerOptions> = {
|
||||
id: DataTransformerID.filterByValue,
|
||||
options: {
|
||||
@ -151,6 +156,66 @@ describe('FilterByValue transformer', () => {
|
||||
state: {},
|
||||
},
|
||||
]);
|
||||
|
||||
expect(console.warn).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
spyConsoleWarn.mockRestore();
|
||||
});
|
||||
|
||||
it('should not cross frame boundaries', async () => {
|
||||
const cfg: DataTransformerConfig<FilterByValueTransformerOptions> = {
|
||||
id: DataTransformerID.filterByValue,
|
||||
options: {
|
||||
type: FilterByValueType.exclude,
|
||||
match: FilterByValueMatch.any,
|
||||
filters: [
|
||||
{
|
||||
fieldName: 'A value',
|
||||
config: {
|
||||
id: ValueMatcherID.greater,
|
||||
options: { value: 0 },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
await expect(transformDataFrame([cfg], multiSeriesWithSingleField)).toEmitValuesWith((received) => {
|
||||
const processed = received[0];
|
||||
expect(processed.length).toEqual(2);
|
||||
|
||||
expect(processed[0].fields).toEqual([
|
||||
{
|
||||
name: 'time',
|
||||
type: FieldType.time,
|
||||
values: [2000],
|
||||
state: {},
|
||||
},
|
||||
{
|
||||
name: 'value',
|
||||
type: FieldType.number,
|
||||
values: [0],
|
||||
state: {},
|
||||
},
|
||||
]);
|
||||
|
||||
expect(processed[1].fields).toEqual([
|
||||
{
|
||||
name: 'time',
|
||||
type: FieldType.time,
|
||||
values: [5000, 6000, 7000],
|
||||
state: {},
|
||||
},
|
||||
{
|
||||
name: 'value',
|
||||
type: FieldType.number,
|
||||
values: [0, 1, 1],
|
||||
state: {},
|
||||
},
|
||||
]);
|
||||
|
||||
expect(console.warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -104,10 +104,9 @@ export const filterByValueTransformer: DataTransformerInfo<FilterByValueTransfor
|
||||
|
||||
const processed: DataFrame[] = [];
|
||||
|
||||
const fieldIndexByName = groupFieldIndexByName(data);
|
||||
|
||||
for (const frame of data) {
|
||||
const rows = new Set<number>();
|
||||
const fieldIndexByName = groupFieldIndexByName(frame, data);
|
||||
|
||||
let matchers;
|
||||
if (transformationsVariableSupport()) {
|
||||
@ -202,15 +201,13 @@ const createFilterValueMatchers = (
|
||||
});
|
||||
};
|
||||
|
||||
const groupFieldIndexByName = (data: DataFrame[]) => {
|
||||
const groupFieldIndexByName = (frame: DataFrame, data: DataFrame[]) => {
|
||||
const lookup: Record<string, number> = {};
|
||||
|
||||
for (const frame of data) {
|
||||
frame.fields.forEach((field, fieldIndex) => {
|
||||
const fieldName = getFieldDisplayName(field, frame, data);
|
||||
lookup[fieldName] = fieldIndex;
|
||||
});
|
||||
}
|
||||
frame.fields.forEach((field, fieldIndex) => {
|
||||
const fieldName = getFieldDisplayName(field, frame, data);
|
||||
lookup[fieldName] = fieldIndex;
|
||||
});
|
||||
|
||||
return lookup;
|
||||
};
|
||||
|
Reference in New Issue
Block a user