Skip to content

Commit

Permalink
Add support for DetectorState on Dashboard. Issue: opendistro-for-ela…
Browse files Browse the repository at this point in the history
…sticsearch#25 (opendistro-for-elasticsearch#28)

* Add support for DetectorState on Dashboard. Issue: opendistro-for-elasticsearch#25
  • Loading branch information
yizheliu-amazon authored Apr 16, 2020
1 parent a54387a commit 66bc75a
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 47 deletions.
3 changes: 1 addition & 2 deletions public/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
* permissions and limitations under the License.
*/

import { DATA_TYPES } from '../utils/constants';
import { DETECTOR_STATE } from 'public/pages/utils/constants';
import { DATA_TYPES, DETECTOR_STATE } from '../utils/constants';

export type FieldInfo = {
label: string;
Expand Down
34 changes: 16 additions & 18 deletions public/pages/Dashboard/Container/DashboardOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ import {
} from '../utils/constants';
import { AppState } from '../../../redux/reducers';
import { CatIndex, IndexAlias } from '../../../../server/models/types';
// import { getVisibleOptions } from '../../../pages/createDetector/containers/DataSource/utils/helpers';
import { getVisibleOptions } from '../../utils/helpers';
import {
DETECTOR_STATE,
PLUGIN_NAME,
APP_PATH,
} from '../../../utils/constants';
import { getDetectorStateOptions } from '../../DetectorsList/utils/helpers';

export function DashboardOverview() {
const dispatch = useDispatch();
Expand Down Expand Up @@ -89,27 +89,20 @@ export function DashboardOverview() {
setAllDetectorsSelected(isEmpty(selectedNames));
};

const allDetectorStates = Object.values(DETECTOR_STATE);

const [selectedDetectorStates, setSelectedDetectorStates] = useState(
[] as string[]
[] as DETECTOR_STATE[]
);
// TODO: DetectorStates is placeholder for now until backend profile API is ready
// Issue link: https://github.com/opendistro-for-elasticsearch/anomaly-detection-kibana-plugin/issues/25

const [allDetectorStatesSelected, setAllDetectorStatesSelected] = useState(
true
);

const getDetectorStateOptions = (states: string[]) => {
return states.map(state => {
return { label: state };
});
};

const handleDetectorStateFilterChange = (
options: EuiComboBoxOptionProps[]
): void => {
const selectedStates = options.map(option => option.label);
const selectedStates = options.map(
option => option.label as DETECTOR_STATE
);
setSelectedDetectorStates(selectedStates);
setAllDetectorStatesSelected(isEmpty(selectedStates));
};
Expand All @@ -134,9 +127,7 @@ export function DashboardOverview() {

const filterSelectedDetectors = async (
selectedNameList: string[],
// TODO: DetectorStates is placeholder for now until backend profile API is ready
// Issue link: https://github.com/opendistro-for-elasticsearch/anomaly-detection-kibana-plugin/issues/25
selectedStateList: string[],
selectedStateList: DETECTOR_STATE[],
selectedIndexList: string[]
) => {
let detectorsToFilter: DetectorListItem[];
Expand All @@ -156,7 +147,14 @@ export function DashboardOverview() {
);
}

setCurrentDetectors(filteredDetectorItemsByNamesAndIndex);
let finalFilteredDetectors = filteredDetectorItemsByNamesAndIndex;
if (!allDetectorStatesSelected) {
finalFilteredDetectors = filteredDetectorItemsByNamesAndIndex.filter(
detectorItem => selectedStateList.includes(detectorItem.curState)
);
}

setCurrentDetectors(finalFilteredDetectors);
};

const intializeDetectors = () => {
Expand Down Expand Up @@ -215,7 +213,7 @@ export function DashboardOverview() {
<EuiComboBox
id="detectorStateFilter"
placeholder={ALL_DETECTOR_STATES_MESSAGE}
options={getDetectorStateOptions(allDetectorStates)}
options={getDetectorStateOptions()}
onChange={handleDetectorStateFilterChange}
selectedOptions={selectedDetectorStates.map(buildItemOption)}
isClearable={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '@elastic/eui';
import React from 'react';
import { getDetectorStateOptions } from '../../utils/helpers';
import { DETECTOR_STATE } from 'public/pages/utils/constants';
import { DETECTOR_STATE } from '../../../../utils/constants';

interface ListControlsProps {
activePage: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
import { fireEvent, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import {
ALL_DETECTOR_STATES,
ALL_INDICES,
DETECTOR_STATE,
} from '../../../../utils/constants';
import { ALL_DETECTOR_STATES, ALL_INDICES } from '../../../../utils/constants';
import { ListControls } from '../ListControls';
import { DETECTOR_STATE } from '../../../../../utils/constants';

describe('<ListControls /> spec', () => {
const defaultProps = {
Expand Down
7 changes: 5 additions & 2 deletions public/pages/DetectorsList/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ import {
getIndices,
getPrioritizedIndices,
} from '../../../redux/reducers/elasticsearch';
import { APP_PATH, PLUGIN_NAME } from '../../../utils/constants';
import {
APP_PATH,
PLUGIN_NAME,
DETECTOR_STATE,
} from '../../../utils/constants';
import { getVisibleOptions, sanitizeSearchText } from '../../utils/helpers';
import { EmptyDetectorMessage } from '../Components/EmptyMessage/EmptyMessage';
import { ListControls } from '../Components/ListControls/ListControls';
Expand All @@ -52,7 +56,6 @@ import {
GET_ALL_DETECTORS_QUERY_PARAMS,
ALL_DETECTOR_STATES,
ALL_INDICES,
DETECTOR_STATE,
} from '../../utils/constants';
import { getURLQueryParams } from '../utils/helpers';
import {
Expand Down
2 changes: 1 addition & 1 deletion public/pages/DetectorsList/List/__tests__/List.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
initialDetectorsState,
} from '../../../../redux/reducers/ad';
import { DetectorList, ListRouterParams } from '../List';
import { DETECTOR_STATE } from '../../../utils/constants';
import { DETECTOR_STATE } from '../../../../utils/constants';

const renderWithRouter = (
initialAdState: Detectors = initialDetectorsState
Expand Down
3 changes: 2 additions & 1 deletion public/pages/DetectorsList/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import queryString from 'query-string';
import { GetDetectorsQueryParams } from '../../../../server/models/types';
import { SORT_DIRECTION } from '../../../../server/utils/constants';
import { DEFAULT_QUERY_PARAMS, DETECTOR_STATE } from '../../utils/constants';
import { DEFAULT_QUERY_PARAMS } from '../../utils/constants';
import { DETECTOR_STATE } from '../../../utils/constants';

export const getURLQueryParams = (location: {
search: string;
Expand Down
4 changes: 2 additions & 2 deletions public/pages/DetectorsList/utils/tableUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import moment from 'moment';
import get from 'lodash/get';
import React from 'react';
import { Detector } from '../../../../server/models/types';
import { PLUGIN_NAME } from '../../../utils/constants';
import { DETECTOR_STATE, stateToColorMap } from '../../utils/constants';
import { PLUGIN_NAME, DETECTOR_STATE } from '../../../utils/constants';
import { stateToColorMap } from '../../utils/constants';

export const DEFAULT_EMPTY_DATA = '-';

Expand Down
9 changes: 1 addition & 8 deletions public/pages/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@
*/

import { SORT_DIRECTION } from '../../../server/utils/constants';

export enum DETECTOR_STATE {
DISABLED = 'Disabled',
INIT = 'Initializing',
RUNNING = 'Running',
INIT_FAILURE = 'Initialization failure',
UNEXPECTED_FAILURE = 'Unexpected failure',
}
import { DETECTOR_STATE } from '../../utils/constants';

export enum DETECTOR_STATE_COLOR {
DISABLED = 'subdued',
Expand Down
3 changes: 2 additions & 1 deletion public/pages/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { CatIndex, IndexAlias } from '../../../server/models/types';
import sortBy from 'lodash/sortBy';
import { DetectorListItem } from '../../models/interfaces';
import { SORT_DIRECTION } from '../../../server/utils/constants';
import { ALL_INDICES, ALL_DETECTOR_STATES, DETECTOR_STATE } from './constants';
import { ALL_INDICES, ALL_DETECTOR_STATES } from './constants';
import { DETECTOR_STATE } from '../../utils/constants';

export function sanitizeSearchText(searchValue: string): string {
if (!searchValue || searchValue == '*') {
Expand Down
12 changes: 7 additions & 5 deletions public/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ export const MAX_DETECTORS = 1000;

export const MAX_ANOMALIES = 10000;

export const DETECTOR_STATE = Object.freeze({
DISABLED: 'Disabled',
INIT: 'Initializing',
RUNNING: 'Running',
});
export enum DETECTOR_STATE {
DISABLED = 'Disabled',
INIT = 'Initializing',
RUNNING = 'Running',
INIT_FAILURE = 'Initialization failure',
UNEXPECTED_FAILURE = 'Unexpected failure',
}
2 changes: 1 addition & 1 deletion server/routes/ad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
convertDetectorKeysToSnakeCase,
getResultAggregationQuery,
} from './utils/adHelpers';
import { DETECTOR_STATE } from '../../public/pages/utils/constants';
import { DETECTOR_STATE } from '../../public/utils/constants';

type PutDetectorParams = {
detectorId: string;
Expand Down

0 comments on commit 66bc75a

Please sign in to comment.