Skip to content

Commit

Permalink
added notifications service as a getter-setter
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <[email protected]>
  • Loading branch information
amitgalitz committed May 22, 2023
1 parent e62cea8 commit f187620
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import { getColumns } from '../utils/helpers';
import { useDispatch, useSelector } from 'react-redux';
import { AppState } from '../../../../redux/reducers';
import { DetectorListItem } from '../../../../models/interfaces';
import { getSavedFeatureAnywhereLoader } from '../../../../services';
import {
getSavedFeatureAnywhereLoader,
getNotifications,
} from '../../../../services';
import {
GET_ALL_DETECTORS_QUERY_PARAMS,
SINGLE_DETECTOR_NOT_FOUND_MSG,
Expand All @@ -47,7 +50,7 @@ interface ConfirmModalState {
affectedDetector: DetectorListItem;
}

function AssociatedDetectors({ embeddable, closeFlyout, core, setMode }) {
function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
const dispatch = useDispatch();
const allDetectors = useSelector((state: AppState) => state.ad.detectorList);
const isRequestingFromES = useSelector(
Expand Down Expand Up @@ -81,13 +84,15 @@ function AssociatedDetectors({ embeddable, closeFlyout, core, setMode }) {
// Establish savedObjectLoader for all operations on vis_augment saved objects
const savedObjectLoader: SavedObjectLoader = getSavedFeatureAnywhereLoader();

const notifications = getNotifications();

useEffect(() => {
if (
errorGettingDetectors &&
!errorGettingDetectors.includes(SINGLE_DETECTOR_NOT_FOUND_MSG)
) {
console.error(errorGettingDetectors);
core.notifications.toasts.addDanger(
notifications.toasts.addDanger(
typeof errorGettingDetectors === 'string' &&
errorGettingDetectors.includes(NO_PERMISSIONS_KEY_WORD)
? prettifyErrorMessage(errorGettingDetectors)
Expand Down Expand Up @@ -141,7 +146,7 @@ function AssociatedDetectors({ embeddable, closeFlyout, core, setMode }) {
}
})
.catch((error) => {
core.notifications.toasts.addDanger(
notifications.toasts.addDanger(
prettifyErrorMessage(`Unable to fetch associated detectors: ${error}`)
);
});
Expand Down Expand Up @@ -178,10 +183,14 @@ function AssociatedDetectors({ embeddable, closeFlyout, core, setMode }) {
await savedObjectLoader.findAll().then(async (resp: any) => {
if (resp != undefined) {
// gets all the saved object for this visualization
const savedAugmentForThisVisualization: ISavedAugmentVis[] =
get(resp, 'hits', [] as ISavedAugmentVis[]).filter(
(savedObj: ISavedAugmentVis[]) => get(savedObj, 'visId', '') === embeddable.vis.id
);
const savedAugmentForThisVisualization: ISavedAugmentVis[] = get(
resp,
'hits',
[] as ISavedAugmentVis[]
).filter(
(savedObj: ISavedAugmentVis[]) =>
get(savedObj, 'visId', '') === embeddable.vis.id
);

// find saved augment object matching detector we want to unlink
// There should only be one detector and vis pairing
Expand All @@ -192,14 +201,14 @@ function AssociatedDetectors({ embeddable, closeFlyout, core, setMode }) {
await savedObjectLoader
.delete(get(savedAugmentToUnlink, 'id', ''))
.then(async (resp: any) => {
core.notifications.toasts.addSuccess({
notifications.toasts.addSuccess({
title: `Association removed between the ${detectorToUnlink.name}
and the ${embeddableTitle} visualization`,
text: "The detector's anomalies do not appear on the visualization. Refresh your dashboard to update the visualization",
});
})
.catch((error) => {
core.notifications.toasts.addDanger(
notifications.toasts.addDanger(
prettifyErrorMessage(
`Error unlinking selected detector: ${error}`
)
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { overlayAnomaliesFunction } from './expressions/overlay_anomalies';
import {
setClient,
setEmbeddable,
setNotifications,
setOverlays,
setSavedFeatureAnywhereLoader,
} from './services';
Expand Down Expand Up @@ -92,6 +93,7 @@ export class AnomalyDetectionOpenSearchDashboardsPlugin
setEmbeddable(embeddable);
setOverlays(core.overlays);
setSavedFeatureAnywhereLoader(visAugmenter.savedAugmentVisLoader);
setNotifications(core.notifications);
return {};
}
}
13 changes: 10 additions & 3 deletions public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { CoreStart, OverlayStart } from '../../../src/core/public';
import {
CoreStart,
NotificationsStart,
OverlayStart,
} from '../../../src/core/public';
import { EmbeddableStart } from '../../../src/plugins/embeddable/public';
import { createGetterSetter } from '../../../src/plugins/opensearch_dashboards_utils/public';
import { SavedObjectLoader } from '../../../src/plugins/saved_objects/public';
Expand All @@ -14,8 +18,11 @@ export const [getSavedFeatureAnywhereLoader, setSavedFeatureAnywhereLoader] =
export const [getClient, setClient] =
createGetterSetter<CoreStart['http']>('http');

export const [getEmbeddable, setEmbeddable] =
export const [getEmbeddable, setEmbeddable] =
createGetterSetter<EmbeddableStart>('Embeddable');

export const [getOverlays, setOverlays] =
export const [getOverlays, setOverlays] =
createGetterSetter<OverlayStart>('Overlays');

export const [getNotifications, setNotifications] =
createGetterSetter<NotificationsStart>('Notifications');

0 comments on commit f187620

Please sign in to comment.