Skip to content

fix: Anomaly detection features count locked at 5 #1031

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions public/pages/ConfigureModel/components/Features/Features.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,34 @@ import { FieldArray, FieldArrayRenderProps, FormikProps } from 'formik';

import { get } from 'lodash';
import React, { Fragment, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import ContentPanel from '../../../../components/ContentPanel/ContentPanel';
import { Detector } from '../../../../models/interfaces';
import { initialFeatureValue } from '../../utils/helpers';
import { MAX_FEATURE_NUM, BASE_DOCS_LINK } from '../../../../utils/constants';
import { initialFeatureValue, getMaxFeatureLimit } from '../../utils/helpers';
import { BASE_DOCS_LINK } from '../../../../utils/constants';
import { FeatureAccordion } from '../FeatureAccordion';
import { getClustersSetting } from '../../../../redux/reducers/opensearch';
import { AppState } from '../../../../redux/reducers';

interface FeaturesProps {
detector: Detector | undefined;
formikProps: FormikProps<any>;
}

export function Features(props: FeaturesProps) {
const dispatch = useDispatch();
const anomalySettings = useSelector(
(state: AppState) => state.opensearch.settings
);

useEffect(() => {
const getSettingResult = async () => {
await dispatch(getClustersSetting());
};
getSettingResult();
}, []);


// If the features list is empty: push a default initial one
useEffect(() => {
if (get(props, 'formikProps.values.featureList', []).length === 0) {
Expand Down Expand Up @@ -88,7 +104,7 @@ export function Features(props: FeaturesProps) {
<EuiFlexItem grow={false}>
<EuiSmallButton
data-test-subj="addFeature"
isDisabled={values.featureList.length >= MAX_FEATURE_NUM}
isDisabled={values.featureList.length >= getMaxFeatureLimit(anomalySettings)}
onClick={() => {
push(initialFeatureValue());
}}
Expand All @@ -99,7 +115,7 @@ export function Features(props: FeaturesProps) {
<p>
You can add up to{' '}
{Math.max(
MAX_FEATURE_NUM - values.featureList.length,
getMaxFeatureLimit(anomalySettings) - values.featureList.length,
0
)}{' '}
more features.
Expand Down
15 changes: 14 additions & 1 deletion public/pages/ConfigureModel/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* GitHub history for details.
*/

import { DATA_TYPES, DEFAULT_SHINGLE_SIZE } from '../../../utils/constants';
import { DATA_TYPES, DEFAULT_SHINGLE_SIZE, MAX_FEATURE_NUM } from '../../../utils/constants';
import {
FEATURE_TYPE,
FeatureAttributes,
Expand Down Expand Up @@ -40,6 +40,7 @@ import {
Action,
} from '../../../models/types';
import { SparseDataOptionValue } from './constants';
import { ClusterSetting } from 'server/models/types';

export const getFieldOptions = (
allFields: { [key: string]: string[] },
Expand Down Expand Up @@ -711,3 +712,15 @@ export const rulesToFormik = (
);
return finalRules;
};

export function getMaxFeatureLimit(
settings: ClusterSetting[]
) {
if (!settings || settings.length === 0) {
return MAX_FEATURE_NUM;
}
const maxFeatureSetting = settings.find(
(setting) => setting.name === 'max_anomaly_features'
);
return maxFeatureSetting ? parseInt(maxFeatureSetting.value, 10) : MAX_FEATURE_NUM;
}
36 changes: 36 additions & 0 deletions public/redux/reducers/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getPathsPerDataType } from './mapper';
import {
CatIndex,
ClusterInfo,
ClusterSetting,
IndexAlias,
} from '../../../server/models/types';
import { AD_NODE_API } from '../../../utils/constants';
Expand All @@ -35,6 +36,7 @@ const BULK = 'opensearch/BULK';
const DELETE_INDEX = 'opensearch/DELETE_INDEX';
const GET_CLUSTERS_INFO = 'opensearch/GET_CLUSTERS_INFO';
const GET_INDICES_AND_ALIASES = 'opensearch/GET_INDICES_AND_ALIASES';
const GET_CLUSTERS_SETTING = 'opensearch/GET_CLUSTERS_SETTING';

export type Mappings = {
[key: string]: any;
Expand Down Expand Up @@ -69,6 +71,7 @@ interface OpenSearchState {
searchResult: object;
errorMessage: string;
clusters: ClusterInfo[];
settings: ClusterSetting[];
}

export const initialState: OpenSearchState = {
Expand Down Expand Up @@ -299,6 +302,31 @@ const reducer = handleActions<OpenSearchState>(
errorMessage: get(action, 'error.error', action.error),
}),
},
[GET_CLUSTERS_SETTING]: {
REQUEST: (state: OpenSearchState): OpenSearchState => {
return { ...state, requesting: true, errorMessage: '' };
},
SUCCESS: (
state: OpenSearchState,
action: APIResponseAction
): OpenSearchState => {
return {
...state,
requesting: false,
settings: get(action, 'result.response.settings', []),
};
},
FAILURE: (
state: OpenSearchState,
action: APIErrorAction
): OpenSearchState => {
return {
...state,
requesting: false,
errorMessage: get(action, 'error.error', action.error),
}
},
},
},
initialState
);
Expand Down Expand Up @@ -326,6 +354,14 @@ export const getClustersInfo = (dataSourceId: string = ''): APIAction => {
};
};

export const getClustersSetting = (): APIAction => {
const baseUrl = `..${AD_NODE_API.GET_CLUSTERS_SETTING}`;
return {
type: GET_CLUSTERS_SETTING,
request: (client: HttpSetup) => client.get(baseUrl),
};
};

export const getIndicesAndAliases = (
searchKey = '',
dataSourceId: string = '',
Expand Down
5 changes: 5 additions & 0 deletions server/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export type ClusterInfo = {
localCluster: boolean;
}

export type ClusterSetting = {
name: string;
value: string;
}

export type IndexAlias = {
index: string[] | string;
alias: string;
Expand Down
54 changes: 54 additions & 0 deletions server/routes/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SearchResponse } from '../models/interfaces';
import {
CatIndex,
ClusterInfo,
ClusterSetting,
GetAliasesResponse,
GetIndicesResponse,
GetMappingResponse,
Expand Down Expand Up @@ -76,6 +77,7 @@ export function registerOpenSearchRoutes(
'/_indices_and_aliases/{dataSourceId}',
opensearchService.getIndicesAndAliases
);
apiRouter.get('/_cluster/settings', opensearchService.getClustersSetting);
}

export default class OpenSearchService {
Expand Down Expand Up @@ -607,4 +609,56 @@ export default class OpenSearchService {
});
}
};

getClustersSetting = async (
context: RequestHandlerContext,
request: OpenSearchDashboardsRequest,
opensearchDashboardsResponse: OpenSearchDashboardsResponseFactory
): Promise<IOpenSearchDashboardsResponse<any>> => {
const { dataSourceId = '' } = request.params as { dataSourceId?: string };
try {
const callWithRequest = getClientBasedOnDataSource(
context,
this.dataSourceEnabled,
request,
dataSourceId,
this.client
);

let anomalySettings: ClusterSetting[] = [];

try {
const anomalySettingsResponse = await callWithRequest('transport.request', {
method: 'GET',
path: '/_cluster/settings',
});

if (
anomalySettingsResponse?.persistent?.plugins?.anomaly_detection
) {
const anomalyDetectionSettings = anomalySettingsResponse.persistent.plugins.anomaly_detection;
anomalySettings = Object.keys(anomalyDetectionSettings).map((key) => ({
name: key,
value: anomalyDetectionSettings[key],
}));
} else {
console.warn('Could not get anomaly detection setting');
}
} catch (err) {
console.warn('Could not get anomaly detection setting', err);
}

return opensearchDashboardsResponse.ok({
body: { ok: true, response: { settings: anomalySettings } },
});
} catch (err) {
console.log('Anomaly detector - Unable to get anomaly detection cluster setting', err);
return opensearchDashboardsResponse.ok({
body: {
ok: false,
error: getErrorMessage(err),
},
});
}
};
}
1 change: 1 addition & 0 deletions utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const AD_NODE_API = Object.freeze({
CREATE_SAMPLE_DATA: `${BASE_NODE_API_PATH}/create_sample_data`,
GET_CLUSTERS_INFO: `${BASE_NODE_API_PATH}/_remote/info`,
GET_INDICES_AND_ALIASES: `${BASE_NODE_API_PATH}/_indices_and_aliases`,
GET_CLUSTERS_SETTING: `${BASE_NODE_API_PATH}/_cluster/settings`,
});
export const ALERTING_NODE_API = Object.freeze({
_SEARCH: `${BASE_NODE_API_PATH}/monitors/_search`,
Expand Down
Loading