mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Security Solution][Fix] Set doc count to 0 when empty aggregated data (#156497)
## Summary bug: https://github.com/elastic/kibana/issues/155228 Despite the hit count may be greater than 0 in the aggregation response, it is possible it won't contain any data, this happens when the aggregated field is empty in all documents, such as the `kibana.alert.rule.name` field. In this situation, we should display `0 events` in the visualization subtitle. 
This commit is contained in:
parent
cb0d0b970a
commit
93e75de88f
2 changed files with 39 additions and 4 deletions
|
@ -45,8 +45,11 @@ jest.mock('./utils', () => ({
|
|||
getCustomChartData: jest.fn().mockReturnValue(true),
|
||||
}));
|
||||
|
||||
const mockUseVisualizationResponse = jest.fn(() => [
|
||||
{ aggregations: [{ buckets: [{ key: '1234' }] }], hits: { total: 999 } },
|
||||
]);
|
||||
jest.mock('../visualization_actions/use_visualization_response', () => ({
|
||||
useVisualizationResponse: jest.fn().mockReturnValue([{ hits: { total: 999 } }]),
|
||||
useVisualizationResponse: () => mockUseVisualizationResponse(),
|
||||
}));
|
||||
|
||||
const mockLocation = jest.fn().mockReturnValue({ pathname: '/test' });
|
||||
|
@ -340,5 +343,25 @@ describe('Matrix Histogram Component', () => {
|
|||
'Showing: 999 events'
|
||||
);
|
||||
});
|
||||
|
||||
test('it should render 0 as subtitle when buckets are empty', () => {
|
||||
mockUseVisualizationResponse.mockReturnValue([
|
||||
{ aggregations: [{ buckets: [] }], hits: { total: 999 } },
|
||||
]);
|
||||
mockUseMatrix.mockReturnValue([
|
||||
false,
|
||||
{
|
||||
data: [],
|
||||
inspect: false,
|
||||
totalCount: 0,
|
||||
},
|
||||
]);
|
||||
wrapper.setProps({ endDate: 100 });
|
||||
wrapper.update();
|
||||
|
||||
expect(wrapper.find(`[data-test-subj="header-section-subtitle"]`).text()).toEqual(
|
||||
'Showing: 0 events'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -11,6 +11,7 @@ import styled from 'styled-components';
|
|||
|
||||
import { EuiFlexGroup, EuiFlexItem, EuiProgress, EuiSelect, EuiSpacer } from '@elastic/eui';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import type { AggregationsTermsAggregateBase } from '@elastic/elasticsearch/lib/api/types';
|
||||
import * as i18n from './translations';
|
||||
import { HeaderSection } from '../header_section';
|
||||
import { Panel } from '../panel';
|
||||
|
@ -30,7 +31,11 @@ import { setAbsoluteRangeDatePicker } from '../../store/inputs/actions';
|
|||
import { InputsModelId } from '../../store/inputs/constants';
|
||||
import { HoverVisibilityContainer } from '../hover_visibility_container';
|
||||
import { VisualizationActions } from '../visualization_actions/actions';
|
||||
import type { GetLensAttributes, LensAttributes } from '../visualization_actions/types';
|
||||
import type {
|
||||
GetLensAttributes,
|
||||
LensAttributes,
|
||||
VisualizationResponse,
|
||||
} from '../visualization_actions/types';
|
||||
import { useQueryToggle } from '../../containers/query_toggle';
|
||||
import { useIsExperimentalFeatureEnabled } from '../../hooks/use_experimental_features';
|
||||
import { VISUALIZATION_ACTIONS_BUTTON_CLASS } from '../visualization_actions/utils';
|
||||
|
@ -73,6 +78,11 @@ const HistogramPanel = styled(Panel)<{ height?: number }>`
|
|||
|
||||
const CHART_HEIGHT = '150px';
|
||||
|
||||
const visualizationResponseHasData = (response: VisualizationResponse): boolean =>
|
||||
Object.values<AggregationsTermsAggregateBase<unknown[]>>(response.aggregations ?? {}).some(
|
||||
({ buckets }) => buckets.length > 0
|
||||
);
|
||||
|
||||
export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> = ({
|
||||
chartHeight,
|
||||
defaultStackByOption,
|
||||
|
@ -201,8 +211,10 @@ export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> =
|
|||
|
||||
if (typeof subtitle === 'function') {
|
||||
if (isChartEmbeddablesEnabled) {
|
||||
const visualizationCount =
|
||||
visualizationResponse != null ? visualizationResponse[0].hits.total : 0;
|
||||
if (!visualizationResponse || !visualizationResponseHasData(visualizationResponse[0])) {
|
||||
return subtitle(0);
|
||||
}
|
||||
const visualizationCount = visualizationResponse[0].hits.total;
|
||||
return visualizationCount >= 0 ? subtitle(visualizationCount) : null;
|
||||
} else {
|
||||
return totalCount >= 0 ? subtitle(totalCount) : null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue