-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Adds reset action to transforms management.
- Loading branch information
Showing
15 changed files
with
554 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/transform/common/api_schemas/reset_transforms.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema, TypeOf } from '@kbn/config-schema'; | ||
|
||
import { transformStateSchema, ResponseStatus } from './common'; | ||
|
||
export const resetTransformsRequestSchema = schema.object({ | ||
/** | ||
* Reset Transforms | ||
*/ | ||
transformsInfo: schema.arrayOf( | ||
schema.object({ | ||
id: schema.string(), | ||
state: transformStateSchema, | ||
}) | ||
), | ||
}); | ||
|
||
export type ResetTransformsRequestSchema = TypeOf<typeof resetTransformsRequestSchema>; | ||
|
||
export interface ResetTransformStatus { | ||
transformReset: ResponseStatus; | ||
} | ||
|
||
export interface ResetTransformsResponseSchema { | ||
[key: string]: ResetTransformStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
x-pack/plugins/transform/public/app/hooks/use_reset_transform.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { toMountPoint } from '../../../../../../src/plugins/kibana_react/public'; | ||
import type { | ||
ResetTransformStatus, | ||
ResetTransformsRequestSchema, | ||
} from '../../../common/api_schemas/reset_transforms'; | ||
import { isResetTransformsResponseSchema } from '../../../common/api_schemas/type_guards'; | ||
import { getErrorMessage } from '../../../common/utils/errors'; | ||
import { useAppDependencies, useToastNotifications } from '../app_dependencies'; | ||
import { REFRESH_TRANSFORM_LIST_STATE, refreshTransformList$ } from '../common'; | ||
import { ToastNotificationText } from '../components'; | ||
import { useApi } from './use_api'; | ||
|
||
type SuccessCountField = keyof Omit<ResetTransformStatus, 'destinationIndex'>; | ||
|
||
export const useResetTransforms = () => { | ||
const { overlays, theme } = useAppDependencies(); | ||
const toastNotifications = useToastNotifications(); | ||
const api = useApi(); | ||
|
||
return async (reqBody: ResetTransformsRequestSchema) => { | ||
const results = await api.resetTransforms(reqBody); | ||
|
||
if (!isResetTransformsResponseSchema(results)) { | ||
toastNotifications.addDanger({ | ||
title: i18n.translate('xpack.transform.transformList.resetTransformGenericErrorMessage', { | ||
defaultMessage: 'An error occurred calling the API endpoint to reset transforms.', | ||
}), | ||
text: toMountPoint( | ||
<ToastNotificationText | ||
previewTextLength={50} | ||
overlays={overlays} | ||
theme={theme} | ||
text={getErrorMessage(results)} | ||
/>, | ||
{ theme$: theme.theme$ } | ||
), | ||
}); | ||
return; | ||
} | ||
|
||
const isBulk = Object.keys(results).length > 1; | ||
const successCount: Record<SuccessCountField, number> = { | ||
transformReset: 0, | ||
}; | ||
for (const transformId in results) { | ||
// hasOwnProperty check to ensure only properties on object itself, and not its prototypes | ||
if (results.hasOwnProperty(transformId)) { | ||
const status = results[transformId]; | ||
|
||
// if we are only deleting one transform, show the success toast messages | ||
if (!isBulk && status.transformReset) { | ||
if (status.transformReset?.success) { | ||
toastNotifications.addSuccess( | ||
i18n.translate('xpack.transform.transformList.resetTransformSuccessMessage', { | ||
defaultMessage: 'Request to reset transform {transformId} acknowledged.', | ||
values: { transformId }, | ||
}) | ||
); | ||
} | ||
} else { | ||
(Object.keys(successCount) as SuccessCountField[]).forEach((key) => { | ||
if (status[key]?.success) { | ||
successCount[key] = successCount[key] + 1; | ||
} | ||
}); | ||
} | ||
if (status.transformReset?.error) { | ||
const error = status.transformReset.error.reason; | ||
toastNotifications.addDanger({ | ||
title: i18n.translate('xpack.transform.transformList.resetTransformErrorMessage', { | ||
defaultMessage: 'An error occurred resetting the transform {transformId}', | ||
values: { transformId }, | ||
}), | ||
text: toMountPoint( | ||
<ToastNotificationText | ||
previewTextLength={50} | ||
overlays={overlays} | ||
theme={theme} | ||
text={error} | ||
/>, | ||
{ theme$: theme.theme$ } | ||
), | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// if we are deleting multiple transforms, combine the success messages | ||
if (isBulk) { | ||
if (successCount.transformReset > 0) { | ||
toastNotifications.addSuccess( | ||
i18n.translate('xpack.transform.transformList.bulkResetTransformSuccessMessage', { | ||
defaultMessage: | ||
'Successfully reset {count} {count, plural, one {transform} other {transforms}}.', | ||
values: { count: successCount.transformReset }, | ||
}) | ||
); | ||
} | ||
} | ||
|
||
refreshTransformList$.next(REFRESH_TRANSFORM_LIST_STATE.REFRESH); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ugins/transform/public/app/sections/transform_management/components/action_reset/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { useResetAction } from './use_reset_action'; | ||
export { ResetActionModal } from './reset_action_modal'; | ||
export { isResetActionDisabled, ResetActionName } from './reset_action_name'; |
88 changes: 88 additions & 0 deletions
88
...m/public/app/sections/transform_management/components/action_reset/reset_action_modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { FC, useMemo } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EUI_MODAL_CONFIRM_BUTTON, EuiConfirmModal, EuiFlexGroup, EuiSpacer } from '@elastic/eui'; | ||
import { ResetAction } from './use_reset_action'; | ||
import { isManagedTransform } from '../../../../common/managed_transforms_utils'; | ||
import { ManagedTransformsWarningCallout } from '../managed_transforms_callout/managed_transforms_callout'; | ||
|
||
export const ResetActionModal: FC<ResetAction> = ({ | ||
closeModal, | ||
resetAndCloseModal, | ||
items, | ||
shouldForceReset, | ||
}) => { | ||
const hasManagedTransforms = useMemo(() => items.some((t) => isManagedTransform(t)), [items]); | ||
const isBulkAction = items.length > 1; | ||
|
||
const bulkResetModalTitle = i18n.translate('xpack.transform.transformList.bulkResetModalTitle', { | ||
defaultMessage: 'Reset {count} {count, plural, one {transform} other {transforms}}?', | ||
values: { count: items.length }, | ||
}); | ||
const resetModalTitle = i18n.translate('xpack.transform.transformList.resetModalTitle', { | ||
defaultMessage: 'Reset {transformId}?', | ||
values: { transformId: items[0] && items[0].config.id }, | ||
}); | ||
const bulkResetModalContent = ( | ||
<> | ||
<EuiFlexGroup direction="column" gutterSize="none"> | ||
{hasManagedTransforms ? ( | ||
<> | ||
<ManagedTransformsWarningCallout | ||
count={items.length} | ||
action={i18n.translate( | ||
'xpack.transform.transformList.resetManagedTransformDescription', | ||
{ defaultMessage: 'resetting' } | ||
)} | ||
/> | ||
<EuiSpacer /> | ||
</> | ||
) : null} | ||
</EuiFlexGroup> | ||
</> | ||
); | ||
|
||
const resetModalContent = ( | ||
<> | ||
<EuiFlexGroup direction="column" gutterSize="none"> | ||
{hasManagedTransforms ? ( | ||
<> | ||
<ManagedTransformsWarningCallout | ||
count={1} | ||
action={i18n.translate( | ||
'xpack.transform.transformList.resetManagedTransformDescription', | ||
{ defaultMessage: 'deleting' } | ||
)} | ||
/> | ||
<EuiSpacer /> | ||
</> | ||
) : null} | ||
</EuiFlexGroup> | ||
</> | ||
); | ||
|
||
return ( | ||
<EuiConfirmModal | ||
data-test-subj="transformResetModal" | ||
title={isBulkAction === true ? bulkResetModalTitle : resetModalTitle} | ||
onCancel={closeModal} | ||
onConfirm={resetAndCloseModal} | ||
cancelButtonText={i18n.translate('xpack.transform.transformList.resetModalCancelButton', { | ||
defaultMessage: 'Cancel', | ||
})} | ||
confirmButtonText={i18n.translate('xpack.transform.transformList.resetModalResetButton', { | ||
defaultMessage: 'Reset', | ||
})} | ||
defaultFocusedButton={EUI_MODAL_CONFIRM_BUTTON} | ||
buttonColor="danger" | ||
> | ||
{isBulkAction ? bulkResetModalContent : resetModalContent} | ||
</EuiConfirmModal> | ||
); | ||
}; |
Oops, something went wrong.