From 7bb298916440df3007f1a737ceeff4a27dd293bd Mon Sep 17 00:00:00 2001 From: Melissa Alvarez Date: Fri, 3 Apr 2020 16:37:32 -0400 Subject: [PATCH] [ML] DF Analytics - ensure destination index pattern created (#62450) * ensure destinationIndex name is defined * set array for destIndex as invalid * update type * reset destIndex already exists error when updating advanced editor --- x-pack/plugins/ml/common/util/es_utils.ts | 1 + .../hooks/use_create_analytics_form/reducer.ts | 13 +++++++++++++ .../use_create_analytics_form.ts | 11 ++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/ml/common/util/es_utils.ts b/x-pack/plugins/ml/common/util/es_utils.ts index bed7ba8bc7736..ff632a60dd516 100644 --- a/x-pack/plugins/ml/common/util/es_utils.ts +++ b/x-pack/plugins/ml/common/util/es_utils.ts @@ -26,6 +26,7 @@ function isValidIndexNameLength(indexName: string) { // https://github.com/elastic/elasticsearch/blob/master/docs/reference/indices/create-index.asciidoc export function isValidIndexName(indexName: string) { return ( + typeof indexName === 'string' && // Lowercase only indexName === indexName.toLowerCase() && // Cannot include \, /, *, ?, ", <, >, |, space character, comma, #, : diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/reducer.ts b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/reducer.ts index 28d8afbcd88cc..4f3d2b6a96490 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/reducer.ts +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/reducer.ts @@ -233,6 +233,17 @@ export const validateAdvancedEditor = (state: State): State => { ), message: '', }); + } else if (destinationIndexPatternTitleExists && !createIndexPattern) { + state.advancedEditorMessages.push({ + error: i18n.translate( + 'xpack.ml.dataframe.analytics.create.advancedEditorMessage.destinationIndexNameExistsWarn', + { + defaultMessage: + 'An index with this destination index name already exists. Be aware that running this analytics job will modify this destination index.', + } + ), + message: '', + }); } else if (!destinationIndexNameValid) { state.advancedEditorMessages.push({ error: i18n.translate( @@ -276,6 +287,8 @@ export const validateAdvancedEditor = (state: State): State => { }); } + state.form.destinationIndexPatternTitleExists = destinationIndexPatternTitleExists; + state.isValid = maxDistinctValuesError === undefined && excludesValid && diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/use_create_analytics_form.ts b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/use_create_analytics_form.ts index 44bfc0c5a472c..2478dbf7cf63d 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/use_create_analytics_form.ts +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/hooks/use_create_analytics_form/use_create_analytics_form.ts @@ -47,7 +47,8 @@ export const useCreateAnalyticsForm = (): CreateAnalyticsFormProps => { const { refresh } = useRefreshAnalyticsList(); const { form, jobConfig, isAdvancedEditorEnabled } = state; - const { createIndexPattern, destinationIndex, jobId } = form; + const { createIndexPattern, jobId } = form; + let { destinationIndex } = form; const addRequestMessage = (requestMessage: FormMessage) => dispatch({ type: ACTION.ADD_REQUEST_MESSAGE, requestMessage }); @@ -90,9 +91,13 @@ export const useCreateAnalyticsForm = (): CreateAnalyticsFormProps => { resetRequestMessages(); setIsModalButtonDisabled(true); - const analyticsJobConfig = isAdvancedEditorEnabled + const analyticsJobConfig = (isAdvancedEditorEnabled ? jobConfig - : getJobConfigFromFormState(form); + : getJobConfigFromFormState(form)) as DataFrameAnalyticsConfig; + + if (isAdvancedEditorEnabled) { + destinationIndex = analyticsJobConfig.dest.index; + } try { await ml.dataFrameAnalytics.createDataFrameAnalytics(jobId, analyticsJobConfig);