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

[Runtime fields] Add support in index template #84184

Merged
merged 41 commits into from Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1ad89cc
Revert changes to add runtime field to mappings properties
sebelga Nov 17, 2020
3ff47f9
Update state and reducer with new actions
sebelga Nov 20, 2020
48ab889
Add tab to mappings editor for runtime fields
sebelga Nov 20, 2020
bb4e7f7
Export flyout content props from runtime fields
sebelga Nov 20, 2020
4c657d9
Add default ownFocus "true" to global flyout
sebelga Nov 20, 2020
8514aac
Use NormalizedRuntimeField in state
sebelga Nov 23, 2020
7952308
List runtime fields from state
sebelga Nov 23, 2020
9e7be04
Delete runtime field from list
sebelga Nov 23, 2020
916d9d6
Edit runtime field
sebelga Nov 23, 2020
29c8fe9
List provided runtime fields
sebelga Nov 23, 2020
9c33b0b
Put script inside source prop
sebelga Nov 23, 2020
3085f50
Add create field button below list
sebelga Nov 23, 2020
5f8d8c9
Forward runtime fields to parent component
sebelga Nov 24, 2020
d6f6452
Improve error description when creating or updating index template
sebelga Nov 24, 2020
a83d6aa
Enhance error from simulate template
sebelga Nov 24, 2020
2c39d68
Use ConfigProvider in mappings editor to provide docLinks to runtime …
sebelga Nov 24, 2020
9b0a240
Fix tests and TS issues
sebelga Nov 24, 2020
0327775
Fix test
sebelga Nov 25, 2020
e32fe7e
Add badge to indicate when a field is shadowed
sebelga Nov 25, 2020
7d67daa
Add copy description in runtime fields list
sebelga Nov 25, 2020
36e3bb9
Validate that duplicate runtime fields are not allowed + callout when…
sebelga Nov 25, 2020
fcb1e0e
Fix i18n issue
sebelga Nov 26, 2020
ed4e84d
Add Api integration tests for the ES error parser
sebelga Nov 26, 2020
800d568
Fix issue validating duplicates when editing a field
sebelga Nov 26, 2020
8919c37
Add missing aria-label to form
sebelga Nov 26, 2020
0148830
Update README.md
sebelga Nov 26, 2020
4f597c3
Update loadEditor handler to accept ctx object
sebelga Nov 26, 2020
b25fc70
Add es_error_parser unit test
sebelga Nov 26, 2020
a5655a2
Merge remote-tracking branch 'upstream/master' into runtime-fields-in…
sebelga Nov 26, 2020
fc20640
Merge remote-tracking branch 'upstream/master' into runtime-fields-in…
sebelga Nov 30, 2020
cd2dabf
Remove commented code
sebelga Nov 30, 2020
8a65bc8
Update copy text
sebelga Nov 30, 2020
5f1fb37
Merge branch 'master' into runtime-fields-in-mappings-2
kibanamachine Dec 2, 2020
59a01db
Update x-pack/plugins/index_management/public/application/components/…
sebelga Dec 3, 2020
8fe6dd4
Apply suggestions from code review
sebelga Dec 3, 2020
c1fc24a
Change callout color to warning for shadowing field
sebelga Dec 3, 2020
446c2cf
Update link for runtime fields painless syntax
sebelga Dec 3, 2020
b3a6cd2
Mark script form field as optional
sebelga Dec 3, 2020
4fa275a
Merge remote-tracking branch 'upstream/master' into runtime-fields-in…
sebelga Dec 3, 2020
89b6dbf
Fix tests
sebelga Dec 3, 2020
ea928f6
Fix linting issue
sebelga Dec 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,42 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { parseEsError } from './es_error_parser';

describe('ES error parser', () => {
test('should return all the cause of the error', () => {
const esError = `{
"error": {
"reason": "Houston we got a problem",
"caused_by": {
"reason": "First reason",
"caused_by": {
"reason": "Second reason",
"caused_by": {
"reason": "Third reason"
}
}
}
}
}`;

const parsedError = parseEsError(esError);
expect(parsedError.message).toEqual('Houston we got a problem');
expect(parsedError.cause).toEqual(['First reason', 'Second reason', 'Third reason']);
});
});
@@ -0,0 +1,52 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to flag that this functionality is similar to what is in x-pack/plugins/ingest_pipelines/public/application/components/pipeline_form/pipeline_form_error/error_utils.ts, but that code has an added, perhaps unnecessary, runtime type guard since the assumption was there are no guarantees on what the error object will be (unknown).

I see this is used on the server side and we guard with the "isEsError" check.

We can replace flattenErrorsTree in ingest pipelines with this logic to get maximum re-use because this should also work for the errors returned for processors if this is the standard es error shape. I think we still need to guard in ingest pipelines with the runtime type check though.

Let me know what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed it is similar but much simpler implementation :) If you think your use case deserves all the complexity of io-ts and fp-ts then it's better to have separate implementations. My goal was "just" to read the "caused_by" recursively from the ES error. I put the logic inside a try/catch so it should not break if ES changes the contract.

I personally think that sometimes too much typings can get in the way and simplicity is often a better approach. That's just my personal preference though. 😊

* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

interface ParsedError {
message: string;
cause: string[];
}

const getCause = (obj: any = {}, causes: string[] = []): string[] => {
const updated = [...causes];

if (obj.caused_by) {
updated.push(obj.caused_by.reason);

// Recursively find all the "caused by" reasons
return getCause(obj.caused_by, updated);
}

return updated.filter(Boolean);
};

export const parseEsError = (err: string): ParsedError => {
try {
const { error } = JSON.parse(err);
const cause = getCause(error);
return {
message: error.reason,
cause,
};
} catch (e) {
return {
message: err,
cause: [],
};
}
};
Expand Up @@ -19,3 +19,4 @@

export { isEsError } from './is_es_error';
export { handleEsError } from './handle_es_error';
export { parseEsError } from './es_error_parser';
Expand Up @@ -54,7 +54,7 @@ export const GlobalFlyoutProvider: React.FC = ({ children }) => {
const [showFlyout, setShowFlyout] = useState(false);
const [activeContent, setActiveContent] = useState<Content<any> | undefined>(undefined);

const { id, Component, props, flyoutProps } = activeContent ?? {};
const { id, Component, props, flyoutProps, cleanUpFunc } = activeContent ?? {};

const addContent: Context['addContent'] = useCallback((content) => {
setActiveContent((prev) => {
Expand All @@ -77,11 +77,19 @@ export const GlobalFlyoutProvider: React.FC = ({ children }) => {

const removeContent: Context['removeContent'] = useCallback(
(contentId: string) => {
// Note: when we will actually deal with multi content then
// there will be more logic here! :)
if (contentId === id) {
setActiveContent(undefined);

if (cleanUpFunc) {
cleanUpFunc();
}

closeFlyout();
}
},
[id, closeFlyout]
[id, closeFlyout, cleanUpFunc]
);

const mergedFlyoutProps = useMemo(() => {
Expand Down Expand Up @@ -130,14 +138,6 @@ export const useGlobalFlyout = () => {
const contents = useRef<Set<string> | undefined>(undefined);
const { removeContent, addContent: addContentToContext } = ctx;

useEffect(() => {
isMounted.current = true;

return () => {
isMounted.current = false;
};
}, []);

const getContents = useCallback(() => {
if (contents.current === undefined) {
contents.current = new Set();
Expand All @@ -153,6 +153,14 @@ export const useGlobalFlyout = () => {
[getContents, addContentToContext]
);

useEffect(() => {
isMounted.current = true;

return () => {
isMounted.current = false;
};
}, []);

useEffect(() => {
return () => {
if (!isMounted.current) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/es_ui_shared/server/errors/index.ts
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

export { isEsError, handleEsError } from '../../__packages_do_not_import__/errors';
export { isEsError, handleEsError, parseEsError } from '../../__packages_do_not_import__/errors';
2 changes: 1 addition & 1 deletion src/plugins/es_ui_shared/server/index.ts
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

export { isEsError, handleEsError } from './errors';
export { isEsError, handleEsError, parseEsError } from './errors';

/** dummy plugin*/
export function plugin() {
Expand Down
Expand Up @@ -37,6 +37,7 @@ export interface AppDependencies {
setBreadcrumbs: ManagementAppMountParams['setBreadcrumbs'];
uiSettings: CoreSetup['uiSettings'];
urlGenerators: SharePluginStart['urlGenerators'];
docLinks: CoreStart['docLinks'];
}

export const AppContextProvider = ({
Expand Down
Expand Up @@ -9,7 +9,7 @@ import { setup as componentTemplateDetailsSetup } from './component_template_det

export { nextTick, getRandomString, findTestSubject } from '@kbn/test/jest';

export { setupEnvironment, appDependencies } from './setup_environment';
export { setupEnvironment, componentTemplatesDependencies } from './setup_environment';

export const pageHelpers = {
componentTemplateList: { setup: componentTemplatesListSetup },
Expand Down
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../../../../../../../../../src/core/public/mocks';

import { GlobalFlyout } from '../../../../../../../../../../src/plugins/es_ui_shared/public';
import { AppContextProvider } from '../../../../../app_context';
import { MappingsEditorProvider } from '../../../../mappings_editor';
import { ComponentTemplatesProvider } from '../../../component_templates_context';

Expand All @@ -24,7 +25,12 @@ import { API_BASE_PATH } from './constants';
const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });
const { GlobalFlyoutProvider } = GlobalFlyout;

export const appDependencies = {
// We provide the minimum deps required to make the tests pass
const appDependencies = {
docLinks: {} as any,
} as any;

export const componentTemplatesDependencies = {
httpClient: (mockHttpClient as unknown) as HttpSetup,
apiBasePath: API_BASE_PATH,
trackMetric: () => {},
Expand All @@ -44,11 +50,14 @@ export const setupEnvironment = () => {
};

export const WithAppDependencies = (Comp: any) => (props: any) => (
<MappingsEditorProvider>
<ComponentTemplatesProvider value={appDependencies}>
<GlobalFlyoutProvider>
<Comp {...props} />
</GlobalFlyoutProvider>
</ComponentTemplatesProvider>
</MappingsEditorProvider>
<AppContextProvider value={appDependencies}>
<MappingsEditorProvider>
<ComponentTemplatesProvider value={componentTemplatesDependencies}>
<GlobalFlyoutProvider>
<Comp {...props} />
</GlobalFlyoutProvider>
</ComponentTemplatesProvider>
</MappingsEditorProvider>
/
</AppContextProvider>
);
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { appDependencies as componentTemplatesMockDependencies } from './client_integration/helpers';
export { componentTemplatesDependencies as componentTemplatesMockDependencies } from './client_integration/helpers';