File tree 7 files changed +32
-46
lines changed
pages/workflow_detail/workflow_inputs
processor_inputs/ml_processor_inputs
7 files changed +32
-46
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import {
27
27
} from '../../../../../common' ;
28
28
import { SourceDataModal } from './source_data_modal' ;
29
29
import { BulkPopoverContent } from './bulk_popover_content' ;
30
+ import { getObjsFromJSONLines } from '../../../../utils' ;
30
31
31
32
interface SourceDataProps {
32
33
workflow : Workflow | undefined ;
@@ -42,11 +43,7 @@ export function SourceData(props: SourceDataProps) {
42
43
const { values, setFieldValue } = useFormikContext < WorkflowFormValues > ( ) ;
43
44
44
45
// empty/populated docs state
45
- let docs = [ ] ;
46
- try {
47
- const lines = getIn ( values , 'ingest.docs' , '' ) . split ( '\n' ) as string [ ] ;
48
- lines . forEach ( ( line ) => docs . push ( JSON . parse ( line ) ) ) ;
49
- } catch { }
46
+ const docs = getObjsFromJSONLines ( getIn ( values , 'ingest.docs' , '' ) ) ;
50
47
const docsPopulated = docs . length > 0 ;
51
48
52
49
// selected option state
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ import {
49
49
generateTransform ,
50
50
getDataSourceId ,
51
51
getInitialValue ,
52
+ getObjsFromJSONLines ,
52
53
getPlaceholdersFromQuery ,
53
54
injectParameters ,
54
55
prepareDocsForSimulate ,
@@ -128,11 +129,7 @@ export function ConfigureExpressionModal(props: ConfigureExpressionModalProps) {
128
129
`${ props . baseConfigPath } .${ props . config . id } .one_to_one`
129
130
) ;
130
131
const docs = getIn ( values , 'ingest.docs' ) ;
131
- let docObjs = [ ] as { } [ ] | undefined ;
132
- try {
133
- const lines = docs ?. split ( '\n' ) as string [ ] ;
134
- lines . forEach ( ( line ) => docObjs ?. push ( JSON . parse ( line ) ) ) ;
135
- } catch { }
132
+ const docObjs = getObjsFromJSONLines ( docs ) ;
136
133
const query = getIn ( values , 'search.request' ) ;
137
134
let queryObj = { } as { } | undefined ;
138
135
try {
@@ -475,12 +472,8 @@ export function ConfigureExpressionModal(props: ConfigureExpressionModalProps) {
475
472
} ) ;
476
473
} else {
477
474
try {
478
- const docObjs = [ ] as { } [ ] ;
479
- const lines = values ?. ingest ?. docs ?. split (
480
- '\n'
481
- ) as string [ ] ;
482
- lines . forEach ( ( line ) =>
483
- docObjs ?. push ( JSON . parse ( line ) )
475
+ const docObjs = getObjsFromJSONLines (
476
+ values ?. ingest ?. docs
484
477
) ;
485
478
if ( docObjs . length > 0 ) {
486
479
setSourceInput (
Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ import {
47
47
formikToPartialPipeline ,
48
48
generateTransform ,
49
49
getDataSourceId ,
50
+ getObjsFromJSONLines ,
50
51
getPlaceholdersFromQuery ,
51
52
injectParameters ,
52
53
prepareDocsForSimulate ,
@@ -134,11 +135,7 @@ export function ConfigureMultiExpressionModal(
134
135
135
136
// get some current form values
136
137
const docs = getIn ( values , 'ingest.docs' ) ;
137
- let docObjs = [ ] as { } [ ] | undefined ;
138
- try {
139
- const lines = docs ?. split ( '\n' ) as string [ ] ;
140
- lines . forEach ( ( line ) => docObjs ?. push ( JSON . parse ( line ) ) ) ;
141
- } catch { }
138
+ const docObjs = getObjsFromJSONLines ( docs ) ;
142
139
const query = getIn ( values , 'search.request' ) ;
143
140
let queryObj = { } as { } | undefined ;
144
141
try {
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ import {
53
53
generateTransform ,
54
54
getDataSourceId ,
55
55
getInitialValue ,
56
+ getObjsFromJSONLines ,
56
57
getPlaceholdersFromQuery ,
57
58
injectParameters ,
58
59
prepareDocsForSimulate ,
@@ -154,11 +155,7 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
154
155
`${ props . baseConfigPath } .${ props . config . id } .one_to_one`
155
156
) ;
156
157
const docs = getIn ( values , 'ingest.docs' ) ;
157
- let docObjs = [ ] as { } [ ] | undefined ;
158
- try {
159
- const lines = docs ?. split ( '\n' ) as string [ ] ;
160
- lines . forEach ( ( line ) => docObjs ?. push ( JSON . parse ( line ) ) ) ;
161
- } catch { }
158
+ const docObjs = getObjsFromJSONLines ( docs ) ;
162
159
const query = getIn ( values , 'search.request' ) ;
163
160
let queryObj = { } as { } | undefined ;
164
161
try {
@@ -706,9 +703,9 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
706
703
} ) ;
707
704
} else {
708
705
try {
709
- const docObjs = JSON . parse (
710
- values . ingest . docs
711
- ) as { } [ ] ;
706
+ const docObjs = getObjsFromJSONLines (
707
+ values ? .ingest ? .docs
708
+ ) ;
712
709
if ( docObjs . length > 0 ) {
713
710
setSourceInput (
714
711
customStringify ( docObjs [ 0 ] )
Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ import {
46
46
import { AppState , getMappings , useAppDispatch } from '../../../../../store' ;
47
47
import {
48
48
getDataSourceId ,
49
+ getObjsFromJSONLines ,
49
50
parseModelInputs ,
50
51
sanitizeJSONPath ,
51
52
} from '../../../../../utils' ;
@@ -126,9 +127,10 @@ export function ModelInputs(props: ModelInputsProps) {
126
127
> ( [ ] ) ;
127
128
useEffect ( ( ) => {
128
129
try {
129
- const docObjKeys = Object . keys (
130
- flattie ( ( JSON . parse ( values . ingest . docs ) as { } [ ] ) [ 0 ] )
130
+ const ingestDocsObjs = getObjsFromJSONLines (
131
+ getIn ( values , ' ingest.docs' , '' )
131
132
) ;
133
+ const docObjKeys = Object . keys ( flattie ( ingestDocsObjs [ 0 ] ) ) ;
132
134
if ( docObjKeys . length > 0 ) {
133
135
setDocFields (
134
136
docObjKeys . map ( ( key ) => {
Original file line number Diff line number Diff line change @@ -61,6 +61,7 @@ import {
61
61
useDataSourceVersion ,
62
62
getIsPreV219 ,
63
63
useMissingDataSourceVersion ,
64
+ getObjsFromJSONLines ,
64
65
} from '../../../utils' ;
65
66
import { BooleanField } from './input_fields' ;
66
67
import '../workspace/workspace-styles.scss' ;
@@ -271,11 +272,7 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
271
272
// populated ingest docs state
272
273
const [ docsPopulated , setDocsPopulated ] = useState < boolean > ( false ) ;
273
274
useEffect ( ( ) => {
274
- let parsedDocsObjs = [ ] as { } [ ] ;
275
- try {
276
- const lines = props . ingestDocs ?. split ( '\n' ) as string [ ] ;
277
- lines . forEach ( ( line ) => parsedDocsObjs . push ( JSON . parse ( line ) ) ) ;
278
- } catch { }
275
+ const parsedDocsObjs = getObjsFromJSONLines ( props . ingestDocs ) ;
279
276
setDocsPopulated ( parsedDocsObjs . length > 0 && ! isEmpty ( parsedDocsObjs [ 0 ] ) ) ;
280
277
} , [ props . ingestDocs ] ) ;
281
278
@@ -604,11 +601,7 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
604
601
props . setIsRunningIngest ( true ) ;
605
602
let success = false ;
606
603
try {
607
- let ingestDocsObjs = [ ] as { } [ ] ;
608
- try {
609
- const lines = props . ingestDocs ?. split ( '\n' ) as string [ ] ;
610
- lines . forEach ( ( line ) => ingestDocsObjs . push ( JSON . parse ( line ) ) ) ;
611
- } catch ( e ) { }
604
+ const ingestDocsObjs = getObjsFromJSONLines ( props . ingestDocs ) ;
612
605
if ( ingestDocsObjs . length > 0 && ! isEmpty ( ingestDocsObjs [ 0 ] ) ) {
613
606
success = await validateAndUpdateWorkflow ( false , true , false ) ;
614
607
if ( success ) {
Original file line number Diff line number Diff line change @@ -209,11 +209,7 @@ export function prepareDocsForSimulate(
209
209
indexName : string
210
210
) : SimulateIngestPipelineDoc [ ] {
211
211
const preparedDocs = [ ] as SimulateIngestPipelineDoc [ ] ;
212
- let docObjs = [ ] as { } [ ] ;
213
- try {
214
- const lines = docs ?. split ( '\n' ) as string [ ] ;
215
- lines . forEach ( ( line ) => docObjs . push ( JSON . parse ( line ) ) ) ;
216
- } catch { }
212
+ const docObjs = getObjsFromJSONLines ( docs ) ;
217
213
docObjs ?. forEach ( ( doc ) => {
218
214
preparedDocs . push ( {
219
215
_index : indexName ,
@@ -224,6 +220,17 @@ export function prepareDocsForSimulate(
224
220
return preparedDocs ;
225
221
}
226
222
223
+ // Utility fn to transform a raw JSON Lines string into an arr of JSON objs
224
+ // for easier downstream parsing
225
+ export function getObjsFromJSONLines ( jsonLines : string | undefined ) : { } [ ] {
226
+ let objs = [ ] as { } [ ] ;
227
+ try {
228
+ const lines = jsonLines ?. split ( '\n' ) as string [ ] ;
229
+ lines . forEach ( ( line ) => objs . push ( JSON . parse ( line ) ) ) ;
230
+ } catch { }
231
+ return objs ;
232
+ }
233
+
227
234
// Docs are returned in a certain format from the simulate ingest pipeline API. We want
228
235
// to format them into a more readable string to display
229
236
export function unwrapTransformedDocs (
You can’t perform that action at this time.
0 commit comments