diff --git a/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectorData.js b/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectorData.js index 135c4989d..3e318e634 100644 --- a/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectorData.js +++ b/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectorData.js @@ -10,6 +10,7 @@ import { CoreContext } from '../../../../utils/CoreContext'; import { AD_PREVIEW_DAYS, DEFAULT_PREVIEW_ERROR_MSG } from '../../../../utils/constants'; import { backendErrorNotification } from '../../../../utils/helpers'; import { getClient, getNotifications } from '../../../../services'; +import { getDataSourceId } from '../../../utils/helpers'; class AnomalyDetectorData extends React.Component { static contextType = CoreContext; @@ -66,8 +67,13 @@ class AnomalyDetectorData extends React.Component { preview: this.props.preview, }; try { + const dataSourceId = getDataSourceId(); + const extendedParams = { + ...(dataSourceId !== undefined && { dataSourceId }), // Only include dataSourceId if it exists + ...requestParams, // Other parameters + }; const response = await httpClient.get(`../api/alerting/detectors/${detectorId}/results`, { - query: requestParams, + query: extendedParams, }); if (response.ok) { const { anomalyResult, detector } = response.response; diff --git a/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectors.js b/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectors.js index 6af625b50..00a308686 100644 --- a/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectors.js +++ b/public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectors.js @@ -7,17 +7,11 @@ import React from 'react'; import PropTypes from 'prop-types'; import { get } from 'lodash'; import { FormikComboBox } from '../../../../components/FormControls'; -import { - hasError, - isInvalid, - required, - validateDetector, - validateMonitorName, -} from '../../../../utils/validate'; +import { hasError, isInvalid, validateDetector } from '../../../../utils/validate'; import { CoreContext } from '../../../../utils/CoreContext'; -import { backendErrorNotification } from '../../../../utils/helpers'; import FormikFieldText from '../../../../components/FormControls/FormikFieldText'; import { getClient } from '../../../../services'; +import { getDataSourceQueryObj } from '../../../utils/helpers'; class AnomalyDetectors extends React.Component { static contextType = CoreContext; @@ -36,7 +30,10 @@ class AnomalyDetectors extends React.Component { async searchDetectors() { const httpClient = getClient(); try { - const response = await httpClient.post('../api/alerting/detectors/_search'); + const dataSourceQuery = getDataSourceQueryObj(); + const response = await httpClient.post('../api/alerting/detectors/_search', { + query: dataSourceQuery?.query, + }); if (response.ok) { const detectorOptions = response.detectors .filter((detector) => detector.detectionDateRange === undefined) diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/CreateMonitor.js b/public/pages/CreateMonitor/containers/CreateMonitor/CreateMonitor.js index 72bdd29aa..3b8c077ff 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/CreateMonitor.js +++ b/public/pages/CreateMonitor/containers/CreateMonitor/CreateMonitor.js @@ -28,6 +28,7 @@ import { getPerformanceModal, RECOMMENDED_DURATION, } from '../../components/QueryPerformance/QueryPerformance'; +import { isDataSourceChanged } from '../../../utils/helpers'; export default class CreateMonitor extends Component { static defaultProps = { @@ -151,6 +152,17 @@ export default class CreateMonitor extends Component { this.props.setFlyout(null); } + componentDidUpdate(prevProps) { + if (isDataSourceChanged(prevProps, this.props)) { + this.setState({ + initialValues: { + ...this.state.initialValues, + dataSourceId: this.props.landingDataSourceId + } + }); + } + } + render() { const { edit, @@ -169,6 +181,7 @@ export default class CreateMonitor extends Component { initialValues={initialValues} onSubmit={this.evaluateSubmission} validateOnChange={false} + enableReinitialize={true} > {({ values, errors, handleSubmit, isSubmitting, isValid, touched }) => { const isComposite = values.monitor_type === MONITOR_TYPE.COMPOSITE_LEVEL; diff --git a/public/pages/CreateMonitor/containers/CreateMonitor/__snapshots__/CreateMonitor.test.js.snap b/public/pages/CreateMonitor/containers/CreateMonitor/__snapshots__/CreateMonitor.test.js.snap index 598937daf..a7f3b3507 100644 --- a/public/pages/CreateMonitor/containers/CreateMonitor/__snapshots__/CreateMonitor.test.js.snap +++ b/public/pages/CreateMonitor/containers/CreateMonitor/__snapshots__/CreateMonitor.test.js.snap @@ -9,6 +9,7 @@ exports[`CreateMonitor renders 1`] = ` } > { const { detectorId } = req.params; - const { callAsCurrentUser } = this.esDriver.asScoped(req); + const client = this.getClientBasedOnDataSource(context, req); try { - const resp = await callAsCurrentUser('alertingAD.getDetector', { detectorId }); + const resp = await client('alertingAD.getDetector', { detectorId }); const { anomaly_detector, _seq_no: seqNo, @@ -48,9 +45,9 @@ export default class DestinationsService { query: { bool: {} }, size: MAX_DETECTOR_COUNT, }; - const { callAsCurrentUser } = this.esDriver.asScoped(req); + const client = this.getClientBasedOnDataSource(context, req); try { - const resp = await callAsCurrentUser('alertingAD.searchDetectors', { + const resp = await client('alertingAD.searchDetectors', { body: searchRequest, }); @@ -87,13 +84,13 @@ export default class DestinationsService { try { const { startTime = 0, endTime = 20, preview = 'false' } = req.query; const { detectorId } = req.params; - const { callAsCurrentUser } = this.esDriver.asScoped(req); + const client = this.getClientBasedOnDataSource(context, req); if (preview == 'true') { const requestBody = { period_start: startTime, period_end: endTime, }; - const previewResponse = await callAsCurrentUser('alertingAD.previewDetector', { + const previewResponse = await client('alertingAD.previewDetector', { detectorId, body: requestBody, }); @@ -134,10 +131,10 @@ export default class DestinationsService { }, }, }; - const detectorResponse = await callAsCurrentUser('alertingAD.getDetector', { + const detectorResponse = await client('alertingAD.getDetector', { detectorId, }); - const anomaliesResponse = await callAsCurrentUser('alertingAD.searchResults', { + const anomaliesResponse = await client('alertingAD.searchResults', { body: requestBody, }); const transformedKeys = get(anomaliesResponse, 'hits.hits', []).map((result) =>