@@ -12,6 +12,7 @@ import {
12
12
IndexFieldsStrategyResponse ,
13
13
IndexField ,
14
14
IndexFieldsStrategyRequest ,
15
+ BeatFields ,
15
16
} from '../../../common/search_strategy/index_fields' ;
16
17
17
18
export const securitySolutionIndexFieldsProvider = ( ) : ISearchStrategy <
@@ -113,20 +114,19 @@ const missingFields: FieldDescriptor[] = [
113
114
* @param index The index its self
114
115
* @param indexesAliasIdx The index within the alias
115
116
*/
116
- export const createFieldItem = async (
117
+ export const createFieldItem = (
118
+ beatFields : BeatFields ,
117
119
indexesAlias : string [ ] ,
118
120
index : FieldDescriptor ,
119
121
indexesAliasIdx : number
120
- ) : Promise < IndexField > => {
121
- const { fieldsBeat } = await import ( '../../utils/beat_schema/fields' ) ;
122
-
122
+ ) : IndexField => {
123
123
const alias = indexesAlias [ indexesAliasIdx ] ;
124
124
const splitIndexName = index . name . split ( '.' ) ;
125
125
const indexName =
126
126
splitIndexName [ splitIndexName . length - 1 ] === 'text'
127
127
? splitIndexName . slice ( 0 , splitIndexName . length - 1 ) . join ( '.' )
128
128
: index . name ;
129
- const beatIndex = fieldsBeat [ indexName ] ?? { } ;
129
+ const beatIndex = beatFields [ indexName ] ?? { } ;
130
130
if ( isEmpty ( beatIndex . category ) ) {
131
131
beatIndex . category = splitIndexName [ 0 ] ;
132
132
}
@@ -154,18 +154,31 @@ export const formatFirstFields = async (
154
154
responsesIndexFields : FieldDescriptor [ ] [ ] ,
155
155
indexesAlias : string [ ]
156
156
) : Promise < IndexField [ ] > => {
157
- const result : IndexField [ ] = [ ] ;
158
-
159
- for ( const [ indexesAliasIdx , indexFields ] of responsesIndexFields . entries ( ) ) {
160
- for ( const index of missingFields ) {
161
- result . push ( await createFieldItem ( indexesAlias , index , indexesAliasIdx ) ) ;
162
- }
163
- for ( const index of indexFields ) {
164
- result . push ( await createFieldItem ( indexesAlias , index , indexesAliasIdx ) ) ;
165
- }
166
- }
157
+ return new Promise ( ( resolve ) => {
158
+ setTimeout ( ( ) => {
159
+ // require the fields once we actually need them, rather than ahead of time, and pass
160
+ // them to createFieldItem to reduce the amount of work done as much as possible
161
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
162
+ const beatFields : BeatFields = require ( '../../utils/beat_schema/fields' ) . fieldsBeat ;
167
163
168
- return result ;
164
+ resolve (
165
+ responsesIndexFields . reduce (
166
+ ( accumulator : IndexField [ ] , indexFields : FieldDescriptor [ ] , indexesAliasIdx : number ) => {
167
+ missingFields . forEach ( ( index ) => {
168
+ const item = createFieldItem ( beatFields , indexesAlias , index , indexesAliasIdx ) ;
169
+ accumulator . push ( item ) ;
170
+ } ) ;
171
+ indexFields . forEach ( ( index ) => {
172
+ const item = createFieldItem ( beatFields , indexesAlias , index , indexesAliasIdx ) ;
173
+ accumulator . push ( item ) ;
174
+ } ) ;
175
+ return accumulator ;
176
+ } ,
177
+ [ ]
178
+ )
179
+ ) ;
180
+ } ) ;
181
+ } ) ;
169
182
} ;
170
183
171
184
/**
0 commit comments