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 24, 2023
1 parent 2b74e3f commit bfb9c69
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import React, { useState } from 'react';
import { get } from 'lodash';
import AssociatedDetectors from '../AssociatedDetectors/containers/AssociatedDetectors';
import { getEmbeddable } from '../../../../public/services';
import AddAnomalyDetector from '../CreateAnomalyDetector/AddAnomalyDetector';
import { DetectorListItem } from '../../../../public/models/interfaces';
import AddAnomalyDetector from '../CreateAnomalyDetector';

const AnywhereParentFlyout = ({ startingFlyout, ...props }) => {
const embeddable = getEmbeddable().getEmbeddableFactory;
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 @@ -271,6 +271,59 @@ function AddAnomalyDetector({ embeddable, closeFlyout, mode, setMode }) {
historical: false,
};

const handleAssociate = async (detector: DetectorListItem) => {
enum VisLayerTypes {
PointInTimeEvents = 'PointInTimeEvents',
}

const fn = {
type: VisLayerTypes.PointInTimeEvents,
name: 'overlay_anomalies',
args: {
detectorId: detector.id,
},
} as VisLayerExpressionFn;

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

const savedObjectToCreate = {
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) => {
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) => {
notifications.toasts.addDanger(
prettifyErrorMessage(
`Error associating selected detector: ${error}`
)
);
});
})
.catch((error) => {
notifications.toasts.addDanger(
prettifyErrorMessage(`Error associating selected detector: ${error}`)
);
});
};

return (
<div className="add-anomaly-detector">
<Formik
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 All @@ -256,7 +246,7 @@ export function AssociateExisting(
<EuiHorizontalRule margin="m" />
<ul className="associate-existing__detector-details">
{[
['Indexes', (detector) => detector.indices],
['Indices', (detector) => detector.indices],
[
'Anomalies last 24 hours',
(detector) => detector.totalAnomalies,
Expand Down

0 comments on commit bfb9c69

Please sign in to comment.