diff --git a/public/components/FeatureAnywhereContextMenu/AnywhereParentFlyout/AnywhereParentFlyout.tsx b/public/components/FeatureAnywhereContextMenu/AnywhereParentFlyout/AnywhereParentFlyout.tsx index 967814ef..8e77299d 100644 --- a/public/components/FeatureAnywhereContextMenu/AnywhereParentFlyout/AnywhereParentFlyout.tsx +++ b/public/components/FeatureAnywhereContextMenu/AnywhereParentFlyout/AnywhereParentFlyout.tsx @@ -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; @@ -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 ( { + 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 (
{ @@ -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', '') ) ); @@ -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 []; @@ -210,8 +199,7 @@ export function AssociateExisting( Eligible detectors don't include high-cardinality detectors. - {!existingDetectorsAvailableToAssociate && } - {existingDetectorsAvailableToAssociate && ( + {existingDetectorsAvailableToAssociate ? ( + ) : ( + )} {detector && ( @@ -256,7 +246,7 @@ export function AssociateExisting(
    {[ - ['Indexes', (detector) => detector.indices], + ['Indices', (detector) => detector.indices], [ 'Anomalies last 24 hours', (detector) => detector.totalAnomalies,