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

mds feature anywhere associated detectors fix #778

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
Loading