Skip to content

Commit

Permalink
support MDS in feature anywhere (#767)
Browse files Browse the repository at this point in the history
Signed-off-by: Jackie Han <[email protected]>
  • Loading branch information
jackiehanyang authored Jun 3, 2024
1 parent fc28db3 commit 1eebf24
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import {
getUISettings,
getUiActions,
getQueryService,
getSavedObjectsClient,
} from '../../../../public/services';
import { prettifyErrorMessage } from '../../../../server/utils/helpers';
import {
Expand All @@ -112,6 +113,12 @@ import { FLYOUT_MODES } from '../AnywhereParentFlyout/constants';
import { DetectorListItem } from '../../../../public/models/interfaces';
import { VisualizeEmbeddable } from '../../../../../../src/plugins/visualizations/public';

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

function AddAnomalyDetector({
embeddable,
closeFlyout,
Expand All @@ -126,24 +133,42 @@ function AddAnomalyDetector({
VisualizeEmbeddable | ErrorEmbeddable
>();

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);
}
}

// useEffect to dispatch actions once dataSourceId fetch is complete
useEffect(() => {
const getInitialIndices = async () => {
await dispatch(getIndices(queryText));
};
getInitialIndices();
dispatch(getMappings(embeddable.vis.data.aggs.indexPattern.title));
async function fetchData() {
await getDataSourceId();

const createEmbeddable = async () => {
const getIndicesDispatchCall = dispatch(getIndices(queryText, dataSourceId));
const getMappingDispatchCall = dispatch(getMappings(embeddable.vis.data.aggs.indexPattern.title, dataSourceId));
await Promise.all([getIndicesDispatchCall, getMappingDispatchCall]);
}

async function createEmbeddable() {
const visEmbeddable = await fetchVisEmbeddable(
embeddable.vis.id,
getEmbeddable(),
getQueryService()
);
setGeneratedEmbeddable(visEmbeddable);
};

}
fetchData();
createEmbeddable();
}, []);
}, [dataSourceId]);

const [isShowVis, setIsShowVis] = useState(false);
const [accordionsOpen, setAccordionsOpen] = useState({ modelFeatures: true });
const [detectorNameFromVis, setDetectorNameFromVis] = useState(
Expand Down Expand Up @@ -310,6 +335,7 @@ function AddAnomalyDetector({
name: OVERLAY_ANOMALIES,
args: {
detectorId: detectorId,
dataSourceId: dataSourceId
},
} as VisLayerExpressionFn;

Expand Down Expand Up @@ -338,7 +364,7 @@ function AddAnomalyDetector({
formikProps.setSubmitting(true);
try {
const detectorToCreate = formikToDetector(formikProps.values);
await dispatch(createDetector(detectorToCreate))
await dispatch(createDetector(detectorToCreate, dataSourceId))
.then(async (response) => {
dispatch(startDetector(response.response.id))
.then((startDetectorResponse) => {})
Expand Down Expand Up @@ -410,7 +436,7 @@ function AddAnomalyDetector({
});
})
.catch((err: any) => {
dispatch(getDetectorCount()).then((response: any) => {
dispatch(getDetectorCount(dataSourceId)).then((response: any) => {
const totalDetectors = get(response, 'response.count', 0);
if (totalDetectors === MAX_DETECTORS) {
notifications.toasts.addDanger(
Expand Down Expand Up @@ -517,7 +543,7 @@ function AddAnomalyDetector({
if (error) {
return error;
}
const resp = await dispatch(matchDetector(detectorName));
const resp = await dispatch(matchDetector(detectorName, dataSourceId));
const match = get(resp, 'response.match', false);
if (!match) {
return undefined;
Expand Down Expand Up @@ -625,6 +651,7 @@ function AddAnomalyDetector({
embeddableVisId={embeddable.vis.id}
selectedDetector={selectedDetector}
setSelectedDetector={setSelectedDetector}
dataSourceId={dataSourceId}
></AssociateExisting>
)}
{mode === FLYOUT_MODES.create && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ import {
PLUGIN_NAME,
} from '../../../../../../public/utils/constants';
import { renderTime } from '../../../../../../public/pages/DetectorsList/utils/tableUtils';
import { getAllDetectorsQueryParamsWithDataSourceId } from '../../../../../pages/utils/helpers';

interface AssociateExistingProps {
embeddableVisId: string;
selectedDetector: DetectorListItem | undefined;
setSelectedDetector(detector: DetectorListItem | undefined): void;
dataSourceId: string | undefined;
}

export function AssociateExisting(
Expand Down Expand Up @@ -147,7 +149,7 @@ export function AssociateExisting(
}, []);

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

const selectedOptions = useMemo(() => {
Expand Down
13 changes: 7 additions & 6 deletions public/expressions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const getAnomalies = async (
detectorId: string,
startTime: number,
endTime: number,
resultIndex: string
resultIndex: string,
dataSourceId: string = '',
): Promise<AnomalyData[]> => {
const anomalySummaryQuery = getAnomalySummaryQuery(
startTime,
Expand All @@ -45,14 +46,14 @@ export const getAnomalies = async (
let anomalySummaryResponse;
if (resultIndex === '') {
anomalySummaryResponse = await getClient().post(
`..${AD_NODE_API.DETECTOR}/results/_search`,
`..${AD_NODE_API.DETECTOR}/results/_search/${dataSourceId}`,
{
body: JSON.stringify(anomalySummaryQuery),
}
);
} else {
anomalySummaryResponse = await getClient().post(
`..${AD_NODE_API.DETECTOR}/results/_search/${resultIndex}/true`,
`..${AD_NODE_API.DETECTOR}/results/_search/${resultIndex}/true/${dataSourceId}`,
{
body: JSON.stringify(anomalySummaryQuery),
}
Expand All @@ -62,11 +63,11 @@ export const getAnomalies = async (
return parsePureAnomalies(anomalySummaryResponse);
};

export const getDetectorResponse = async (detectorId: string) => {
const resp = await getClient().get(`..${AD_NODE_API.DETECTOR}/${detectorId}`);
export const getDetectorResponse = async (detectorId: string, dataSourceId: string = '') => {
const url = dataSourceId ? `..${AD_NODE_API.DETECTOR}/${detectorId}/${dataSourceId}` : `..${AD_NODE_API.DETECTOR}/${detectorId}`;
const resp = await getClient().get(url);
return resp;
};

// This takes anomalies and returns them as vis layer of type PointInTimeEvents
export const convertAnomaliesToPointInTimeEventsVisLayer = (
anomalies: AnomalyData[],
Expand Down
9 changes: 8 additions & 1 deletion public/expressions/overlay_anomalies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Name = typeof OVERLAY_ANOMALIES;

interface Arguments {
detectorId: string;
dataSourceId: string;
}

export type OverlayAnomaliesExpressionFunctionDefinition =
Expand Down Expand Up @@ -78,11 +79,17 @@ export const overlayAnomaliesFunction =
default: '""',
help: '',
},
dataSourceId: {
types: ['string'],
default: '""',
help: '',
},
},

async fn(input, args, context): Promise<ExprVisLayers> {
// Parsing all of the args & input
const detectorId = get(args, 'detectorId', '');
const dataSourceId = get(args, 'dataSourceId', '');
const timeRange = get(
context,
'searchContext.timeRange',
Expand All @@ -103,7 +110,7 @@ export const overlayAnomaliesFunction =
urlPath: `${PLUGIN_NAME}#/detectors/${detectorId}/results`, //details page for detector in AD plugin
};
try {
const detectorResponse = await getDetectorResponse(detectorId);
const detectorResponse = await getDetectorResponse(detectorId, dataSourceId);
if (get(detectorResponse, 'error', '').includes(CANT_FIND_KEY_WORD)) {
throw new Error('Anomaly Detector - ' + DETECTOR_HAS_BEEN_DELETED);
} else if (
Expand Down

0 comments on commit 1eebf24

Please sign in to comment.