From 5ffac3401576dca946d26f40dd965cab88b5dadb Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 21 Jul 2020 14:28:10 +0100 Subject: [PATCH] [ML] Handling data recognizer saved object errors (#72447) * [ML] Handling data recognizer saved object errors * adding text for unknown errors * fixing typos --- .../plugins/ml/common/types/capabilities.ts | 2 +- x-pack/plugins/ml/common/types/modules.ts | 1 + .../recognize/components/kibana_objects.tsx | 7 ++++- .../models/data_recognizer/data_recognizer.ts | 30 +++++++++++++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/ml/common/types/capabilities.ts b/x-pack/plugins/ml/common/types/capabilities.ts index b46dd87eec15f..f2177b0a3572f 100644 --- a/x-pack/plugins/ml/common/types/capabilities.ts +++ b/x-pack/plugins/ml/common/types/capabilities.ts @@ -81,7 +81,7 @@ export function getPluginPrivileges() { catalogue: [PLUGIN_ID], savedObject: { all: [], - read: ['index-pattern', 'search'], + read: ['index-pattern', 'dashboard', 'search', 'visualization'], }, }; diff --git a/x-pack/plugins/ml/common/types/modules.ts b/x-pack/plugins/ml/common/types/modules.ts index b476762f6efca..bfa7e38332c1b 100644 --- a/x-pack/plugins/ml/common/types/modules.ts +++ b/x-pack/plugins/ml/common/types/modules.ts @@ -30,6 +30,7 @@ export interface KibanaObject { title: string; config: KibanaObjectConfig; exists?: boolean; + error?: any; } export interface KibanaObjects { diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/recognize/components/kibana_objects.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/recognize/components/kibana_objects.tsx index 4954b44bf8842..f8ca7926ad7d6 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/recognize/components/kibana_objects.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/recognize/components/kibana_objects.tsx @@ -46,7 +46,7 @@ export const KibanaObjects: FC = memo(
    - {kibanaObjects.map(({ id, title, success, exists }, i) => ( + {kibanaObjects.map(({ id, title, success, exists, error }, i) => (
  • @@ -55,6 +55,11 @@ export const KibanaObjects: FC = memo( {title} + {success === false && error !== undefined && ( + + {error.message} + + )} {exists && ( diff --git a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts index 521d04159ca7a..21e178dcc7e76 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts +++ b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts @@ -93,6 +93,7 @@ export interface RecognizeResult { interface ObjectExistResult { id: string; type: string; + exists?: boolean; } interface ObjectExistResponse { @@ -493,7 +494,13 @@ export class DataRecognizer { // update the exists flag in the results this.updateKibanaResults(results.kibana, savedObjects); // create the savedObjects - saveResults.savedObjects = await this.saveKibanaObjects(savedObjects); + try { + saveResults.savedObjects = await this.saveKibanaObjects(savedObjects); + } catch (error) { + // only one error is returned for the bulk create saved object request + // so populate every saved object with the same error. + this.populateKibanaResultErrors(results.kibana, error.output?.payload); + } } // merge all the save results this.updateResults(results, saveResults); @@ -610,7 +617,26 @@ export class DataRecognizer { (type) => { kibanaSaveResults[type].forEach((resultItem) => { const i = objectExistResults.find((o) => o.id === resultItem.id && o.type === type); - resultItem.exists = i !== undefined; + resultItem.exists = i !== undefined && i.exists; + }); + } + ); + } + + // add an error object to every kibana saved object, + // if it doesn't already exist. + populateKibanaResultErrors( + kibanaSaveResults: DataRecognizerConfigResponse['kibana'], + error: any + ) { + const errorObj = + error === undefined ? { message: 'Unknown error when creating saved object' } : error; + (Object.keys(kibanaSaveResults) as Array).forEach( + (type) => { + kibanaSaveResults[type].forEach((resultItem) => { + if (resultItem.exists === false) { + resultItem.error = errorObj; + } }); } );