Skip to content

Commit

Permalink
[Security solution] Fix network map time range (elastic#156193)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic authored May 1, 2023
1 parent 5ef0df5 commit 10bb8e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { createEmbeddable } from './create_embeddable';
import { useSourcererDataView } from '../../../../common/containers/sourcerer';
import { getLayerList } from './map_config';
import { useIsFieldInIndexPattern } from '../../../containers/fields';
import { buildTimeRangeFilter } from '../../../../detections/components/alerts_table/helpers';

jest.mock('./create_embeddable');
jest.mock('./map_config');
Expand Down Expand Up @@ -62,6 +63,7 @@ const packetbeatDataView = { id: '28995490-023d-11eb-bcb6-6ba0578012a9', title:
const mockSelector = {
kibanaDataViews: [filebeatDataView, packetbeatDataView],
};
const mockUpdateInput = jest.fn();
const embeddableValue = {
destroyed: false,
enhancements: { dynamicActions: {} },
Expand All @@ -88,7 +90,7 @@ const embeddableValue = {
setEventHandlers: jest.fn(),
setRenderTooltipContent: jest.fn(),
type: 'map',
updateInput: jest.fn(),
updateInput: mockUpdateInput,
};
const testProps = {
endDate: '2019-08-28T05:50:57.877Z',
Expand Down Expand Up @@ -122,6 +124,20 @@ describe('EmbeddedMapComponent', () => {
});
});

test('calls updateInput with time range filter', async () => {
render(
<TestProviders>
<EmbeddedMapComponent {...testProps} />
</TestProviders>
);
await waitFor(() => {
expect(mockUpdateInput).toHaveBeenCalledTimes(2);
expect(mockUpdateInput).toHaveBeenNthCalledWith(2, {
filters: buildTimeRangeFilter(testProps.startDate, testProps.endDate),
});
});
});

test('renders services.embeddable.EmbeddablePanel', async () => {
const { getByTestId, queryByTestId } = render(
<TestProviders>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { ErrorEmbeddable } from '@kbn/embeddable-plugin/public';
import { isErrorEmbeddable } from '@kbn/embeddable-plugin/public';
import type { MapEmbeddable } from '@kbn/maps-plugin/public/embeddable';
import { isEqual } from 'lodash/fp';
import { buildTimeRangeFilter } from '../../../../detections/components/alerts_table/helpers';
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
import { useIsFieldInIndexPattern } from '../../../containers/fields';
import { Loader } from '../../../../common/components/loader';
Expand Down Expand Up @@ -242,22 +243,19 @@ export const EmbeddedMapComponent = ({
}
}, [embeddable, query]);

useEffect(() => {
if (embeddable != null) {
embeddable.updateInput({ filters });
}
}, [embeddable, filters]);
const timeRangeFilter = useMemo(
() => buildTimeRangeFilter(startDate, endDate),
[startDate, endDate]
);

// DateRange updated useEffect
useEffect(() => {
if (embeddable != null && startDate != null && endDate != null) {
const timeRange = {
from: new Date(startDate).toISOString(),
to: new Date(endDate).toISOString(),
};
embeddable.updateInput({ timeRange });
if (embeddable != null) {
// pass time range as filter instead of via timeRange param
// if user's data view does not have a time field, the timeRange param is not applied
// using filter will always apply the time range
embeddable.updateInput({ filters: [...filters, ...timeRangeFilter] });
}
}, [embeddable, startDate, endDate]);
}, [embeddable, filters, timeRangeFilter]);

const setDefaultMapVisibility = useCallback(
(isOpen: boolean) => {
Expand Down

0 comments on commit 10bb8e9

Please sign in to comment.