Geomap: Fix data filter for layers (#114515)

* Geomap: Fix data filter for layers

* Simplify comments
This commit is contained in:
Drew Slobodnjak
2025-11-26 14:59:21 -08:00
committed by GitHub
parent f116539541
commit a8aef11926
2 changed files with 130 additions and 1 deletions
@@ -0,0 +1,123 @@
jest.mock('ol-mapbox-style', () => ({}));
jest.mock('geotiff', () => ({}));
import BaseLayer from 'ol/layer/Base';
import {
DataFrame,
DataQueryRequest,
FieldType,
LoadingState,
MapLayerHandler,
MapLayerOptions,
PanelData,
TimeRange,
} from '@grafana/data';
import { applyLayerFilter } from './layers';
describe('applyLayerFilter', () => {
const createDataFrame = (refId: string): DataFrame => ({
refId,
fields: [{ name: 'value', type: FieldType.number, values: [1, 2, 3], config: {} }],
length: 3,
});
it('should apply filter when query exists and is visible', () => {
const update = jest.fn();
const handler: MapLayerHandler = {
init: () => ({}) as BaseLayer,
update,
};
const options: MapLayerOptions = {
name: 'Test',
type: 'markers',
filterData: { id: 'byRefId', options: 'A' },
};
const panelData: PanelData = {
series: [createDataFrame('A'), createDataFrame('B')],
state: LoadingState.Done,
timeRange: {} as TimeRange,
request: { targets: [{ refId: 'A' }, { refId: 'B' }] } as DataQueryRequest,
};
applyLayerFilter(handler, options, panelData);
expect(update).toHaveBeenCalledWith(
expect.objectContaining({
series: [createDataFrame('A')],
})
);
});
it('should return empty series when query exists but is hidden', () => {
const update = jest.fn();
const handler: MapLayerHandler = {
init: () => ({}) as BaseLayer,
update,
};
const options: MapLayerOptions = {
name: 'Test',
type: 'markers',
filterData: { id: 'byRefId', options: 'A' },
};
const panelData: PanelData = {
series: [createDataFrame('B')],
state: LoadingState.Done,
timeRange: {} as TimeRange,
request: { targets: [{ refId: 'A', hide: true }, { refId: 'B' }] } as DataQueryRequest,
};
applyLayerFilter(handler, options, panelData);
expect(update).toHaveBeenCalledWith(
expect.objectContaining({
series: [],
})
);
});
it('should not apply filter when query does not exist', () => {
const update = jest.fn();
const handler: MapLayerHandler = {
init: () => ({}) as BaseLayer,
update,
};
const options: MapLayerOptions = {
name: 'Test',
type: 'markers',
filterData: { id: 'byRefId', options: 'C' },
};
const panelData: PanelData = {
series: [createDataFrame('A'), createDataFrame('B')],
state: LoadingState.Done,
timeRange: {} as TimeRange,
request: { targets: [{ refId: 'A' }, { refId: 'B' }] } as DataQueryRequest,
};
applyLayerFilter(handler, options, panelData);
expect(update).toHaveBeenCalledWith(panelData);
});
it('should pass through all data when no filter is configured', () => {
const update = jest.fn();
const handler: MapLayerHandler = {
init: () => ({}) as BaseLayer,
update,
};
const options: MapLayerOptions = {
name: 'Test',
type: 'markers',
};
const panelData: PanelData = {
series: [createDataFrame('A'), createDataFrame('B')],
state: LoadingState.Done,
timeRange: {} as TimeRange,
};
applyLayerFilter(handler, options, panelData);
expect(update).toHaveBeenCalledWith(panelData);
});
});
@@ -26,7 +26,13 @@ export const applyLayerFilter = (
let panelData = panelDataProps;
if (options.filterData) {
const matcherFunc = getFrameMatchers(options.filterData);
if (panelData.series.some(matcherFunc)) {
const queryExists = panelData.request?.targets.some((target) => {
return target.refId === options.filterData?.options;
});
// Only apply filter if the target query exists
if (queryExists) {
panelData = {
...panelData,
series: panelData.series.filter(matcherFunc),