Skip to content

Commit

Permalink
removed usememo, addressed other comments
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <amgalitz@amazon.com>
  • Loading branch information
amitgalitz committed May 22, 2023
1 parent 26a4467 commit 98867f2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import React, { useState } from 'react';
import { get } from 'lodash';
import AssociatedDetectors from '../AssociatedDetectors/containers/AssociatedDetectors';
import { getEmbeddable } from '../../../../public/services';
import { DetectorListItem } from '../../../../public/models/interfaces';
import AddAnomalyDetector from '../CreateAnomalyDetector';

const AnywhereParentFlyout = ({ startingFlyout, ...props }) => {
Expand All @@ -16,15 +15,13 @@ const AnywhereParentFlyout = ({ startingFlyout, ...props }) => {
];

const [mode, setMode] = useState(startingFlyout);
const [selectedDetector, setSelectedDetector] = useState(
{} as DetectorListItem
);
const [selectedDetector, setSelectedDetector] = useState(undefined);

const AnywhereFlyout = {
create: AddAnomalyDetector,
associated: AssociatedDetectors,
existing: AddAnomalyDetector,
}[mode];
}[mode];

return (
<AnywhereFlyout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import './styles.scss';
import {
createAugmentVisSavedObject,
ISavedAugmentVis,
ISavedPluginResource,
VisLayerExpressionFn,
} from '../../../../../../src/plugins/vis_augmenter/public';
import { useDispatch, useSelector } from 'react-redux';
Expand Down Expand Up @@ -75,6 +76,8 @@ import {
} from '../../../../public/redux/reducers/opensearch';
import AssociateExisting from './AssociateExisting/containers/AssociateExisting';
import { prettifyErrorMessage } from '../../../../server/utils/helpers';
import { ORIGIN_PLUGIN_VIS_LAYER } from '../../../../public/expressions/constants';
import { getNotifications } from '../../../../public/services';

function AddAnomalyDetector({
embeddable,
Expand Down Expand Up @@ -109,6 +112,8 @@ function AddAnomalyDetector({

const title = embeddable.getTitle();

const notifications = getNotifications();

const onAccordionToggle = (key) => {
const newAccordionsOpen = { ...accordionsOpen };
newAccordionsOpen[key] = !accordionsOpen[key];
Expand Down Expand Up @@ -265,33 +270,41 @@ function AddAnomalyDetector({
},
} as VisLayerExpressionFn;

const pluginResource = {
type: 'anomay-detection',
id: detector.id,
} as ISavedPluginResource;

const savedObjectToCreate = {
title: 'test-title',
pluginResourceId: detector.id,
title: embeddable.vis.title,
originPlugin: ORIGIN_PLUGIN_VIS_LAYER,
pluginResource: pluginResource,
visId: embeddable.vis.id,
savedObjectType: 'visualization',
visLayerExpressionFn: fn,
} as ISavedAugmentVis;

createAugmentVisSavedObject(savedObjectToCreate)
.then((savedObject: any) => {
savedObject
.save({})
.then((response: any) => {
core.notifications.toasts.addSuccess({
notifications.toasts.addSuccess({
title: `The ${detector.name} is associated with the ${title} visualization`,
text: "The detector's anomalies do not appear on the visualization. Refresh your dashboard to update the visualization",
});
closeFlyout();
})
.catch((error) => {
core.notifications.toasts.addDanger(
notifications.toasts.addDanger(
prettifyErrorMessage(
`Error associating selected detector: ${error}`
)
);
});
})
.catch((error) => {
core.notifications.toasts.addDanger(
notifications.toasts.addDanger(
prettifyErrorMessage(`Error associating selected detector: ${error}`)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function AssociateExisting(
[]
);
const curDetectorsToDisplayOnList =
getExistingDetectorsAvalableToAssociate(
getExistingDetectorsAvailableToAssociate(
Object.values(allDetectors),
savedAugmentObjectsArr
);
Expand All @@ -110,7 +110,7 @@ export function AssociateExisting(

// cross checks all the detectors that exist with all the savedAugment Objects to only display ones
// that are associated to the current visualization
const getExistingDetectorsAvalableToAssociate = (
const getExistingDetectorsAvailableToAssociate = (
detectors: DetectorListItem[],
savedAugmentObjects: ISavedAugmentVis[]
) => {
Expand All @@ -124,7 +124,7 @@ export function AssociateExisting(
// Map all detector IDs for all the found augmented vis objects
const savedAugmentDetectorsSet = new Set(
savedAugmentForThisVisualization.map((savedObject) =>
get(savedObject, 'pluginResourceId', '')
get(savedObject, 'pluginResource.id', '')
)
);

Expand Down Expand Up @@ -168,19 +168,8 @@ export function AssociateExisting(
existingDetectorsAvailableToAssociate,
]);

const detector = useMemo(
() =>
existingDetectorsAvailableToAssociate &&
associateExistingProps.selectedDetector &&
existingDetectorsAvailableToAssociate.find(
(detector) =>
detector.id === get(associateExistingProps.selectedDetector, 'id', '')
),
[
associateExistingProps.selectedDetector,
existingDetectorsAvailableToAssociate,
]
);
const detector = associateExistingProps.selectedDetector;

const options = useMemo(() => {
if (!existingDetectorsAvailableToAssociate) {
return [];
Expand Down Expand Up @@ -210,8 +199,7 @@ export function AssociateExisting(
<EuiText size="xs" color="subdued" style={{ paddingBottom: '3px' }}>
Eligible detectors don't include high-cardinality detectors.
</EuiText>
{!existingDetectorsAvailableToAssociate && <EuiLoadingSpinner size="l" />}
{existingDetectorsAvailableToAssociate && (
{existingDetectorsAvailableToAssociate ? (
<EuiComboBox
isLoading={isLoading}
id="associate-existing__select"
Expand All @@ -233,6 +221,8 @@ export function AssociateExisting(
singleSelection
placeholder="Search for an anomaly detector"
/>
) : (
<EuiLoadingSpinner size="l" />
)}
<EuiSpacer size="xl" />
{detector && (
Expand Down

0 comments on commit 98867f2

Please sign in to comment.