Skip to content

Commit

Permalink
[7.x] [ML] Transform: Support multi-line JSON notation in advanced ed…
Browse files Browse the repository at this point in the history
…itor (#58015) (#58239)

* [ML] Transform: Support multi-line JSON notation in advanced editor (#58015)

* [ML] use xJsonMode

* [ML] remove commented code

* [ML] move use_x_json_mode hook, disable json formatting

* [ML] mocks for shared_imports

* [ML] ts-ignore worker import

* [ML] remove unused imports
  • Loading branch information
darnautov authored Feb 22, 2020
1 parent fc6674a commit 25b3618
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 9 deletions.
10 changes: 10 additions & 0 deletions x-pack/legacy/plugins/transform/public/__mocks__/shared_imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export function XJsonMode() {}
export function setDependencyCache() {}
export { mlInMemoryTableBasicFactory } from '../../../ml/public/application/components/ml_in_memory_table';
export const SORT_DIRECTION = { ASC: 'asc' };
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { useState } from 'react';
import { collapseLiteralStrings, expandLiteralStrings, XJsonMode } from '../../shared_imports';

export const xJsonMode = new XJsonMode();

export const useXJsonMode = (json: string) => {
const [xJson, setXJson] = useState(expandLiteralStrings(json));

return {
xJson,
setXJson,
xJsonMode,
convertToJson: collapseLiteralStrings,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jest.mock('react', () => {
return { ...r, memo: (x: any) => x };
});

jest.mock('../../../../../shared_imports');

describe('Transform: <SourceIndexPreview />', () => {
test('Minimal initialization', () => {
const props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jest.mock('react', () => {
return { ...r, memo: (x: any) => x };
});

jest.mock('../../../../../shared_imports');

describe('Transform: <StepCreateForm />', () => {
test('Minimal initialization', () => {
const props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jest.mock('react', () => {
return { ...r, memo: (x: any) => x };
});

jest.mock('../../../../../shared_imports');

describe('Transform: <PivotPreview />', () => {
test('Minimal initialization', () => {
const groupBy: PivotGroupByConfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jest.mock('react', () => {
return { ...r, memo: (x: any) => x };
});

jest.mock('../../../../../shared_imports');

describe('Transform: <DefinePivotForm />', () => {
test('Minimal initialization', () => {
// Using a wrapping <div> element because shallow() would fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
EuiSwitch,
} from '@elastic/eui';

import { useXJsonMode, xJsonMode } from '../../../../hooks/use_x_json_mode';
import { dictionaryToArray } from '../../../../../../common/types/common';
import { DropDown } from '../aggregation_dropdown';
import { AggListForm } from '../aggregation_list';
Expand Down Expand Up @@ -329,7 +330,13 @@ export const StepDefineForm: FC<Props> = React.memo(({ overrides = {}, onChange
const [advancedEditorConfigLastApplied, setAdvancedEditorConfigLastApplied] = useState(
stringifiedPivotConfig
);
const [advancedEditorConfig, setAdvancedEditorConfig] = useState(stringifiedPivotConfig);

const {
convertToJson,
setXJson: setAdvancedEditorConfig,
xJson: advancedEditorConfig,
} = useXJsonMode(stringifiedPivotConfig);

// source config
const stringifiedSourceConfig = JSON.stringify(previewRequest.source.query, null, 2);
const [
Expand All @@ -353,7 +360,7 @@ export const StepDefineForm: FC<Props> = React.memo(({ overrides = {}, onChange
};

const applyAdvancedPivotEditorChanges = () => {
const pivotConfig = JSON.parse(advancedEditorConfig);
const pivotConfig = JSON.parse(convertToJson(advancedEditorConfig));

const newGroupByList: PivotGroupByConfigDict = {};
if (pivotConfig !== undefined && pivotConfig.group_by !== undefined) {
Expand Down Expand Up @@ -388,10 +395,8 @@ export const StepDefineForm: FC<Props> = React.memo(({ overrides = {}, onChange
});
}
setAggList(newAggList);
const prettyPivotConfig = JSON.stringify(pivotConfig, null, 2);

setAdvancedEditorConfig(prettyPivotConfig);
setAdvancedEditorConfigLastApplied(prettyPivotConfig);
setAdvancedEditorConfigLastApplied(advancedEditorConfig);
setAdvancedPivotEditorApplyButtonEnabled(false);
};

Expand Down Expand Up @@ -459,13 +464,11 @@ export const StepDefineForm: FC<Props> = React.memo(({ overrides = {}, onChange
pivotAggsArr
);

const stringifiedPivotConfigUpdate = JSON.stringify(previewRequestUpdate.pivot, null, 2);
const stringifiedSourceConfigUpdate = JSON.stringify(
previewRequestUpdate.source.query,
null,
2
);
setAdvancedEditorConfig(stringifiedPivotConfigUpdate);
setAdvancedEditorSourceConfig(stringifiedSourceConfigUpdate);

onChange({
Expand Down Expand Up @@ -730,7 +733,7 @@ export const StepDefineForm: FC<Props> = React.memo(({ overrides = {}, onChange
>
<EuiPanel grow={false} paddingSize="none">
<EuiCodeEditor
mode="json"
mode={xJsonMode}
width="100%"
value={advancedEditorConfig}
onChange={(d: string) => {
Expand All @@ -745,7 +748,7 @@ export const StepDefineForm: FC<Props> = React.memo(({ overrides = {}, onChange
// Try to parse the string passed on from the editor.
// If parsing fails, the "Apply"-Button will be disabled
try {
JSON.parse(d);
JSON.parse(convertToJson(d));
setAdvancedPivotEditorApplyButtonEnabled(true);
} catch (e) {
setAdvancedPivotEditorApplyButtonEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jest.mock('react', () => {
return { ...r, memo: (x: any) => x };
});

jest.mock('../../../../../shared_imports');

describe('Transform: <DefinePivotSummary />', () => {
test('Minimal initialization', () => {
const groupBy: PivotGroupByConfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { CreateTransformButton } from './create_transform_button';

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List <CreateTransformButton />', () => {
test('Minimal initialization', () => {
const wrapper = shallow(<CreateTransformButton onClick={jest.fn()} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import transformListRow from '../../../../common/__mocks__/transform_list_row.js

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List Actions <DeleteAction />', () => {
test('Minimal initialization', () => {
const Providers = getAppProviders(createPublicShim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import transformListRow from '../../../../common/__mocks__/transform_list_row.js

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List Actions <StartAction />', () => {
test('Minimal initialization', () => {
const Providers = getAppProviders(createPublicShim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import transformListRow from '../../../../common/__mocks__/transform_list_row.js

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List Actions <StopAction />', () => {
test('Minimal initialization', () => {
const Providers = getAppProviders(createPublicShim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { getActions } from './actions';

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List Actions', () => {
test('getActions()', () => {
const actions = getActions({ forceDisable: false });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { getColumns } from './columns';

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Job List Columns', () => {
test('getColumns()', () => {
const columns = getColumns([], () => {}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ExpandedRow } from './expanded_row';

import transformListRow from '../../../../common/__mocks__/transform_list_row.json';

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List <ExpandedRow />', () => {
// Set timezone to US/Eastern for consistent test results.
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { TransformList } from './transform_list';

jest.mock('ui/new_platform');

jest.mock('../../../../../shared_imports');

describe('Transform: Transform List <TransformList />', () => {
test('Minimal initialization', () => {
const wrapper = shallow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jest.mock('ui/timefilter', () => {
return {};
});

jest.mock('../../../shared_imports');

describe('Transform: <TransformManagementSection />', () => {
test('Minimal initialization', () => {
const wrapper = shallow(<TransformManagementSection />);
Expand Down
6 changes: 6 additions & 0 deletions x-pack/legacy/plugins/transform/public/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { XJsonMode } from '../../../../plugins/es_ui_shared/console_lang/ace/modes/x_json';
export {
collapseLiteralStrings,
expandLiteralStrings,
} from '../../../../../src/plugins/es_ui_shared/console_lang/lib';

export {
SendRequestConfig,
SendRequestResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

// @ts-ignore
import src from '!!raw-loader!./worker.js';

export const workerModule = {
Expand Down

0 comments on commit 25b3618

Please sign in to comment.