forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] AiOps: Action for adding Log Rate analysis embeddable to a dashb…
…oard (elastic#200557) ## Summary Part of: [elastic#197247](elastic#197247) - Added the ability to add a Log Rate Analysis embeddable to a dashboard https://github.com/user-attachments/assets/37efd83e-9196-434d-a80d-9249623f3222 ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
Showing
7 changed files
with
282 additions
and
23 deletions.
There are no files selected for viewing
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
176 changes: 176 additions & 0 deletions
176
...onents/log_rate_analysis/log_rate_analysis_content/log_rate_analysis_attachments_menu.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,176 @@ | ||
/* | ||
* 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 type { SaveModalDashboardProps } from '@kbn/presentation-util-plugin/public'; | ||
import { LazySavedObjectSaveModalDashboard } from '@kbn/presentation-util-plugin/public'; | ||
import { withSuspense } from '@kbn/shared-ux-utility'; | ||
import React, { useState, useCallback, useMemo } from 'react'; | ||
import { useTimeRangeUpdates } from '@kbn/ml-date-picker'; | ||
import { EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE } from '@kbn/aiops-log-rate-analysis/constants'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import type { EuiContextMenuProps } from '@elastic/eui'; | ||
import { | ||
EuiButton, | ||
EuiButtonIcon, | ||
EuiContextMenu, | ||
EuiFlexItem, | ||
EuiForm, | ||
EuiFormRow, | ||
EuiPanel, | ||
EuiPopover, | ||
EuiSpacer, | ||
EuiSwitch, | ||
} from '@elastic/eui'; | ||
import { useDataSource } from '../../../hooks/use_data_source'; | ||
import type { LogRateAnalysisEmbeddableState } from '../../../embeddables/log_rate_analysis/types'; | ||
import { useAiopsAppContext } from '../../../hooks/use_aiops_app_context'; | ||
|
||
const SavedObjectSaveModalDashboard = withSuspense(LazySavedObjectSaveModalDashboard); | ||
|
||
export const LogRateAnalysisAttachmentsMenu = () => { | ||
const { | ||
application: { capabilities }, | ||
embeddable, | ||
} = useAiopsAppContext(); | ||
const { dataView } = useDataSource(); | ||
|
||
const [applyTimeRange, setApplyTimeRange] = useState(false); | ||
const [isActionMenuOpen, setIsActionMenuOpen] = useState(false); | ||
const [dashboardAttachmentReady, setDashboardAttachmentReady] = useState(false); | ||
|
||
const timeRange = useTimeRangeUpdates(); | ||
|
||
const canEditDashboards = capabilities.dashboard.createNew; | ||
|
||
const onSave: SaveModalDashboardProps['onSave'] = useCallback( | ||
({ dashboardId, newTitle, newDescription }) => { | ||
const stateTransfer = embeddable!.getStateTransfer(); | ||
|
||
const embeddableInput: Partial<LogRateAnalysisEmbeddableState> = { | ||
title: newTitle, | ||
description: newDescription, | ||
dataViewId: dataView.id, | ||
hidePanelTitles: false, | ||
...(applyTimeRange && { timeRange }), | ||
}; | ||
|
||
const state = { | ||
input: embeddableInput, | ||
type: EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE, | ||
}; | ||
|
||
const path = dashboardId === 'new' ? '#/create' : `#/view/${dashboardId}`; | ||
|
||
stateTransfer.navigateToWithEmbeddablePackage('dashboards', { state, path }); | ||
}, | ||
[dataView.id, embeddable, applyTimeRange, timeRange] | ||
); | ||
|
||
const panels = useMemo<Exclude<EuiContextMenuProps['panels'], undefined>>(() => { | ||
return [ | ||
{ | ||
id: 'attachMainPanel', | ||
size: 's', | ||
items: [ | ||
...(canEditDashboards | ||
? [ | ||
{ | ||
name: i18n.translate('xpack.aiops.logRateAnalysis.addToDashboardTitle', { | ||
defaultMessage: 'Add to dashboard', | ||
}), | ||
panel: 'attachToDashboardPanel', | ||
'data-test-subj': 'aiopsLogRateAnalysisAttachToDashboardButton', | ||
}, | ||
] | ||
: []), | ||
], | ||
}, | ||
{ | ||
id: 'attachToDashboardPanel', | ||
size: 's', | ||
title: i18n.translate('xpack.aiops.logRateAnalysis.attachToDashboardTitle', { | ||
defaultMessage: 'Add to dashboard', | ||
}), | ||
content: ( | ||
<EuiPanel paddingSize="s"> | ||
<EuiSpacer size="s" /> | ||
<EuiForm> | ||
<EuiFormRow> | ||
<EuiSwitch | ||
label={i18n.translate('xpack.aiops.logRateAnalysis.applyTimeRangeLabel', { | ||
defaultMessage: 'Apply time range', | ||
})} | ||
checked={applyTimeRange} | ||
onChange={(e) => setApplyTimeRange(e.target.checked)} | ||
/> | ||
</EuiFormRow> | ||
<EuiSpacer size="s" /> | ||
<EuiButton | ||
size="s" | ||
data-test-subj="aiopsLogRateAnalysisAttachToDashboardSubmitButton" | ||
fill | ||
fullWidth | ||
type={'submit'} | ||
onClick={() => { | ||
setIsActionMenuOpen(false); | ||
setDashboardAttachmentReady(true); | ||
}} | ||
> | ||
<FormattedMessage | ||
id="xpack.aiops.logRateAnalysis.attachToDashboardSubmitButtonLabel" | ||
defaultMessage="Add to dashboard" | ||
/> | ||
</EuiButton> | ||
</EuiForm> | ||
</EuiPanel> | ||
), | ||
}, | ||
]; | ||
}, [canEditDashboards, applyTimeRange]); | ||
|
||
return ( | ||
<> | ||
{!!panels[0]?.items?.length && ( | ||
<EuiFlexItem> | ||
<EuiPopover | ||
button={ | ||
<EuiButtonIcon | ||
data-test-subj="aiopsLogRateAnalysisAttachmentsMenuButton" | ||
aria-label={i18n.translate('xpack.aiops.logRateAnalysis.attachmentsMenuAriaLabel', { | ||
defaultMessage: 'Attachments', | ||
})} | ||
iconType="boxesHorizontal" | ||
color="text" | ||
onClick={() => setIsActionMenuOpen(!isActionMenuOpen)} | ||
/> | ||
} | ||
isOpen={isActionMenuOpen} | ||
closePopover={() => setIsActionMenuOpen(false)} | ||
panelPaddingSize="none" | ||
anchorPosition="downRight" | ||
> | ||
<EuiContextMenu panels={panels} initialPanelId="attachMainPanel" /> | ||
</EuiPopover> | ||
</EuiFlexItem> | ||
)} | ||
{dashboardAttachmentReady ? ( | ||
<SavedObjectSaveModalDashboard | ||
canSaveByReference={false} | ||
objectType={i18n.translate('xpack.aiops.logRateAnalysis.objectTypeLabel', { | ||
defaultMessage: 'Log rate analysis', | ||
})} | ||
documentInfo={{ | ||
title: 'Log rate analysis', | ||
}} | ||
onClose={() => setDashboardAttachmentReady(false)} | ||
onSave={onSave} | ||
/> | ||
) : null} | ||
</> | ||
); | ||
}; |
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
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