Skip to content

Commit fe61162

Browse files
Fix autofilling for certain scenarios (#672) (#673)
(cherry picked from commit 6cefa2a) Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 4edadd7 commit fe61162

File tree

7 files changed

+32
-46
lines changed

7 files changed

+32
-46
lines changed

public/pages/workflow_detail/workflow_inputs/ingest_inputs/source_data.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from '../../../../../common';
2828
import { SourceDataModal } from './source_data_modal';
2929
import { BulkPopoverContent } from './bulk_popover_content';
30+
import { getObjsFromJSONLines } from '../../../../utils';
3031

3132
interface SourceDataProps {
3233
workflow: Workflow | undefined;
@@ -42,11 +43,7 @@ export function SourceData(props: SourceDataProps) {
4243
const { values, setFieldValue } = useFormikContext<WorkflowFormValues>();
4344

4445
// 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', ''));
5047
const docsPopulated = docs.length > 0;
5148

5249
// selected option state

public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/modals/configure_expression_modal.tsx

+4-11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
generateTransform,
5050
getDataSourceId,
5151
getInitialValue,
52+
getObjsFromJSONLines,
5253
getPlaceholdersFromQuery,
5354
injectParameters,
5455
prepareDocsForSimulate,
@@ -128,11 +129,7 @@ export function ConfigureExpressionModal(props: ConfigureExpressionModalProps) {
128129
`${props.baseConfigPath}.${props.config.id}.one_to_one`
129130
);
130131
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);
136133
const query = getIn(values, 'search.request');
137134
let queryObj = {} as {} | undefined;
138135
try {
@@ -475,12 +472,8 @@ export function ConfigureExpressionModal(props: ConfigureExpressionModalProps) {
475472
});
476473
} else {
477474
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
484477
);
485478
if (docObjs.length > 0) {
486479
setSourceInput(

public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/modals/configure_multi_expression_modal.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
formikToPartialPipeline,
4848
generateTransform,
4949
getDataSourceId,
50+
getObjsFromJSONLines,
5051
getPlaceholdersFromQuery,
5152
injectParameters,
5253
prepareDocsForSimulate,
@@ -134,11 +135,7 @@ export function ConfigureMultiExpressionModal(
134135

135136
// get some current form values
136137
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);
142139
const query = getIn(values, 'search.request');
143140
let queryObj = {} as {} | undefined;
144141
try {

public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/modals/configure_template_modal.tsx

+5-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
generateTransform,
5454
getDataSourceId,
5555
getInitialValue,
56+
getObjsFromJSONLines,
5657
getPlaceholdersFromQuery,
5758
injectParameters,
5859
prepareDocsForSimulate,
@@ -154,11 +155,7 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
154155
`${props.baseConfigPath}.${props.config.id}.one_to_one`
155156
);
156157
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);
162159
const query = getIn(values, 'search.request');
163160
let queryObj = {} as {} | undefined;
164161
try {
@@ -706,9 +703,9 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
706703
});
707704
} else {
708705
try {
709-
const docObjs = JSON.parse(
710-
values.ingest.docs
711-
) as {}[];
706+
const docObjs = getObjsFromJSONLines(
707+
values?.ingest?.docs
708+
);
712709
if (docObjs.length > 0) {
713710
setSourceInput(
714711
customStringify(docObjs[0])

public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/model_inputs.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
import { AppState, getMappings, useAppDispatch } from '../../../../../store';
4747
import {
4848
getDataSourceId,
49+
getObjsFromJSONLines,
4950
parseModelInputs,
5051
sanitizeJSONPath,
5152
} from '../../../../../utils';
@@ -126,9 +127,10 @@ export function ModelInputs(props: ModelInputsProps) {
126127
>([]);
127128
useEffect(() => {
128129
try {
129-
const docObjKeys = Object.keys(
130-
flattie((JSON.parse(values.ingest.docs) as {}[])[0])
130+
const ingestDocsObjs = getObjsFromJSONLines(
131+
getIn(values, 'ingest.docs', '')
131132
);
133+
const docObjKeys = Object.keys(flattie(ingestDocsObjs[0]));
132134
if (docObjKeys.length > 0) {
133135
setDocFields(
134136
docObjKeys.map((key) => {

public/pages/workflow_detail/workflow_inputs/workflow_inputs.tsx

+3-10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
useDataSourceVersion,
6262
getIsPreV219,
6363
useMissingDataSourceVersion,
64+
getObjsFromJSONLines,
6465
} from '../../../utils';
6566
import { BooleanField } from './input_fields';
6667
import '../workspace/workspace-styles.scss';
@@ -271,11 +272,7 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
271272
// populated ingest docs state
272273
const [docsPopulated, setDocsPopulated] = useState<boolean>(false);
273274
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);
279276
setDocsPopulated(parsedDocsObjs.length > 0 && !isEmpty(parsedDocsObjs[0]));
280277
}, [props.ingestDocs]);
281278

@@ -604,11 +601,7 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
604601
props.setIsRunningIngest(true);
605602
let success = false;
606603
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);
612605
if (ingestDocsObjs.length > 0 && !isEmpty(ingestDocsObjs[0])) {
613606
success = await validateAndUpdateWorkflow(false, true, false);
614607
if (success) {

public/utils/utils.tsx

+12-5
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,7 @@ export function prepareDocsForSimulate(
209209
indexName: string
210210
): SimulateIngestPipelineDoc[] {
211211
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);
217213
docObjs?.forEach((doc) => {
218214
preparedDocs.push({
219215
_index: indexName,
@@ -224,6 +220,17 @@ export function prepareDocsForSimulate(
224220
return preparedDocs;
225221
}
226222

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+
227234
// Docs are returned in a certain format from the simulate ingest pipeline API. We want
228235
// to format them into a more readable string to display
229236
export function unwrapTransformedDocs(

0 commit comments

Comments
 (0)