Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to associate existing detector with a visualization #484

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ 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 AddAnomalyDetector from '../CreateAnomalyDetector';
import { FLYOUT_MODES } from './constants';

const AnywhereParentFlyout = ({ startingFlyout, ...props }) => {
const embeddable = getEmbeddable().getEmbeddableFactory;
Expand All @@ -15,11 +16,12 @@ const AnywhereParentFlyout = ({ startingFlyout, ...props }) => {
];

const [mode, setMode] = useState(startingFlyout);
const [selectedDetectorId, setSelectedDetectorId] = useState();
const [selectedDetector, setSelectedDetector] = useState(undefined);

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

return (
Expand All @@ -29,8 +31,8 @@ const AnywhereParentFlyout = ({ startingFlyout, ...props }) => {
setMode,
mode,
indices,
selectedDetectorId,
setSelectedDetectorId,
selectedDetector,
setSelectedDetector,
}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

//created: Flyout for creating a new anomaly detector from a visualization
//associated: Flyout for listing all the associated detectors to the given visualization
//existing: Flyout for associating existing detectors with the current visualizations
export enum FLYOUT_MODES {
create = 'create',
associated = 'associated',
existing = 'existing',
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DetectorListItem } from '../../../../models/interfaces';
import {
getSavedFeatureAnywhereLoader,
getNotifications,
getUISettings,
} from '../../../../services';
import {
GET_ALL_DETECTORS_QUERY_PARAMS,
Expand All @@ -39,7 +40,11 @@ import {
EmptyAssociatedDetectorMessage,
ConfirmUnlinkDetectorModal,
} from '../components';
import { ISavedAugmentVis } from '../../../../../../../src/plugins/vis_augmenter/public';
import {
ISavedAugmentVis,
SavedAugmentVisLoader,
getAugmentVisSavedObjs,
} from '../../../../../../../src/plugins/vis_augmenter/public';
import { ASSOCIATED_DETECTOR_ACTION } from '../utils/constants';

interface ConfirmModalState {
Expand Down Expand Up @@ -82,8 +87,10 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
);

// Establish savedObjectLoader for all operations on vis_augment saved objects
const savedObjectLoader: SavedObjectLoader = getSavedFeatureAnywhereLoader();
const savedObjectLoader: SavedAugmentVisLoader =
getSavedFeatureAnywhereLoader();

const uiSettings = getUISettings();
const notifications = getNotifications();

useEffect(() => {
Expand Down Expand Up @@ -127,15 +134,12 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {

// Handles all changes in the assoicated detectors such as unlinking or new detectors associated
useEffect(() => {
// Gets all augmented saved objects
savedObjectLoader
.findAll()
.then((resp: any) => {
if (resp != undefined) {
const savedAugmentObjectsArr: ISavedAugmentVis[] = get(
resp,
'hits',
[]
// Gets all augmented saved objects that are associated to the given visualization
getAugmentVisSavedObjs(embeddable.vis.id, savedObjectLoader, uiSettings)
.then((savedAugmentObjectsArr: any) => {
if (savedAugmentObjectsArr != undefined) {
console.log(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this console line needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not, you can remove it in the next pr

'savedAugmentObjectsArr: ' + JSON.stringify(savedAugmentObjectsArr)
);
const curSelectedDetectors = getAssociatedDetectors(
Object.values(allDetectors),
Expand All @@ -156,14 +160,8 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
// that are associated to the current visualization
const getAssociatedDetectors = (
detectors: DetectorListItem[],
savedAugmentObjects: ISavedAugmentVis[]
savedAugmentForThisVisualization: ISavedAugmentVis[]
) => {
// Filter all savedAugmentObjects that aren't linked to the specific visualization
const savedAugmentForThisVisualization: ISavedAugmentVis[] =
savedAugmentObjects.filter(
(savedObj) => get(savedObj, 'visId', '') === embeddable.vis.id
);

// Map all detector IDs for all the found augmented vis objects
const savedAugmentDetectorsSet = new Set(
savedAugmentForThisVisualization.map((savedObject) =>
Expand All @@ -180,18 +178,13 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {

const onUnlinkDetector = async () => {
setIsLoadingFinalDetectors(true);
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
);

// Gets all augmented saved objects that are associated to the given visualization
await getAugmentVisSavedObjs(
embeddable.vis.id,
savedObjectLoader,
uiSettings
).then(async (savedAugmentForThisVisualization: any) => {
if (savedAugmentForThisVisualization != undefined) {
// find saved augment object matching detector we want to unlink
// There should only be one detector and vis pairing
const savedAugmentToUnlink = savedAugmentForThisVisualization.find(
Expand Down Expand Up @@ -239,11 +232,6 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
dispatch(getDetectorList(GET_ALL_DETECTORS_QUERY_PARAMS));
};

// TODO: this part is incomplete because it is pending on a different PR that will have all the associate existing changes
const openAssociateDetectorFlyout = async () => {
console.log('inside create anomaly detector');
};

const handleUnlinkDetectorAction = (detector: DetectorListItem) => {
setDetectorToUnlink(detector);
setConfirmModalState({
Expand Down Expand Up @@ -326,7 +314,7 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
fill
iconType="link"
onClick={() => {
openAssociateDetectorFlyout();
setMode('existing');
}}
>
Associate a detector
Expand Down
Loading