Skip to content

Commit 916b3ea

Browse files
jackiehanyanggithub-actions[bot]
authored andcommitted
mds feature anywhere associated detectors fix (#778)
* 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> (cherry picked from commit 086fc35)
1 parent 78b80df commit 916b3ea

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

public/components/FeatureAnywhereContextMenu/AssociatedDetectors/containers/AssociatedDetectors.tsx

+36-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getSavedFeatureAnywhereLoader,
2727
getNotifications,
2828
getUISettings,
29+
getSavedObjectsClient,
2930
} from '../../../../services';
3031
import {
3132
GET_ALL_DETECTORS_QUERY_PARAMS,
@@ -47,6 +48,7 @@ import {
4748
} from '../../../../../../../src/plugins/vis_augmenter/public';
4849
import { ASSOCIATED_DETECTOR_ACTION } from '../utils/constants';
4950
import { PLUGIN_AUGMENTATION_MAX_OBJECTS_SETTING } from '../../../../../public/expressions/constants';
51+
import { getAllDetectorsQueryParamsWithDataSourceId } from '../../../../../public/pages/utils/helpers';
5052

5153
interface ConfirmModalState {
5254
isOpen: boolean;
@@ -56,6 +58,12 @@ interface ConfirmModalState {
5658
affectedDetector: DetectorListItem;
5759
}
5860

61+
interface References {
62+
id: string;
63+
name: string;
64+
type: string;
65+
}
66+
5967
function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
6068
const dispatch = useDispatch();
6169
const allDetectors = useSelector((state: AppState) => state.ad.detectorList);
@@ -69,6 +77,20 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
6977
(state: AppState) => state.ad.errorMessage
7078
);
7179
const embeddableTitle = embeddable.getTitle();
80+
const indexPatternId = embeddable.vis.data.aggs.indexPattern.id;
81+
const [dataSourceId, setDataSourceId] = useState<string | undefined>(undefined);
82+
83+
async function getDataSourceId() {
84+
try {
85+
const indexPattern = await getSavedObjectsClient().get('index-pattern', indexPatternId);
86+
const refs = indexPattern.references as References[];
87+
const foundDataSourceId = refs.find(ref => ref.type === 'data-source')?.id;
88+
setDataSourceId(foundDataSourceId);
89+
} catch (error) {
90+
console.error("Error fetching index pattern:", error);
91+
}
92+
}
93+
7294
const [selectedDetectors, setSelectedDetectors] = useState(
7395
[] as DetectorListItem[]
7496
);
@@ -135,8 +157,12 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
135157
}, [confirmModalState.isRequestingToClose, isLoading]);
136158

137159
useEffect(() => {
138-
getDetectors();
139-
}, []);
160+
async function fetchData() {
161+
await getDataSourceId();
162+
getDetectors();
163+
}
164+
fetchData();
165+
}, [dataSourceId]);
140166

141167
// Handles all changes in the assoicated detectors such as unlinking or new detectors associated
142168
useEffect(() => {
@@ -175,9 +201,9 @@ function AssociatedDetectors({ embeddable, closeFlyout, setMode }) {
175201
) => {
176202
// Map all detector IDs for all the found augmented vis objects
177203
const savedAugmentDetectorsSet = new Set(
178-
savedAugmentForThisVisualization.map((savedObject) =>
179-
get(savedObject, 'pluginResource.id', '')
180-
)
204+
savedAugmentForThisVisualization
205+
.map(savedObject => get(savedObject, 'pluginResource.id', ''))
206+
.filter(id => id !== '')
181207
);
182208

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

242268
const getDetectors = async () => {
243-
dispatch(getDetectorList(GET_ALL_DETECTORS_QUERY_PARAMS));
269+
dispatch(
270+
getDetectorList(
271+
getAllDetectorsQueryParamsWithDataSourceId(dataSourceId)
272+
)
273+
);
244274
};
245275

246276
const handleUnlinkDetectorAction = (detector: DetectorListItem) => {

public/components/FeatureAnywhereContextMenu/CreateAnomalyDetector/AddAnomalyDetector.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function AddAnomalyDetector({
366366
const detectorToCreate = formikToDetector(formikProps.values);
367367
await dispatch(createDetector(detectorToCreate, dataSourceId))
368368
.then(async (response) => {
369-
dispatch(startDetector(response.response.id))
369+
dispatch(startDetector(response.response.id, dataSourceId))
370370
.then((startDetectorResponse) => {})
371371
.catch((err: any) => {
372372
notifications.toasts.addDanger(
@@ -651,7 +651,7 @@ function AddAnomalyDetector({
651651
embeddableVisId={embeddable.vis.id}
652652
selectedDetector={selectedDetector}
653653
setSelectedDetector={setSelectedDetector}
654-
dataSourceId={dataSourceId}
654+
indexPatternId={indexPatternId}
655655
></AssociateExisting>
656656
)}
657657
{mode === FLYOUT_MODES.create && (

public/components/FeatureAnywhereContextMenu/CreateAnomalyDetector/AssociateExisting/containers/AssociateExisting.tsx

+26-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import { getDetectorList } from '../../../../../redux/reducers/ad';
3535
import {
3636
getSavedFeatureAnywhereLoader,
37+
getSavedObjectsClient,
3738
getUISettings,
3839
} from '../../../../../services';
3940
import {
@@ -53,7 +54,12 @@ interface AssociateExistingProps {
5354
embeddableVisId: string;
5455
selectedDetector: DetectorListItem | undefined;
5556
setSelectedDetector(detector: DetectorListItem | undefined): void;
56-
dataSourceId: string | undefined;
57+
indexPatternId: string;
58+
}
59+
interface References {
60+
id: string;
61+
name: string;
62+
type: string;
5763
}
5864

5965
export function AssociateExisting(
@@ -65,6 +71,18 @@ export function AssociateExisting(
6571
const isRequestingFromES = useSelector(
6672
(state: AppState) => state.ad.requesting
6773
);
74+
const [dataSourceId, setDataSourceId] = useState<string | undefined>(undefined);
75+
76+
async function getDataSourceId() {
77+
try {
78+
const indexPattern = await getSavedObjectsClient().get('index-pattern', associateExistingProps.indexPatternId);
79+
const refs = indexPattern.references as References[];
80+
const foundDataSourceId = refs.find(ref => ref.type === 'data-source')?.id;
81+
setDataSourceId(foundDataSourceId);
82+
} catch (error) {
83+
console.error("Error fetching index pattern:", error);
84+
}
85+
}
6886
const uiSettings = getUISettings();
6987
const [isLoadingFinalDetectors, setIsLoadingFinalDetectors] =
7088
useState<boolean>(true);
@@ -145,11 +163,15 @@ export function AssociateExisting(
145163
};
146164

147165
useEffect(() => {
148-
getDetectors();
149-
}, []);
166+
async function fetchData() {
167+
await getDataSourceId();
168+
getDetectors();
169+
}
170+
fetchData();
171+
}, [dataSourceId]);
150172

151173
const getDetectors = async () => {
152-
dispatch(getDetectorList(getAllDetectorsQueryParamsWithDataSourceId(associateExistingProps.dataSourceId)));
174+
dispatch(getDetectorList(getAllDetectorsQueryParamsWithDataSourceId(dataSourceId)));
153175
};
154176

155177
const selectedOptions = useMemo(() => {

0 commit comments

Comments
 (0)