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

Bug fix and mds support for AD Plugin APIs #962

Merged
merged 15 commits into from
Jun 4, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`CreateMonitor renders 1`] = `
}
>
<Formik
enableReinitialize={true}
initialValues={
Object {
"adResultIndex": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../../components/MonitorExpressions/expressions/utils/constants';
import { MONITOR_TYPE } from '../../../../../utils/constants';
import { SUPPORTED_DOC_LEVEL_QUERY_OPERATORS } from '../../../components/DocumentLevelMonitorQueries/utils/constants';
import { dataSourceEnabled } from '../../../../utils/helpers';

export const BUCKET_COUNT = 5;

Expand Down Expand Up @@ -80,6 +81,10 @@ export const FORMIK_INITIAL_VALUES = {
preventVisualEditor: false,
};

if (dataSourceEnabled()) {
FORMIK_INITIAL_VALUES["dataSourceId"] = "random-dataSourceId";
}

export const FORMIK_INITIAL_AGG_VALUES = {
aggregationType: 'count',
fieldName: '',
Expand Down
4 changes: 3 additions & 1 deletion server/routes/anomalyDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { schema } from '@osd/config-schema';
import { createValidateQuerySchema } from '../services/utils/helpers';

export default function (services, router, dataSourceEnabled) {
const { anomalyDetectorService } = services;
Expand All @@ -15,6 +16,7 @@ export default function (services, router, dataSourceEnabled) {
params: schema.object({
detectorId: schema.string(),
}),
query: createValidateQuerySchema(dataSourceEnabled),
},
},
anomalyDetectorService.getDetector
Expand All @@ -35,7 +37,7 @@ export default function (services, router, dataSourceEnabled) {
params: schema.object({
detectorId: schema.string(),
}),
query: schema.any(),
query: createValidateQuerySchema(dataSourceEnabled),
},
},
anomalyDetectorService.getDetectorResults
Expand Down
23 changes: 10 additions & 13 deletions server/services/AnomalyDetectorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import { get } from 'lodash';
import { mapKeysDeep, toCamel } from './utils/helpers';
import { anomalyResultMapper } from './utils/adHelpers';
import { MDSEnabledClientService } from './MDSEnabledClientService';

const MAX_DETECTOR_COUNT = 1000;
export default class DestinationsService {
constructor(esDriver) {
this.esDriver = esDriver;
}

export default class DestinationsService extends MDSEnabledClientService {
getDetector = async (context, req, res) => {
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,
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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) =>
Expand Down
Loading