Skip to content

Commit b04ec68

Browse files
committed
refactored code to use existing matrix histogram hook and component for custom queries
1 parent ece38ef commit b04ec68

File tree

12 files changed

+537
-451
lines changed

12 files changed

+537
-451
lines changed

x-pack/plugins/security_solution/public/common/components/matrix_histogram/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type MatrixHistogramComponentProps = MatrixHistogramProps &
3434
defaultStackByOption: MatrixHistogramOption;
3535
errorMessage: string;
3636
headerChildren?: React.ReactNode;
37+
footerChildren?: React.ReactNode;
3738
hideHistogramIfEmpty?: boolean;
3839
histogramType: MatrixHistogramType;
3940
id: string;
@@ -47,6 +48,7 @@ export type MatrixHistogramComponentProps = MatrixHistogramProps &
4748
subtitle?: string | GetSubTitle;
4849
timelineId?: string;
4950
title: string | GetTitle;
51+
yTitle?: string | undefined;
5052
};
5153

5254
const DEFAULT_PANEL_HEIGHT = 300;
@@ -68,6 +70,7 @@ export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> =
6870
errorMessage,
6971
filterQuery,
7072
headerChildren,
73+
footerChildren,
7174
histogramType,
7275
hideHistogramIfEmpty = false,
7376
id,
@@ -86,6 +89,7 @@ export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> =
8689
title,
8790
titleSize,
8891
yTickFormatter,
92+
yTitle,
8993
}) => {
9094
const dispatch = useDispatch();
9195
const handleBrushEnd = useCallback(
@@ -114,8 +118,18 @@ export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> =
114118
onBrushEnd: handleBrushEnd,
115119
yTickFormatter,
116120
showLegend,
121+
yTitle,
117122
}),
118-
[chartHeight, startDate, legendPosition, endDate, handleBrushEnd, yTickFormatter, showLegend]
123+
[
124+
chartHeight,
125+
startDate,
126+
legendPosition,
127+
endDate,
128+
handleBrushEnd,
129+
yTickFormatter,
130+
showLegend,
131+
yTitle,
132+
]
119133
);
120134
const [isInitialLoading, setIsInitialLoading] = useState(true);
121135
const [selectedStackByOption, setSelectedStackByOption] = useState<MatrixHistogramOption>(
@@ -229,6 +243,11 @@ export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> =
229243
timelineId={timelineId}
230244
/>
231245
)}
246+
{footerChildren != null && (
247+
<EuiFlexGroup gutterSize="none" direction="row">
248+
{footerChildren}
249+
</EuiFlexGroup>
250+
)}
232251
</HistogramPanel>
233252
</InspectButtonContainer>
234253
{showSpacer && <EuiSpacer data-test-subj="spacer" size="l" />}

x-pack/plugins/security_solution/public/common/components/matrix_histogram/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export interface BarchartConfigs {
105105
yTickFormatter: TickFormatter;
106106
tickSize: number;
107107
};
108+
yAxisTitle: string | undefined;
108109
settings: {
109110
legendPosition: Position;
110111
onBrushEnd: UpdateDateRange;

x-pack/plugins/security_solution/public/common/components/matrix_histogram/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface GetBarchartConfigsProps {
1919
onBrushEnd: UpdateDateRange;
2020
yTickFormatter?: (value: number) => string;
2121
showLegend?: boolean;
22+
yTitle?: string | undefined;
2223
}
2324

2425
export const DEFAULT_CHART_HEIGHT = 174;
@@ -32,6 +33,7 @@ export const getBarchartConfigs = ({
3233
onBrushEnd,
3334
yTickFormatter,
3435
showLegend,
36+
yTitle,
3537
}: GetBarchartConfigsProps): BarchartConfigs => ({
3638
series: {
3739
xScaleType: ScaleType.Time,
@@ -43,6 +45,7 @@ export const getBarchartConfigs = ({
4345
yTickFormatter: yTickFormatter != null ? yTickFormatter : DEFAULT_Y_TICK_FORMATTER,
4446
tickSize: 8,
4547
},
48+
yAxisTitle: yTitle,
4649
settings: {
4750
legendPosition: legendPosition ?? Position.Right,
4851
onBrushEnd,

x-pack/plugins/security_solution/public/common/containers/matrix_histogram/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import deepEqual from 'fast-deep-equal';
8-
import { noop } from 'lodash/fp';
8+
import { getOr, noop } from 'lodash/fp';
99
import { useCallback, useEffect, useRef, useState } from 'react';
1010

1111
import { MatrixHistogramQueryProps } from '../../components/matrix_histogram/types';
@@ -44,7 +44,15 @@ export const useMatrixHistogram = ({
4444
indexNames,
4545
stackByField,
4646
startDate,
47-
}: MatrixHistogramQueryProps): [boolean, UseMatrixHistogramArgs] => {
47+
threshold,
48+
}: MatrixHistogramQueryProps): [
49+
boolean,
50+
UseMatrixHistogramArgs,
51+
Array<{
52+
key: string;
53+
doc_count: number;
54+
}>
55+
] => {
4856
const { data, notifications } = useKibana().services;
4957
const refetch = useRef<inputsModel.Refetch>(noop);
5058
const abortCtrl = useRef(new AbortController());
@@ -63,6 +71,7 @@ export const useMatrixHistogram = ({
6371
to: endDate,
6472
},
6573
stackByField,
74+
threshold,
6675
});
6776

6877
const [matrixHistogramResponse, setMatrixHistogramResponse] = useState<UseMatrixHistogramArgs>({
@@ -74,6 +83,12 @@ export const useMatrixHistogram = ({
7483
refetch: refetch.current,
7584
totalCount: -1,
7685
});
86+
const [matrixHistogramBuckets, setMatrixHistogramBuckets] = useState<
87+
Array<{
88+
key: string;
89+
doc_count: number;
90+
}>
91+
>([]);
7792

7893
const hostsSearch = useCallback(
7994
(request: MatrixHistogramRequestOptions) => {
@@ -91,6 +106,10 @@ export const useMatrixHistogram = ({
91106
next: (response) => {
92107
if (isCompleteResponse(response)) {
93108
if (!didCancel) {
109+
const histogramBuckets: Array<{
110+
key: string;
111+
doc_count: number;
112+
}> = getOr([], 'rawResponse.aggregations.eventActionGroup.buckets', response);
94113
setLoading(false);
95114
setMatrixHistogramResponse((prevResponse) => ({
96115
...prevResponse,
@@ -99,6 +118,7 @@ export const useMatrixHistogram = ({
99118
refetch: refetch.current,
100119
totalCount: response.totalCount,
101120
}));
121+
setMatrixHistogramBuckets(histogramBuckets);
102122
}
103123
searchSubscription$.unsubscribe();
104124
} else if (isErrorResponse(response)) {
@@ -144,17 +164,18 @@ export const useMatrixHistogram = ({
144164
to: endDate,
145165
},
146166
stackByField,
167+
threshold,
147168
};
148169
if (!deepEqual(prevRequest, myRequest)) {
149170
return myRequest;
150171
}
151172
return prevRequest;
152173
});
153-
}, [indexNames, endDate, filterQuery, startDate, stackByField, histogramType]);
174+
}, [indexNames, endDate, filterQuery, startDate, stackByField, histogramType, threshold]);
154175

155176
useEffect(() => {
156177
hostsSearch(matrixHistogramRequest);
157178
}, [matrixHistogramRequest, hostsSearch]);
158179

159-
return [loading, matrixHistogramResponse];
180+
return [loading, matrixHistogramResponse, matrixHistogramBuckets];
160181
};

x-pack/plugins/security_solution/public/common/containers/matrix_histogram/use_matrix_histogram_async.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

x-pack/plugins/security_solution/public/common/hooks/eql/helpers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ describe('eql/helpers', () => {
462462
},
463463
lte: '2020-10-04T16:00:00.368707900Z',
464464
totalCount: 0,
465+
warnings: [],
465466
});
466467
});
467468
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import React, { useEffect } from 'react';
8+
import { EuiFlexGroup, EuiFlexItem, EuiTitle, EuiSpacer, EuiText } from '@elastic/eui';
9+
import styled from 'styled-components';
10+
11+
import * as i18n from './translations';
12+
import { BarChart } from '../../../../common/components/charts/barchart';
13+
import { getHistogramConfig } from './helpers';
14+
import { ChartData } from '../../../../common/components/charts/common';
15+
import { InspectQuery } from '../../../../common/store/inputs/model';
16+
import { InspectButton } from '../../../../common/components/inspect';
17+
import { useGlobalTime } from '../../../../common/containers/use_global_time';
18+
import { Panel } from '../../../../common/components/panel';
19+
20+
export const ID = 'queryEqlPreviewHistogramQuery';
21+
22+
interface PreviewEqlQueryHistogramProps {
23+
to: string;
24+
from: string;
25+
totalHits: number;
26+
data: ChartData[];
27+
query: string;
28+
inspect: InspectQuery;
29+
}
30+
31+
export const PreviewEqlQueryHistogram = ({
32+
from,
33+
to,
34+
totalHits,
35+
data,
36+
query,
37+
inspect,
38+
}: PreviewEqlQueryHistogramProps) => {
39+
const { setQuery, isInitializing } = useGlobalTime();
40+
41+
useEffect((): void => {
42+
if (!isInitializing) {
43+
setQuery({ id: ID, inspect, loading: false, refetch: () => {} });
44+
}
45+
}, [setQuery, inspect, isInitializing]);
46+
47+
return (
48+
<>
49+
{totalHits > 0 && (
50+
<Panel height={300}>
51+
<EuiFlexGroup gutterSize="none" direction="column">
52+
<EuiFlexItem grow={1}>
53+
<EuiFlexGroup direction="row" justifyContent="center">
54+
<EuiFlexItem grow={false}>
55+
<EuiTitle size="xs">
56+
<h2 data-test-subj="header-section-title">
57+
{i18n.QUERY_PREVIEW_TITLE(totalHits)}
58+
</h2>
59+
</EuiTitle>
60+
</EuiFlexItem>
61+
62+
<EuiFlexItem grow={false}>
63+
<InspectButton
64+
queryId={ID}
65+
inspectIndex={0}
66+
title={i18n.QUERY_PREVIEW_INSPECT_TITLE}
67+
/>
68+
</EuiFlexItem>
69+
</EuiFlexGroup>
70+
</EuiFlexItem>
71+
<EuiFlexItem grow={1}>
72+
<BarChart
73+
configs={getHistogramConfig(to, from, query.includes('sequence'))}
74+
barChart={[{ key: 'hits', value: data }]}
75+
stackByField={undefined}
76+
timelineId={undefined}
77+
/>
78+
</EuiFlexItem>
79+
<EuiFlexItem grow={false}>
80+
<>
81+
<EuiSpacer />
82+
<EuiText size="s" color="subdued">
83+
<p>{i18n.PREVIEW_QUERY_DISCLAIMER_EQL}</p>
84+
</EuiText>
85+
</>
86+
</EuiFlexItem>
87+
</EuiFlexGroup>
88+
</Panel>
89+
)}
90+
</>
91+
);
92+
};

0 commit comments

Comments
 (0)