Skip to content

Commit

Permalink
mds feature anywhere associated detectors fix (#778)
Browse files Browse the repository at this point in the history
* mds feature anywhere associated detectors fix

Signed-off-by: Jackie Han <jkhanjob@gmail.com>

* update getDetectors call in associate existing detector flyout

Signed-off-by: Jackie Han <jkhanjob@gmail.com>

---------

Signed-off-by: Jackie Han <jkhanjob@gmail.com>
  • Loading branch information
jackiehanyang authored Jun 11, 2024
1 parent f1fb57a commit 086fc35
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getSavedFeatureAnywhereLoader,
getNotifications,
getUISettings,
getSavedObjectsClient,
} from '../../../../services';
import {
GET_ALL_DETECTORS_QUERY_PARAMS,
Expand All @@ -47,6 +48,7 @@ import {
} from '../../../../../../../src/plugins/vis_augmenter/public';
import { ASSOCIATED_DETECTOR_ACTION } from '../utils/constants';
import { PLUGIN_AUGMENTATION_MAX_OBJECTS_SETTING } from '../../../../../public/expressions/constants';
import { getAllDetectorsQueryParamsWithDataSourceId } from '../../../../../public/pages/utils/helpers';

interface ConfirmModalState {
isOpen: boolean;
Expand All @@ -56,6 +58,12 @@ interface ConfirmModalState {
affectedDetector: DetectorListItem;
}

interface References {
id: string;
name: string;
type: string;
}

function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
const dispatch = useDispatch();
const allDetectors = useSelector((state: AppState) => state.ad.detectorList);
Expand All @@ -69,6 +77,20 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
(state: AppState) => state.ad.errorMessage
);
const embeddableTitle = embeddable.getTitle();
const indexPatternId = embeddable.vis.data.aggs.indexPattern.id;
const [dataSourceId, setDataSourceId] = useState<string | undefined>(undefined);

async function getDataSourceId() {
try {
const indexPattern = await getSavedObjectsClient().get('index-pattern', indexPatternId);
const refs = indexPattern.references as References[];
const foundDataSourceId = refs.find(ref => ref.type === 'data-source')?.id;
setDataSourceId(foundDataSourceId);
} catch (error) {
console.error("Error fetching index pattern:", error);
}
}

const [selectedDetectors, setSelectedDetectors] = useState(
[] as DetectorListItem[]
);
Expand Down Expand Up @@ -135,8 +157,12 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
}, [confirmModalState.isRequestingToClose, isLoading]);

useEffect(() => {
getDetectors();
}, []);
async function fetchData() {
await getDataSourceId();
getDetectors();
}
fetchData();
}, [dataSourceId]);

// Handles all changes in the assoicated detectors such as unlinking or new detectors associated
useEffect(() => {
Expand Down Expand Up @@ -175,9 +201,9 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
) => {
// Map all detector IDs for all the found augmented vis objects
const savedAugmentDetectorsSet = new Set(
savedAugmentForThisVisualization.map((savedObject) =>
get(savedObject, 'pluginResource.id', '')
)
savedAugmentForThisVisualization
.map(savedObject => get(savedObject, 'pluginResource.id', ''))
.filter(id => id !== '')
);

// filter out any detectors that aren't on the set of detectors IDs from the augmented vis objects.
Expand Down Expand Up @@ -240,7 +266,11 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
};

const getDetectors = async () => {
dispatch(getDetectorList(GET_ALL_DETECTORS_QUERY_PARAMS));
dispatch(
getDetectorList(
getAllDetectorsQueryParamsWithDataSourceId(dataSourceId)
)
);
};

const handleUnlinkDetectorAction = (detector: DetectorListItem) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ function AddAnomalyDetector({
const detectorToCreate = formikToDetector(formikProps.values);
await dispatch(createDetector(detectorToCreate, dataSourceId))
.then(async (response) => {
dispatch(startDetector(response.response.id))
dispatch(startDetector(response.response.id, dataSourceId))
.then((startDetectorResponse) => {})
.catch((err: any) => {
notifications.toasts.addDanger(
Expand Down Expand Up @@ -651,7 +651,7 @@ function AddAnomalyDetector({
embeddableVisId={embeddable.vis.id}
selectedDetector={selectedDetector}
setSelectedDetector={setSelectedDetector}
dataSourceId={dataSourceId}
indexPatternId={indexPatternId}
></AssociateExisting>
)}
{mode === FLYOUT_MODES.create && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import { getDetectorList } from '../../../../../redux/reducers/ad';
import {
getSavedFeatureAnywhereLoader,
getSavedObjectsClient,
getUISettings,
} from '../../../../../services';
import {
Expand All @@ -53,7 +54,12 @@ interface AssociateExistingProps {
embeddableVisId: string;
selectedDetector: DetectorListItem | undefined;
setSelectedDetector(detector: DetectorListItem | undefined): void;
dataSourceId: string | undefined;
indexPatternId: string;
}
interface References {
id: string;
name: string;
type: string;
}

export function AssociateExisting(
Expand All @@ -65,6 +71,18 @@ export function AssociateExisting(
const isRequestingFromES = useSelector(
(state: AppState) => state.ad.requesting
);
const [dataSourceId, setDataSourceId] = useState<string | undefined>(undefined);

async function getDataSourceId() {
try {
const indexPattern = await getSavedObjectsClient().get('index-pattern', associateExistingProps.indexPatternId);
const refs = indexPattern.references as References[];
const foundDataSourceId = refs.find(ref => ref.type === 'data-source')?.id;
setDataSourceId(foundDataSourceId);
} catch (error) {
console.error("Error fetching index pattern:", error);
}
}
const uiSettings = getUISettings();
const [isLoadingFinalDetectors, setIsLoadingFinalDetectors] =
useState<boolean>(true);
Expand Down Expand Up @@ -145,11 +163,15 @@ export function AssociateExisting(
};

useEffect(() => {
getDetectors();
}, []);
async function fetchData() {
await getDataSourceId();
getDetectors();
}
fetchData();
}, [dataSourceId]);

const getDetectors = async () => {
dispatch(getDetectorList(getAllDetectorsQueryParamsWithDataSourceId(associateExistingProps.dataSourceId)));
dispatch(getDetectorList(getAllDetectorsQueryParamsWithDataSourceId(dataSourceId)));
};

const selectedOptions = useMemo(() => {
Expand Down

0 comments on commit 086fc35

Please sign in to comment.