Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SIEM] Fix unnecessary re-renders on the Overview page #56587

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test_utils/*": [
"src/test_utils/public/*"
],
"fixtures/*": ["src/fixtures/*"]
"fixtures/*": ["src/fixtures/*"],
"fast-deep-equal/*": ["typings/fast_deep_equal.d.ts"]
},
// Support .tsx files and transform JSX into calls to React.createElement
"jsx": "react",
Expand Down
26 changes: 26 additions & 0 deletions typings/fast_deep_equal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
TODO: Remove after https://github.com/epoberezkin/fast-deep-equal/pull/48
is merged and fast-deep-equal version is bumped
*/

declare const equal: (a: any, b: any) => boolean;
export = equal;
Copy link
Contributor

@FrankHassanabad FrankHassanabad Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment where this came from.

Looks like it is from one of these files?

epoberezkin/fast-deep-equal@f0e3dc1

and why we have to have it here and cannot use regular TypeScript without additional "cruft" and duplication for the next maintainer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see this above:

/*
  TODO: Remove after https://github.com/epoberezkin/fast-deep-equal/pull/48
  is merged and fast-deep-equal version is bumped
*/

I think you are good!

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,4 @@ export const AreaChartComponent: React.FC<AreaChartComponentProps> = ({ areaChar
);
};

AreaChartComponent.displayName = 'AreaChartComponent';

export const AreaChart = React.memo(AreaChartComponent);

AreaChart.displayName = 'AreaChart';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';

import { BarChartBaseComponent, BarChartComponent } from './barchart';
import { ChartSeriesData } from './common';
import { BarSeries, ScaleType, Axis } from '@elastic/charts';
import { Chart, BarSeries, Axis, ScaleType } from '@elastic/charts';

jest.mock('../../lib/kibana');

Expand Down Expand Up @@ -139,7 +139,7 @@ describe('BarChartBaseComponent', () => {
});

it('should render two bar series', () => {
expect(shallowWrapper.find('Chart')).toHaveLength(1);
expect(shallowWrapper.find(Chart)).toHaveLength(1);
});
});

Expand Down Expand Up @@ -167,7 +167,6 @@ describe('BarChartBaseComponent', () => {
});

it(`should ${mockBarChartData.length} render BarSeries`, () => {
expect(shallow).toMatchSnapshot();
expect(shallowWrapper.find(BarSeries)).toHaveLength(mockBarChartData.length);
});

Expand Down Expand Up @@ -265,7 +264,7 @@ describe('BarChartBaseComponent', () => {
});

it('should not render without height and width', () => {
expect(shallowWrapper.find('Chart')).toHaveLength(0);
expect(shallowWrapper.find(Chart)).toHaveLength(0);
});
});
});
Expand Down
11 changes: 8 additions & 3 deletions x-pack/legacy/plugins/siem/public/components/charts/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
TickFormatter,
Position,
} from '@elastic/charts';
import React, { useMemo } from 'react';
import styled from 'styled-components';

import { useUiSetting } from '../../lib/kibana';
import { DEFAULT_DARK_MODE } from '../../../common/constants';

Expand Down Expand Up @@ -54,7 +56,7 @@ export interface ChartSeriesData {
color?: string | undefined;
}

export const WrappedByAutoSizer = styled.div<{ height?: string }>`
const WrappedByAutoSizerComponent = styled.div<{ height?: string }>`
${style =>
`
height: ${style.height != null ? style.height : defaultChartHeight};
Expand All @@ -66,7 +68,9 @@ export const WrappedByAutoSizer = styled.div<{ height?: string }>`
}
`;

WrappedByAutoSizer.displayName = 'WrappedByAutoSizer';
WrappedByAutoSizerComponent.displayName = 'WrappedByAutoSizer';

export const WrappedByAutoSizer = React.memo(WrappedByAutoSizerComponent);

export enum SeriesType {
BAR = 'bar',
Expand Down Expand Up @@ -96,8 +100,9 @@ const theme: PartialTheme = {
export const useTheme = () => {
const isDarkMode = useUiSetting<boolean>(DEFAULT_DARK_MODE);
const defaultTheme = isDarkMode ? DARK_THEME : LIGHT_THEME;
const themeValue = useMemo(() => mergeWithDefaultTheme(theme, defaultTheme), []);

return mergeWithDefaultTheme(theme, defaultTheme);
return themeValue;
};

export const chartDefaultSettings = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isEqual } from 'lodash/fp';
import React, { createContext, useContext, useEffect } from 'react';
import {
Draggable,
Expand All @@ -14,6 +13,7 @@ import {
} from 'react-beautiful-dnd';
import { connect, ConnectedProps } from 'react-redux';
import styled from 'styled-components';
import deepEqual from 'fast-deep-equal/es6/react';

import { EuiPortal } from '@elastic/eui';
import { dragAndDropActions } from '../../store/drag_and_drop';
Expand Down Expand Up @@ -122,7 +122,7 @@ const DraggableWrapperComponent = React.memo<Props>(
},
(prevProps, nextProps) => {
return (
isEqual(prevProps.dataProvider, nextProps.dataProvider) &&
deepEqual(prevProps.dataProvider, nextProps.dataProvider) &&
prevProps.render !== nextProps.render &&
prevProps.truncate === nextProps.truncate
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

import { EuiPanel } from '@elastic/eui';
import deepEqual from 'fast-deep-equal';
import { getOr, isEmpty, isEqual, union } from 'lodash/fp';
import { getOr, isEmpty, union } from 'lodash/fp';
import React, { useMemo } from 'react';
import styled from 'styled-components';
import deepEqual from 'fast-deep-equal/es6/react';
import useResizeObserver from 'use-resize-observer';

import { BrowserFields } from '../../containers/source';
Expand Down Expand Up @@ -230,7 +230,7 @@ const EventsViewerComponent: React.FC<Props> = ({
export const EventsViewer = React.memo(
EventsViewerComponent,
(prevProps, nextProps) =>
isEqual(prevProps.browserFields, nextProps.browserFields) &&
deepEqual(prevProps.browserFields, nextProps.browserFields) &&
prevProps.columns === nextProps.columns &&
prevProps.dataProviders === nextProps.dataProviders &&
prevProps.deletedEventIds === nextProps.deletedEventIds &&
Expand All @@ -243,9 +243,9 @@ export const EventsViewer = React.memo(
prevProps.itemsPerPage === nextProps.itemsPerPage &&
prevProps.itemsPerPageOptions === nextProps.itemsPerPageOptions &&
prevProps.kqlMode === nextProps.kqlMode &&
isEqual(prevProps.query, nextProps.query) &&
deepEqual(prevProps.query, nextProps.query) &&
prevProps.start === nextProps.start &&
prevProps.sort === nextProps.sort &&
isEqual(prevProps.timelineTypeContext, nextProps.timelineTypeContext) &&
deepEqual(prevProps.timelineTypeContext, nextProps.timelineTypeContext) &&
prevProps.utilityBar === nextProps.utilityBar
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isEqual } from 'lodash/fp';
import React, { useCallback, useMemo, useEffect } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import deepEqual from 'fast-deep-equal/es6/react';

import { inputsModel, inputsSelectors, State, timelineSelectors } from '../../store';
import { inputsActions, timelineActions } from '../../store/actions';
import { SubsetTimelineModel, TimelineModel } from '../../store/timeline/model';
Expand Down Expand Up @@ -193,23 +194,23 @@ export const StatefulEventsViewer = connector(
StatefulEventsViewerComponent,
(prevProps, nextProps) =>
prevProps.id === nextProps.id &&
isEqual(prevProps.columns, nextProps.columns) &&
isEqual(prevProps.dataProviders, nextProps.dataProviders) &&
deepEqual(prevProps.columns, nextProps.columns) &&
deepEqual(prevProps.dataProviders, nextProps.dataProviders) &&
prevProps.deletedEventIds === nextProps.deletedEventIds &&
prevProps.end === nextProps.end &&
isEqual(prevProps.filters, nextProps.filters) &&
deepEqual(prevProps.filters, nextProps.filters) &&
prevProps.isLive === nextProps.isLive &&
prevProps.itemsPerPage === nextProps.itemsPerPage &&
isEqual(prevProps.itemsPerPageOptions, nextProps.itemsPerPageOptions) &&
deepEqual(prevProps.itemsPerPageOptions, nextProps.itemsPerPageOptions) &&
prevProps.kqlMode === nextProps.kqlMode &&
isEqual(prevProps.query, nextProps.query) &&
isEqual(prevProps.sort, nextProps.sort) &&
deepEqual(prevProps.query, nextProps.query) &&
deepEqual(prevProps.sort, nextProps.sort) &&
prevProps.start === nextProps.start &&
isEqual(prevProps.pageFilters, nextProps.pageFilters) &&
deepEqual(prevProps.pageFilters, nextProps.pageFilters) &&
prevProps.showCheckboxes === nextProps.showCheckboxes &&
prevProps.showRowRenderers === nextProps.showRowRenderers &&
prevProps.start === nextProps.start &&
isEqual(prevProps.timelineTypeContext, nextProps.timelineTypeContext) &&
deepEqual(prevProps.timelineTypeContext, nextProps.timelineTypeContext) &&
prevProps.utilityBar === nextProps.utilityBar
)
);
55 changes: 33 additions & 22 deletions x-pack/legacy/plugins/siem/public/components/flyout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { EuiBadge } from '@elastic/eui';
import { defaultTo, getOr } from 'lodash/fp';
import React from 'react';
import React, { useCallback } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import styled from 'styled-components';

Expand Down Expand Up @@ -58,28 +58,39 @@ export const FlyoutComponent = React.memo<Props>(
timelineId,
usersViewing,
width,
}) => (
<>
<Visible show={show}>
<Pane
flyoutHeight={flyoutHeight}
headerHeight={headerHeight}
onClose={() => showTimeline({ id: timelineId, show: false })}
}) => {
const handleClose = useCallback(() => showTimeline({ id: timelineId, show: false }), [
showTimeline,
timelineId,
]);
const handleOpen = useCallback(() => showTimeline({ id: timelineId, show: true }), [
showTimeline,
timelineId,
]);

return (
<>
<Visible show={show}>
<Pane
flyoutHeight={flyoutHeight}
headerHeight={headerHeight}
onClose={handleClose}
timelineId={timelineId}
usersViewing={usersViewing}
width={width}
>
{children}
</Pane>
</Visible>
<FlyoutButton
dataProviders={dataProviders!}
show={!show}
timelineId={timelineId}
usersViewing={usersViewing}
width={width}
>
{children}
</Pane>
</Visible>
<FlyoutButton
dataProviders={dataProviders!}
show={!show}
timelineId={timelineId}
onOpen={() => showTimeline({ id: timelineId, show: true })}
/>
</>
)
onOpen={handleOpen}
/>
</>
);
}
);

FlyoutComponent.displayName = 'FlyoutComponent';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { EuiButtonEmpty, EuiButtonIcon } from '@elastic/eui';
import { getOr } from 'lodash/fp';
import { getOr, omit } from 'lodash/fp';
import React, { useCallback } from 'react';
import { connect } from 'react-redux';
import { ActionCreator } from 'typescript-fsa';
Expand Down Expand Up @@ -162,7 +162,11 @@ const makeMapStateToProps = () => {
const getGlobalQuery = inputsSelectors.globalQueryByIdSelector();
const getTimelineQuery = inputsSelectors.timelineQueryByIdSelector();
const mapStateToProps = (state: State, { inputId = 'global', queryId }: OwnProps) => {
return inputId === 'global' ? getGlobalQuery(state, queryId) : getTimelineQuery(state, queryId);
const props =
inputId === 'global' ? getGlobalQuery(state, queryId) : getTimelineQuery(state, queryId);
// refetch caused unnecessary component rerender and it was even not used
const propsWithoutRefetch = omit('refetch', props);
return propsWithoutRefetch;
};
return mapStateToProps;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

import isEqual from 'lodash/fp/isEqual';
import deepEqual from 'fast-deep-equal';
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import deepEqual from 'fast-deep-equal/es6/react';

import { useKibana } from '../../lib/kibana';
import { RouteSpyState } from '../../utils/route/types';
Expand Down Expand Up @@ -81,8 +80,8 @@ export const SiemNavigationRedux = compose<
(prevProps, nextProps) =>
prevProps.pathName === nextProps.pathName &&
prevProps.search === nextProps.search &&
isEqual(prevProps.navTabs, nextProps.navTabs) &&
isEqual(prevProps.urlState, nextProps.urlState) &&
deepEqual(prevProps.navTabs, nextProps.navTabs) &&
deepEqual(prevProps.urlState, nextProps.urlState) &&
deepEqual(prevProps.state, nextProps.state)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/* eslint-disable react/display-name */

import { has } from 'lodash/fp';
import React, { useCallback } from 'react';
import React, { useCallback, useMemo } from 'react';
import { connect, ConnectedProps } from 'react-redux';

import { hostsActions } from '../../../../store/hosts';
Expand Down Expand Up @@ -100,10 +100,12 @@ const AuthenticationTableComponent = React.memo<AuthenticationTableProps>(
[type, updateTableActivePage]
);

const columns = useMemo(() => getAuthenticationColumnsCurated(type), [type]);

return (
<PaginatedTable
activePage={activePage}
columns={getAuthenticationColumnsCurated(type)}
columns={columns}
dataTestSubj={`table-${tableType}`}
headerCount={totalCount}
headerTitle={i18n.AUTHENTICATIONS}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import React, { useMemo, useCallback } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import { IIndexPattern } from 'src/plugins/data/public';

import { hostsActions } from '../../../../store/actions';
import {
Direction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { EuiFlexGroup, EuiFlexItem, EuiLoadingSpinner } from '@elastic/eui';
import React from 'react';
import styled from 'styled-components';

import { KpiHostsData, KpiHostDetailsData } from '../../../../graphql/types';
import { StatItemsComponent, StatItemsProps, useKpiMatrixStatus } from '../../../stat_items';
import { kpiHostsMapping } from './kpi_hosts_mapping';
Expand Down
Loading