Skip to content

Commit

Permalink
[ML] Handling data recognizer saved object errors (#72447)
Browse files Browse the repository at this point in the history
* [ML] Handling data recognizer saved object errors

* adding text for unknown errors

* fixing typos
  • Loading branch information
jgowdyelastic committed Jul 21, 2020
1 parent 9cf319d commit 5ffac34
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/common/types/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function getPluginPrivileges() {
catalogue: [PLUGIN_ID],
savedObject: {
all: [],
read: ['index-pattern', 'search'],
read: ['index-pattern', 'dashboard', 'search', 'visualization'],
},
};

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/ml/common/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface KibanaObject {
title: string;
config: KibanaObjectConfig;
exists?: boolean;
error?: any;
}

export interface KibanaObjects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const KibanaObjects: FC<KibanaObjectItemProps> = memo(
</EuiTitle>
<EuiSpacer size="s" />
<ul>
{kibanaObjects.map(({ id, title, success, exists }, i) => (
{kibanaObjects.map(({ id, title, success, exists, error }, i) => (
<li key={id}>
<EuiFlexGroup alignItems="center" gutterSize="s">
<EuiFlexItem>
Expand All @@ -55,6 +55,11 @@ export const KibanaObjects: FC<KibanaObjectItemProps> = memo(
<EuiText size="s" color={exists ? 'subdued' : 'secondary'}>
{title}
</EuiText>
{success === false && error !== undefined && (
<EuiText size="xs" color="danger">
{error.message}
</EuiText>
)}
</EuiFlexItem>
{exists && (
<EuiFlexItem grow={false}>
Expand Down
30 changes: 28 additions & 2 deletions x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface RecognizeResult {
interface ObjectExistResult {
id: string;
type: string;
exists?: boolean;
}

interface ObjectExistResponse {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<keyof DataRecognizerConfigResponse['kibana']>).forEach(
(type) => {
kibanaSaveResults[type].forEach((resultItem) => {
if (resultItem.exists === false) {
resultItem.error = errorObj;
}
});
}
);
Expand Down

0 comments on commit 5ffac34

Please sign in to comment.