@@ -12,14 +12,18 @@ import {
12
12
IndexFieldsStrategyResponse ,
13
13
IndexField ,
14
14
IndexFieldsStrategyRequest ,
15
+ BeatFields ,
15
16
} from '../../../common/search_strategy/index_fields' ;
16
17
17
- import { fieldsBeat } from '../../utils/beat_schema/fields' ;
18
-
19
18
export const securitySolutionIndexFieldsProvider = ( ) : ISearchStrategy <
20
19
IndexFieldsStrategyRequest ,
21
20
IndexFieldsStrategyResponse
22
21
> => {
22
+ // require the fields once we actually need them, rather than ahead of time, and pass
23
+ // them to createFieldItem to reduce the amount of work done as much as possible
24
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
25
+ const beatFields : BeatFields = require ( '../../utils/beat_schema/fields' ) . fieldsBeat ;
26
+
23
27
return {
24
28
search : async ( context , request ) => {
25
29
const { elasticsearch } = context . core ;
@@ -41,6 +45,7 @@ export const securitySolutionIndexFieldsProvider = (): ISearchStrategy<
41
45
42
46
if ( ! request . onlyCheckIfIndicesExist ) {
43
47
indexFields = await formatIndexFields (
48
+ beatFields ,
44
49
responsesIndexFields . filter ( ( rif ) => rif !== false ) as FieldDescriptor [ ] [ ] ,
45
50
dedupeIndices
46
51
) ;
@@ -116,6 +121,7 @@ const missingFields: FieldDescriptor[] = [
116
121
* @param indexesAliasIdx The index within the alias
117
122
*/
118
123
export const createFieldItem = (
124
+ beatFields : BeatFields ,
119
125
indexesAlias : string [ ] ,
120
126
index : FieldDescriptor ,
121
127
indexesAliasIdx : number
@@ -126,7 +132,7 @@ export const createFieldItem = (
126
132
splitIndexName [ splitIndexName . length - 1 ] === 'text'
127
133
? splitIndexName . slice ( 0 , splitIndexName . length - 1 ) . join ( '.' )
128
134
: index . name ;
129
- const beatIndex = fieldsBeat [ indexName ] ?? { } ;
135
+ const beatIndex = beatFields [ indexName ] ?? { } ;
130
136
if ( isEmpty ( beatIndex . category ) ) {
131
137
beatIndex . category = splitIndexName [ 0 ] ;
132
138
}
@@ -151,6 +157,7 @@ export const createFieldItem = (
151
157
* @param indexesAlias The index aliases such as filebeat-*
152
158
*/
153
159
export const formatFirstFields = async (
160
+ beatFields : BeatFields ,
154
161
responsesIndexFields : FieldDescriptor [ ] [ ] ,
155
162
indexesAlias : string [ ]
156
163
) : Promise < IndexField [ ] > => {
@@ -160,11 +167,11 @@ export const formatFirstFields = async (
160
167
responsesIndexFields . reduce (
161
168
( accumulator : IndexField [ ] , indexFields : FieldDescriptor [ ] , indexesAliasIdx : number ) => {
162
169
missingFields . forEach ( ( index ) => {
163
- const item = createFieldItem ( indexesAlias , index , indexesAliasIdx ) ;
170
+ const item = createFieldItem ( beatFields , indexesAlias , index , indexesAliasIdx ) ;
164
171
accumulator . push ( item ) ;
165
172
} ) ;
166
173
indexFields . forEach ( ( index ) => {
167
- const item = createFieldItem ( indexesAlias , index , indexesAliasIdx ) ;
174
+ const item = createFieldItem ( beatFields , indexesAlias , index , indexesAliasIdx ) ;
168
175
accumulator . push ( item ) ;
169
176
} ) ;
170
177
return accumulator ;
@@ -224,10 +231,11 @@ export const formatSecondFields = async (fields: IndexField[]): Promise<IndexFie
224
231
* @param indexesAlias The index alias
225
232
*/
226
233
export const formatIndexFields = async (
234
+ beatFields : BeatFields ,
227
235
responsesIndexFields : FieldDescriptor [ ] [ ] ,
228
236
indexesAlias : string [ ]
229
237
) : Promise < IndexField [ ] > => {
230
- const fields = await formatFirstFields ( responsesIndexFields , indexesAlias ) ;
238
+ const fields = await formatFirstFields ( beatFields , responsesIndexFields , indexesAlias ) ;
231
239
const secondFields = await formatSecondFields ( fields ) ;
232
240
return secondFields ;
233
241
} ;
0 commit comments