Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport main] Fix missed UI autofilling after JSON Lines change #673

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '../../../../../common';
import { SourceDataModal } from './source_data_modal';
import { BulkPopoverContent } from './bulk_popover_content';
import { getObjsFromJSONLines } from '../../../../utils';

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

// empty/populated docs state
let docs = [];
try {
const lines = getIn(values, 'ingest.docs', '').split('\n') as string[];
lines.forEach((line) => docs.push(JSON.parse(line)));
} catch {}
const docs = getObjsFromJSONLines(getIn(values, 'ingest.docs', ''));
const docsPopulated = docs.length > 0;

// selected option state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
generateTransform,
getDataSourceId,
getInitialValue,
getObjsFromJSONLines,
getPlaceholdersFromQuery,
injectParameters,
prepareDocsForSimulate,
Expand Down Expand Up @@ -128,11 +129,7 @@ export function ConfigureExpressionModal(props: ConfigureExpressionModalProps) {
`${props.baseConfigPath}.${props.config.id}.one_to_one`
);
const docs = getIn(values, 'ingest.docs');
let docObjs = [] as {}[] | undefined;
try {
const lines = docs?.split('\n') as string[];
lines.forEach((line) => docObjs?.push(JSON.parse(line)));
} catch {}
const docObjs = getObjsFromJSONLines(docs);
const query = getIn(values, 'search.request');
let queryObj = {} as {} | undefined;
try {
Expand Down Expand Up @@ -475,12 +472,8 @@ export function ConfigureExpressionModal(props: ConfigureExpressionModalProps) {
});
} else {
try {
const docObjs = [] as {}[];
const lines = values?.ingest?.docs?.split(
'\n'
) as string[];
lines.forEach((line) =>
docObjs?.push(JSON.parse(line))
const docObjs = getObjsFromJSONLines(
values?.ingest?.docs
);
if (docObjs.length > 0) {
setSourceInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
formikToPartialPipeline,
generateTransform,
getDataSourceId,
getObjsFromJSONLines,
getPlaceholdersFromQuery,
injectParameters,
prepareDocsForSimulate,
Expand Down Expand Up @@ -134,11 +135,7 @@ export function ConfigureMultiExpressionModal(

// get some current form values
const docs = getIn(values, 'ingest.docs');
let docObjs = [] as {}[] | undefined;
try {
const lines = docs?.split('\n') as string[];
lines.forEach((line) => docObjs?.push(JSON.parse(line)));
} catch {}
const docObjs = getObjsFromJSONLines(docs);
const query = getIn(values, 'search.request');
let queryObj = {} as {} | undefined;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
generateTransform,
getDataSourceId,
getInitialValue,
getObjsFromJSONLines,
getPlaceholdersFromQuery,
injectParameters,
prepareDocsForSimulate,
Expand Down Expand Up @@ -154,11 +155,7 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
`${props.baseConfigPath}.${props.config.id}.one_to_one`
);
const docs = getIn(values, 'ingest.docs');
let docObjs = [] as {}[] | undefined;
try {
const lines = docs?.split('\n') as string[];
lines.forEach((line) => docObjs?.push(JSON.parse(line)));
} catch {}
const docObjs = getObjsFromJSONLines(docs);
const query = getIn(values, 'search.request');
let queryObj = {} as {} | undefined;
try {
Expand Down Expand Up @@ -706,9 +703,9 @@ export function ConfigureTemplateModal(props: ConfigureTemplateModalProps) {
});
} else {
try {
const docObjs = JSON.parse(
values.ingest.docs
) as {}[];
const docObjs = getObjsFromJSONLines(
values?.ingest?.docs
);
if (docObjs.length > 0) {
setSourceInput(
customStringify(docObjs[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import { AppState, getMappings, useAppDispatch } from '../../../../../store';
import {
getDataSourceId,
getObjsFromJSONLines,
parseModelInputs,
sanitizeJSONPath,
} from '../../../../../utils';
Expand Down Expand Up @@ -126,9 +127,10 @@ export function ModelInputs(props: ModelInputsProps) {
>([]);
useEffect(() => {
try {
const docObjKeys = Object.keys(
flattie((JSON.parse(values.ingest.docs) as {}[])[0])
const ingestDocsObjs = getObjsFromJSONLines(
getIn(values, 'ingest.docs', '')
);
const docObjKeys = Object.keys(flattie(ingestDocsObjs[0]));
if (docObjKeys.length > 0) {
setDocFields(
docObjKeys.map((key) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
useDataSourceVersion,
getIsPreV219,
useMissingDataSourceVersion,
getObjsFromJSONLines,
} from '../../../utils';
import { BooleanField } from './input_fields';
import '../workspace/workspace-styles.scss';
Expand Down Expand Up @@ -271,11 +272,7 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
// populated ingest docs state
const [docsPopulated, setDocsPopulated] = useState<boolean>(false);
useEffect(() => {
let parsedDocsObjs = [] as {}[];
try {
const lines = props.ingestDocs?.split('\n') as string[];
lines.forEach((line) => parsedDocsObjs.push(JSON.parse(line)));
} catch {}
const parsedDocsObjs = getObjsFromJSONLines(props.ingestDocs);
setDocsPopulated(parsedDocsObjs.length > 0 && !isEmpty(parsedDocsObjs[0]));
}, [props.ingestDocs]);

Expand Down Expand Up @@ -604,11 +601,7 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
props.setIsRunningIngest(true);
let success = false;
try {
let ingestDocsObjs = [] as {}[];
try {
const lines = props.ingestDocs?.split('\n') as string[];
lines.forEach((line) => ingestDocsObjs.push(JSON.parse(line)));
} catch (e) {}
const ingestDocsObjs = getObjsFromJSONLines(props.ingestDocs);
if (ingestDocsObjs.length > 0 && !isEmpty(ingestDocsObjs[0])) {
success = await validateAndUpdateWorkflow(false, true, false);
if (success) {
Expand Down
17 changes: 12 additions & 5 deletions public/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ export function prepareDocsForSimulate(
indexName: string
): SimulateIngestPipelineDoc[] {
const preparedDocs = [] as SimulateIngestPipelineDoc[];
let docObjs = [] as {}[];
try {
const lines = docs?.split('\n') as string[];
lines.forEach((line) => docObjs.push(JSON.parse(line)));
} catch {}
const docObjs = getObjsFromJSONLines(docs);
docObjs?.forEach((doc) => {
preparedDocs.push({
_index: indexName,
Expand All @@ -224,6 +220,17 @@ export function prepareDocsForSimulate(
return preparedDocs;
}

// Utility fn to transform a raw JSON Lines string into an arr of JSON objs
// for easier downstream parsing
export function getObjsFromJSONLines(jsonLines: string | undefined): {}[] {
let objs = [] as {}[];
try {
const lines = jsonLines?.split('\n') as string[];
lines.forEach((line) => objs.push(JSON.parse(line)));
} catch {}
return objs;
}

// Docs are returned in a certain format from the simulate ingest pipeline API. We want
// to format them into a more readable string to display
export function unwrapTransformedDocs(
Expand Down
Loading