-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
902f6db
commit c195e14
Showing
12 changed files
with
247 additions
and
91 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/security_solution/public/common/utils/normalize_machine_learning_job_id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { MachineLearningJobId } from '../../../common/api/detection_engine'; | ||
|
||
export function normalizeMachineLearningJobId(jobId: MachineLearningJobId): string[] { | ||
return typeof jobId === 'string' ? [jobId] : jobId; | ||
} |
9 changes: 9 additions & 0 deletions
9
...on/public/detection_engine/rule_creation/components/machine_learning_job_id_edit/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { MachineLearningJobIdEdit } from './machine_learning_job_id_edit'; | ||
export { MachineLearningJobSelector } from './machine_learning_job_selector'; |
22 changes: 22 additions & 0 deletions
22
...ne/rule_creation/components/machine_learning_job_id_edit/machine_learning_job_id_edit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { UseField } from '../../../../shared_imports'; | ||
import { MlJobSelect } from '../ml_job_select'; | ||
|
||
const componentProps = { | ||
describedByIds: ['machineLearningJobId'], | ||
}; | ||
|
||
interface MachineLearningJobIdEditProps { | ||
path: string; | ||
} | ||
|
||
export function MachineLearningJobIdEdit({ path }: MachineLearningJobIdEditProps): JSX.Element { | ||
return <UseField path={path} component={MlJobSelect} componentProps={componentProps} />; | ||
} |
28 changes: 28 additions & 0 deletions
28
...e/rule_creation/components/machine_learning_job_id_edit/machine_learning_job_selector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useMemo } from 'react'; | ||
import { UseField } from '../../../../shared_imports'; | ||
import { MlJobComboBox } from '../ml_job_select/ml_job_combobox'; | ||
import { useSecurityJobs } from '../../../../common/components/ml_popover/hooks/use_security_jobs'; | ||
|
||
interface MachineLearningJobIdEditProps { | ||
path: string; | ||
} | ||
|
||
export function MachineLearningJobSelector({ path }: MachineLearningJobIdEditProps): JSX.Element { | ||
const { loading, jobs } = useSecurityJobs(); | ||
|
||
const componentProps = useMemo(() => { | ||
return { | ||
jobs, | ||
loading, | ||
}; | ||
}, [jobs, loading]); | ||
|
||
return <UseField path={path} component={MlJobComboBox} componentProps={componentProps} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...lution/public/detection_engine/rule_creation/components/ml_job_select/ml_job_combobox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useCallback } from 'react'; | ||
import styled from 'styled-components'; | ||
import { EuiComboBox, EuiToolTip, EuiText } from '@elastic/eui'; | ||
import type { MlJobOption, MlJobValue } from './types'; | ||
import type { FieldHook } from '../../../../shared_imports'; | ||
import type { SecurityJob } from '../../../../common/components/ml_popover/types'; | ||
import * as i18n from './translations'; | ||
|
||
interface MlJobComboBoxProps { | ||
field: FieldHook; | ||
isLoading: boolean; | ||
jobs: SecurityJob[]; | ||
} | ||
|
||
export function MlJobComboBox({ field, isLoading, jobs }: MlJobComboBoxProps) { | ||
const jobIds = field.value as string[]; | ||
|
||
const jobOptions = jobs.map((job) => ({ | ||
value: { | ||
id: job.id, | ||
description: job.description, | ||
name: job.customSettings?.security_app_display_name, | ||
}, | ||
// Make sure users can search for id or name. | ||
// The label contains the name and id because EuiComboBox uses it for the textual search. | ||
label: `${job.customSettings?.security_app_display_name} ${job.id}`, | ||
})); | ||
|
||
const handleJobSelect = useCallback( | ||
(selectedJobOptions: MlJobOption[]): void => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const selectedJobIds = selectedJobOptions.map((option) => option.value!.id); | ||
field.setValue(selectedJobIds); | ||
}, | ||
[field] | ||
); | ||
|
||
const selectedJobOptions = jobOptions | ||
.filter((option) => jobIds.includes(option.value.id)) | ||
// 'label' defines what is rendered inside the selected ComboBoxPill | ||
.map((options) => ({ ...options, label: options.value.name ?? options.value.id })); | ||
|
||
return ( | ||
<EuiComboBox | ||
isLoading={isLoading} | ||
onChange={handleJobSelect} | ||
options={jobOptions} | ||
placeholder={i18n.ML_JOB_SELECT_PLACEHOLDER_TEXT} | ||
renderOption={renderJobOption} | ||
rowHeight={50} | ||
selectedOptions={selectedJobOptions} | ||
/> | ||
); | ||
} | ||
|
||
const renderJobOption = (option: MlJobOption) => ( | ||
<JobDisplay | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
id={option.value!.id} | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
description={option.value!.description} | ||
name={option.value?.name} | ||
/> | ||
); | ||
|
||
const JobDisplay: React.FC<MlJobValue> = ({ description, name, id }) => ( | ||
<JobDisplayContainer> | ||
<strong>{name ?? id}</strong> | ||
<EuiToolTip content={description}> | ||
<EuiText size="xs" color="subdued"> | ||
<p>{description}</p> | ||
</EuiText> | ||
</EuiToolTip> | ||
</JobDisplayContainer> | ||
); | ||
|
||
const JobDisplayContainer = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
`; |
16 changes: 16 additions & 0 deletions
16
...security_solution/public/detection_engine/rule_creation/components/ml_job_select/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { EuiComboBoxOptionOption } from '@elastic/eui'; | ||
|
||
export interface MlJobValue { | ||
id: string; | ||
description: string; | ||
name?: string; | ||
} | ||
|
||
export type MlJobOption = EuiComboBoxOptionOption<MlJobValue>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ee_way_diff/final_edit/fields/machine_learning_job_id/machine_learning_job_id_adapter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { MachineLearningJobSelector } from '../../../../../../../rule_creation/components/machine_learning_job_id_edit/machine_learning_job_selector'; | ||
|
||
export function MachineLearningJobIdAdapter(): JSX.Element { | ||
return <MachineLearningJobSelector path="machineLearningJobId" />; | ||
} |
47 changes: 47 additions & 0 deletions
47
...three_way_diff/final_edit/fields/machine_learning_job_id/machine_learning_job_id_form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import type { FormData, FormSchema } from '../../../../../../../../shared_imports'; | ||
import { schema } from '../../../../../../../rule_creation_ui/components/step_define_rule/schema'; | ||
import { RuleFieldEditFormWrapper } from '../rule_field_edit_form_wrapper'; | ||
import { type MachineLearningJobId } from '../../../../../../../../../common/api/detection_engine'; | ||
import { normalizeMachineLearningJobId } from '../../../../../../../../common/utils/normalize_machine_learning_job_id'; | ||
import { MachineLearningJobIdAdapter } from './machine_learning_job_id_adapter'; | ||
|
||
interface MachineLearningJobIdFormData { | ||
machineLearningJobId: MachineLearningJobId; | ||
} | ||
|
||
export function MachineLearningJobIdForm(): JSX.Element { | ||
return ( | ||
<RuleFieldEditFormWrapper | ||
component={MachineLearningJobIdAdapter} | ||
ruleFieldFormSchema={machineLearningJobIdSchema} | ||
deserializer={deserializer} | ||
serializer={serializer} | ||
/> | ||
); | ||
} | ||
|
||
function deserializer(defaultValue: FormData): MachineLearningJobIdFormData { | ||
return { | ||
machineLearningJobId: normalizeMachineLearningJobId(defaultValue.machine_learning_job_id), | ||
}; | ||
} | ||
|
||
function serializer(formData: FormData): { | ||
machine_learning_job_id: MachineLearningJobId; | ||
} { | ||
return { | ||
machine_learning_job_id: formData.machineLearningJobId, | ||
}; | ||
} | ||
|
||
const machineLearningJobIdSchema = { | ||
machineLearningJobId: schema.machineLearningJobId, | ||
} as FormSchema<MachineLearningJobIdFormData>; |
Oops, something went wrong.