|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { FC, useCallback, useEffect, useMemo, useState } from 'react'; |
| 9 | +import { i18n } from '@kbn/i18n'; |
| 10 | +import { FormattedMessage } from '@kbn/i18n/react'; |
| 11 | +import { EuiComboBox, EuiComboBoxOptionOption, EuiComboBoxProps, EuiFormRow } from '@elastic/eui'; |
| 12 | +import { JobId } from '../../common/types/anomaly_detection_jobs'; |
| 13 | +import { MlApiServices } from '../application/services/ml_api_service'; |
| 14 | + |
| 15 | +interface JobSelection { |
| 16 | + jobIds?: JobId[]; |
| 17 | + groupIds?: string[]; |
| 18 | +} |
| 19 | + |
| 20 | +export interface JobSelectorControlProps { |
| 21 | + jobSelection?: JobSelection; |
| 22 | + onSelectionChange: (jobSelection: JobSelection) => void; |
| 23 | + adJobsApiService: MlApiServices['jobs']; |
| 24 | + /** |
| 25 | + * Validation is handled by alerting framework |
| 26 | + */ |
| 27 | + errors: string[]; |
| 28 | +} |
| 29 | + |
| 30 | +export const JobSelectorControl: FC<JobSelectorControlProps> = ({ |
| 31 | + jobSelection, |
| 32 | + onSelectionChange, |
| 33 | + adJobsApiService, |
| 34 | + errors, |
| 35 | +}) => { |
| 36 | + const [options, setOptions] = useState<Array<EuiComboBoxOptionOption<string>>>([]); |
| 37 | + const jobIds = useMemo(() => new Set(), []); |
| 38 | + const groupIds = useMemo(() => new Set(), []); |
| 39 | + |
| 40 | + const fetchOptions = useCallback(async () => { |
| 41 | + try { |
| 42 | + const { |
| 43 | + jobIds: jobIdOptions, |
| 44 | + groupIds: groupIdOptions, |
| 45 | + } = await adJobsApiService.getAllJobAndGroupIds(); |
| 46 | + |
| 47 | + jobIdOptions.forEach((v) => { |
| 48 | + jobIds.add(v); |
| 49 | + }); |
| 50 | + groupIdOptions.forEach((v) => { |
| 51 | + groupIds.add(v); |
| 52 | + }); |
| 53 | + |
| 54 | + setOptions([ |
| 55 | + { |
| 56 | + label: i18n.translate('xpack.ml.jobSelector.jobOptionsLabel', { |
| 57 | + defaultMessage: 'Jobs', |
| 58 | + }), |
| 59 | + options: jobIdOptions.map((v) => ({ label: v })), |
| 60 | + }, |
| 61 | + { |
| 62 | + label: i18n.translate('xpack.ml.jobSelector.groupOptionsLabel', { |
| 63 | + defaultMessage: 'Groups', |
| 64 | + }), |
| 65 | + options: groupIdOptions.map((v) => ({ label: v })), |
| 66 | + }, |
| 67 | + ]); |
| 68 | + } catch (e) { |
| 69 | + // TODO add error handling |
| 70 | + } |
| 71 | + }, [adJobsApiService]); |
| 72 | + |
| 73 | + const onChange: EuiComboBoxProps<string>['onChange'] = useCallback( |
| 74 | + (selectedOptions) => { |
| 75 | + const selectedJobIds: JobId[] = []; |
| 76 | + const selectedGroupIds: string[] = []; |
| 77 | + selectedOptions.forEach(({ label }: { label: string }) => { |
| 78 | + if (jobIds.has(label)) { |
| 79 | + selectedJobIds.push(label); |
| 80 | + } else if (groupIds.has(label)) { |
| 81 | + selectedGroupIds.push(label); |
| 82 | + } |
| 83 | + }); |
| 84 | + onSelectionChange({ |
| 85 | + ...(selectedJobIds.length > 0 ? { jobIds: selectedJobIds } : {}), |
| 86 | + ...(selectedGroupIds.length > 0 ? { groupIds: selectedGroupIds } : {}), |
| 87 | + }); |
| 88 | + }, |
| 89 | + [jobIds, groupIds] |
| 90 | + ); |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + fetchOptions(); |
| 94 | + }, []); |
| 95 | + |
| 96 | + const selectedOptions = Object.values(jobSelection ?? {}) |
| 97 | + .flat() |
| 98 | + .map((v) => ({ |
| 99 | + label: v, |
| 100 | + })); |
| 101 | + |
| 102 | + return ( |
| 103 | + <EuiFormRow |
| 104 | + fullWidth |
| 105 | + label={ |
| 106 | + <FormattedMessage |
| 107 | + id="xpack.ml.jobSelector.formControlLabel" |
| 108 | + defaultMessage="Select jobs or groups" |
| 109 | + /> |
| 110 | + } |
| 111 | + isInvalid={!!errors?.length} |
| 112 | + error={errors} |
| 113 | + > |
| 114 | + <EuiComboBox<string> |
| 115 | + selectedOptions={selectedOptions} |
| 116 | + options={options} |
| 117 | + onChange={onChange} |
| 118 | + fullWidth |
| 119 | + data-test-subj={'mlAnomalyAlertJobSelection'} |
| 120 | + isInvalid={!!errors?.length} |
| 121 | + /> |
| 122 | + </EuiFormRow> |
| 123 | + ); |
| 124 | +}; |
0 commit comments