Skip to content

Commit 0966a2f

Browse files
support MDS in feature anywhere (#767) (#768)
Signed-off-by: Jackie Han <jkhanjob@gmail.com> (cherry picked from commit 1eebf24) Co-authored-by: Jackie Han <jkhanjob@gmail.com>
1 parent c9844a9 commit 0966a2f

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

public/components/FeatureAnywhereContextMenu/CreateAnomalyDetector/AddAnomalyDetector.tsx

+39-12
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import {
9696
getUISettings,
9797
getUiActions,
9898
getQueryService,
99+
getSavedObjectsClient,
99100
} from '../../../../public/services';
100101
import { prettifyErrorMessage } from '../../../../server/utils/helpers';
101102
import {
@@ -112,6 +113,12 @@ import { FLYOUT_MODES } from '../AnywhereParentFlyout/constants';
112113
import { DetectorListItem } from '../../../../public/models/interfaces';
113114
import { VisualizeEmbeddable } from '../../../../../../src/plugins/visualizations/public';
114115

116+
interface References {
117+
id: string;
118+
name: string;
119+
type: string;
120+
}
121+
115122
function AddAnomalyDetector({
116123
embeddable,
117124
closeFlyout,
@@ -126,24 +133,42 @@ function AddAnomalyDetector({
126133
VisualizeEmbeddable | ErrorEmbeddable
127134
>();
128135

136+
const indexPatternId = embeddable.vis.data.aggs.indexPattern.id;
137+
const [dataSourceId, setDataSourceId] = useState<string | undefined>(undefined);
138+
139+
async function getDataSourceId() {
140+
try {
141+
const indexPattern = await getSavedObjectsClient().get('index-pattern', indexPatternId);
142+
const refs = indexPattern.references as References[];
143+
const foundDataSourceId = refs.find(ref => ref.type === 'data-source')?.id;
144+
setDataSourceId(foundDataSourceId);
145+
} catch (error) {
146+
console.error("Error fetching index pattern:", error);
147+
}
148+
}
149+
150+
// useEffect to dispatch actions once dataSourceId fetch is complete
129151
useEffect(() => {
130-
const getInitialIndices = async () => {
131-
await dispatch(getIndices(queryText));
132-
};
133-
getInitialIndices();
134-
dispatch(getMappings(embeddable.vis.data.aggs.indexPattern.title));
152+
async function fetchData() {
153+
await getDataSourceId();
135154

136-
const createEmbeddable = async () => {
155+
const getIndicesDispatchCall = dispatch(getIndices(queryText, dataSourceId));
156+
const getMappingDispatchCall = dispatch(getMappings(embeddable.vis.data.aggs.indexPattern.title, dataSourceId));
157+
await Promise.all([getIndicesDispatchCall, getMappingDispatchCall]);
158+
}
159+
160+
async function createEmbeddable() {
137161
const visEmbeddable = await fetchVisEmbeddable(
138162
embeddable.vis.id,
139163
getEmbeddable(),
140164
getQueryService()
141165
);
142166
setGeneratedEmbeddable(visEmbeddable);
143-
};
144-
167+
}
168+
fetchData();
145169
createEmbeddable();
146-
}, []);
170+
}, [dataSourceId]);
171+
147172
const [isShowVis, setIsShowVis] = useState(false);
148173
const [accordionsOpen, setAccordionsOpen] = useState({ modelFeatures: true });
149174
const [detectorNameFromVis, setDetectorNameFromVis] = useState(
@@ -310,6 +335,7 @@ function AddAnomalyDetector({
310335
name: OVERLAY_ANOMALIES,
311336
args: {
312337
detectorId: detectorId,
338+
dataSourceId: dataSourceId
313339
},
314340
} as VisLayerExpressionFn;
315341

@@ -338,7 +364,7 @@ function AddAnomalyDetector({
338364
formikProps.setSubmitting(true);
339365
try {
340366
const detectorToCreate = formikToDetector(formikProps.values);
341-
await dispatch(createDetector(detectorToCreate))
367+
await dispatch(createDetector(detectorToCreate, dataSourceId))
342368
.then(async (response) => {
343369
dispatch(startDetector(response.response.id))
344370
.then((startDetectorResponse) => {})
@@ -410,7 +436,7 @@ function AddAnomalyDetector({
410436
});
411437
})
412438
.catch((err: any) => {
413-
dispatch(getDetectorCount()).then((response: any) => {
439+
dispatch(getDetectorCount(dataSourceId)).then((response: any) => {
414440
const totalDetectors = get(response, 'response.count', 0);
415441
if (totalDetectors === MAX_DETECTORS) {
416442
notifications.toasts.addDanger(
@@ -517,7 +543,7 @@ function AddAnomalyDetector({
517543
if (error) {
518544
return error;
519545
}
520-
const resp = await dispatch(matchDetector(detectorName));
546+
const resp = await dispatch(matchDetector(detectorName, dataSourceId));
521547
const match = get(resp, 'response.match', false);
522548
if (!match) {
523549
return undefined;
@@ -625,6 +651,7 @@ function AddAnomalyDetector({
625651
embeddableVisId={embeddable.vis.id}
626652
selectedDetector={selectedDetector}
627653
setSelectedDetector={setSelectedDetector}
654+
dataSourceId={dataSourceId}
628655
></AssociateExisting>
629656
)}
630657
{mode === FLYOUT_MODES.create && (

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ import {
4747
PLUGIN_NAME,
4848
} from '../../../../../../public/utils/constants';
4949
import { renderTime } from '../../../../../../public/pages/DetectorsList/utils/tableUtils';
50+
import { getAllDetectorsQueryParamsWithDataSourceId } from '../../../../../pages/utils/helpers';
5051

5152
interface AssociateExistingProps {
5253
embeddableVisId: string;
5354
selectedDetector: DetectorListItem | undefined;
5455
setSelectedDetector(detector: DetectorListItem | undefined): void;
56+
dataSourceId: string | undefined;
5557
}
5658

5759
export function AssociateExisting(
@@ -147,7 +149,7 @@ export function AssociateExisting(
147149
}, []);
148150

149151
const getDetectors = async () => {
150-
dispatch(getDetectorList(GET_ALL_DETECTORS_QUERY_PARAMS));
152+
dispatch(getDetectorList(getAllDetectorsQueryParamsWithDataSourceId(associateExistingProps.dataSourceId)));
151153
};
152154

153155
const selectedOptions = useMemo(() => {

public/expressions/helpers.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const getAnomalies = async (
3333
detectorId: string,
3434
startTime: number,
3535
endTime: number,
36-
resultIndex: string
36+
resultIndex: string,
37+
dataSourceId: string = '',
3738
): Promise<AnomalyData[]> => {
3839
const anomalySummaryQuery = getAnomalySummaryQuery(
3940
startTime,
@@ -45,14 +46,14 @@ export const getAnomalies = async (
4546
let anomalySummaryResponse;
4647
if (resultIndex === '') {
4748
anomalySummaryResponse = await getClient().post(
48-
`..${AD_NODE_API.DETECTOR}/results/_search`,
49+
`..${AD_NODE_API.DETECTOR}/results/_search/${dataSourceId}`,
4950
{
5051
body: JSON.stringify(anomalySummaryQuery),
5152
}
5253
);
5354
} else {
5455
anomalySummaryResponse = await getClient().post(
55-
`..${AD_NODE_API.DETECTOR}/results/_search/${resultIndex}/true`,
56+
`..${AD_NODE_API.DETECTOR}/results/_search/${resultIndex}/true/${dataSourceId}`,
5657
{
5758
body: JSON.stringify(anomalySummaryQuery),
5859
}
@@ -62,11 +63,11 @@ export const getAnomalies = async (
6263
return parsePureAnomalies(anomalySummaryResponse);
6364
};
6465

65-
export const getDetectorResponse = async (detectorId: string) => {
66-
const resp = await getClient().get(`..${AD_NODE_API.DETECTOR}/${detectorId}`);
66+
export const getDetectorResponse = async (detectorId: string, dataSourceId: string = '') => {
67+
const url = dataSourceId ? `..${AD_NODE_API.DETECTOR}/${detectorId}/${dataSourceId}` : `..${AD_NODE_API.DETECTOR}/${detectorId}`;
68+
const resp = await getClient().get(url);
6769
return resp;
6870
};
69-
7071
// This takes anomalies and returns them as vis layer of type PointInTimeEvents
7172
export const convertAnomaliesToPointInTimeEventsVisLayer = (
7273
anomalies: AnomalyData[],

public/expressions/overlay_anomalies.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Name = typeof OVERLAY_ANOMALIES;
4242

4343
interface Arguments {
4444
detectorId: string;
45+
dataSourceId: string;
4546
}
4647

4748
export type OverlayAnomaliesExpressionFunctionDefinition =
@@ -78,11 +79,17 @@ export const overlayAnomaliesFunction =
7879
default: '""',
7980
help: '',
8081
},
82+
dataSourceId: {
83+
types: ['string'],
84+
default: '""',
85+
help: '',
86+
},
8187
},
8288

8389
async fn(input, args, context): Promise<ExprVisLayers> {
8490
// Parsing all of the args & input
8591
const detectorId = get(args, 'detectorId', '');
92+
const dataSourceId = get(args, 'dataSourceId', '');
8693
const timeRange = get(
8794
context,
8895
'searchContext.timeRange',
@@ -103,7 +110,7 @@ export const overlayAnomaliesFunction =
103110
urlPath: `${PLUGIN_NAME}#/detectors/${detectorId}/results`, //details page for detector in AD plugin
104111
};
105112
try {
106-
const detectorResponse = await getDetectorResponse(detectorId);
113+
const detectorResponse = await getDetectorResponse(detectorId, dataSourceId);
107114
if (get(detectorResponse, 'error', '').includes(CANT_FIND_KEY_WORD)) {
108115
throw new Error('Anomaly Detector - ' + DETECTOR_HAS_BEEN_DELETED);
109116
} else if (

0 commit comments

Comments
 (0)