Skip to content

Commit e653b6d

Browse files
[Security Solution] Refactor MatrixHistogram to use Search Strategy (#76603) (#76856)
1 parent fee6631 commit e653b6d

File tree

42 files changed

+1254
-499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1254
-499
lines changed

x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
export * from './authentications';
87
export * from './all';
8+
export * from './authentications';
99
export * from './common';
10-
export * from './overview';
1110
export * from './first_last_seen';
11+
export * from './overview';
1212
export * from './uncommon_processes';
1313

1414
export enum HostsQueries {

x-pack/plugins/security_solution/common/search_strategy/security_solution/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import {
3030
NetworkTopNFlowStrategyResponse,
3131
NetworkTopNFlowRequestOptions,
3232
} from './network';
33+
import {
34+
MatrixHistogramQuery,
35+
MatrixHistogramRequestOptions,
36+
MatrixHistogramStrategyResponse,
37+
} from './matrix_histogram';
3338
import {
3439
DocValueFields,
3540
TimerangeInput,
@@ -39,9 +44,10 @@ import {
3944
} from '../common';
4045

4146
export * from './hosts';
47+
export * from './matrix_histogram';
4248
export * from './network';
4349

44-
export type FactoryQueryTypes = HostsQueries | NetworkQueries;
50+
export type FactoryQueryTypes = HostsQueries | NetworkQueries | typeof MatrixHistogramQuery;
4551

4652
export interface RequestBasicOptions extends IEsSearchRequest {
4753
timerange: TimerangeInput;
@@ -81,6 +87,8 @@ export type StrategyResponseType<T extends FactoryQueryTypes> = T extends HostsQ
8187
? NetworkTopCountriesStrategyResponse
8288
: T extends NetworkQueries.topNFlow
8389
? NetworkTopNFlowStrategyResponse
90+
: T extends typeof MatrixHistogramQuery
91+
? MatrixHistogramStrategyResponse
8492
: never;
8593

8694
export type StrategyRequestType<T extends FactoryQueryTypes> = T extends HostsQueries.hosts
@@ -101,4 +109,6 @@ export type StrategyRequestType<T extends FactoryQueryTypes> = T extends HostsQu
101109
? NetworkTopCountriesRequestOptions
102110
: T extends NetworkQueries.topNFlow
103111
? NetworkTopNFlowRequestOptions
112+
: T extends typeof MatrixHistogramQuery
113+
? MatrixHistogramRequestOptions
104114
: never;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 { HistogramBucket } from '../common';
8+
9+
export interface AlertsGroupData {
10+
key: string;
11+
doc_count: number;
12+
alerts: {
13+
buckets: HistogramBucket[];
14+
};
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 { SearchHit } from '../../../common';
8+
9+
interface AnomaliesOverTimeHistogramData {
10+
key_as_string: string;
11+
key: number;
12+
doc_count: number;
13+
}
14+
15+
export interface AnomaliesActionGroupData {
16+
key: number;
17+
anomalies: {
18+
bucket: AnomaliesOverTimeHistogramData[];
19+
};
20+
doc_count: number;
21+
}
22+
23+
export interface AnomalySource {
24+
[field: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
25+
}
26+
27+
export interface AnomalyHit extends SearchHit {
28+
sort: string[];
29+
_source: AnomalySource;
30+
aggregations: {
31+
[agg: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
32+
};
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
export interface AuthenticationsOverTimeHistogramData {
8+
key_as_string: string;
9+
key: number;
10+
doc_count: number;
11+
}
12+
13+
export interface AuthenticationsActionGroupData {
14+
key: number;
15+
events: {
16+
bucket: AuthenticationsOverTimeHistogramData[];
17+
};
18+
doc_count: number;
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export interface HistogramBucket {
8+
key: number;
9+
doc_count: number;
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
export interface DnsHistogramSubBucket {
8+
key: string;
9+
doc_count: number;
10+
orderAgg: {
11+
value: number;
12+
};
13+
}
14+
interface DnsHistogramBucket {
15+
doc_count_error_upper_bound: number;
16+
sum_other_doc_count: number;
17+
buckets: DnsHistogramSubBucket[];
18+
}
19+
20+
export interface DnsHistogramGroupData {
21+
key: number;
22+
doc_count: number;
23+
key_as_string: string;
24+
histogram: DnsHistogramBucket;
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { SearchHit } from '../../../common';
8+
9+
interface EventsMatrixHistogramData {
10+
key_as_string: string;
11+
key: number;
12+
doc_count: number;
13+
}
14+
15+
export interface EventSource {
16+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17+
[field: string]: any;
18+
}
19+
20+
export interface EventsActionGroupData {
21+
key: number;
22+
events: {
23+
bucket: EventsMatrixHistogramData[];
24+
};
25+
doc_count: number;
26+
}
27+
28+
export interface EventHit extends SearchHit {
29+
sort: string[];
30+
_source: EventSource;
31+
aggregations: {
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
[agg: string]: any;
34+
};
35+
}
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 { IEsSearchResponse } from '../../../../../../../src/plugins/data/common';
8+
import { AuthenticationHit } from '../hosts';
9+
import { Inspect, Maybe, TimerangeInput } from '../../common';
10+
import { RequestBasicOptions } from '../';
11+
import { AlertsGroupData } from './alerts';
12+
import { AnomaliesActionGroupData, AnomalyHit } from './anomalies';
13+
import { DnsHistogramGroupData } from './dns';
14+
import { AuthenticationsActionGroupData } from './authentications';
15+
import { EventsActionGroupData, EventHit } from './events';
16+
17+
export * from './alerts';
18+
export * from './anomalies';
19+
export * from './authentications';
20+
export * from './common';
21+
export * from './dns';
22+
export * from './events';
23+
24+
export const MatrixHistogramQuery = 'matrixHistogram';
25+
26+
export enum MatrixHistogramType {
27+
authentications = 'authentications',
28+
anomalies = 'anomalies',
29+
events = 'events',
30+
alerts = 'alerts',
31+
dns = 'dns',
32+
}
33+
34+
export interface MatrixHistogramRequestOptions extends RequestBasicOptions {
35+
timerange: TimerangeInput;
36+
histogramType: MatrixHistogramType;
37+
stackByField: string;
38+
inspect?: Maybe<Inspect>;
39+
}
40+
41+
export interface MatrixHistogramStrategyResponse extends IEsSearchResponse {
42+
inspect?: Maybe<Inspect>;
43+
matrixHistogramData: MatrixHistogramData[];
44+
totalCount: number;
45+
}
46+
47+
export interface MatrixHistogramData {
48+
x?: Maybe<number>;
49+
y?: Maybe<number>;
50+
g?: Maybe<string>;
51+
}
52+
53+
export interface MatrixHistogramBucket {
54+
key: number;
55+
doc_count: number;
56+
}
57+
58+
export interface MatrixHistogramSchema<T> {
59+
buildDsl: (options: MatrixHistogramRequestOptions) => {};
60+
aggName: string;
61+
parseKey: string;
62+
parser?: <T>(data: MatrixHistogramParseData<T>, keyBucket: string) => MatrixHistogramData[];
63+
}
64+
65+
export type MatrixHistogramParseData<T> = T extends MatrixHistogramType.alerts
66+
? AlertsGroupData[]
67+
: T extends MatrixHistogramType.anomalies
68+
? AnomaliesActionGroupData[]
69+
: T extends MatrixHistogramType.dns
70+
? DnsHistogramGroupData[]
71+
: T extends MatrixHistogramType.authentications
72+
? AuthenticationsActionGroupData[]
73+
: T extends MatrixHistogramType.events
74+
? EventsActionGroupData[]
75+
: never;
76+
77+
export type MatrixHistogramHit<T> = T extends MatrixHistogramType.alerts
78+
? EventHit
79+
: T extends MatrixHistogramType.anomalies
80+
? AnomalyHit
81+
: T extends MatrixHistogramType.dns
82+
? EventHit
83+
: T extends MatrixHistogramType.authentications
84+
? AuthenticationHit
85+
: T extends MatrixHistogramType.events
86+
? EventHit
87+
: never;
88+
89+
export type MatrixHistogramDataConfig = Record<
90+
MatrixHistogramType,
91+
MatrixHistogramSchema<MatrixHistogramType>
92+
>;

x-pack/plugins/security_solution/public/common/components/alerts_viewer/histogram_configs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as i18n from './translations';
88
import { MatrixHistogramOption, MatrixHistogramConfigs } from '../matrix_histogram/types';
9-
import { HistogramType } from '../../../graphql/types';
9+
import { MatrixHistogramType } from '../../../../common/search_strategy/security_solution/matrix_histogram';
1010

1111
export const alertsStackByOptions: MatrixHistogramOption[] = [
1212
{
@@ -25,7 +25,7 @@ export const histogramConfigs: MatrixHistogramConfigs = {
2525
defaultStackByOption:
2626
alertsStackByOptions.find((o) => o.text === DEFAULT_STACK_BY) ?? alertsStackByOptions[1],
2727
errorMessage: i18n.ERROR_FETCHING_ALERTS_DATA,
28-
histogramType: HistogramType.alerts,
28+
histogramType: MatrixHistogramType.alerts,
2929
stackByOptions: alertsStackByOptions,
3030
subtitle: undefined,
3131
title: i18n.ALERTS_GRAPH_TITLE,

0 commit comments

Comments
 (0)