-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[AI4DSOC] Add cell renderer for datetime fields to the alert summary table #219126
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * 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 { render } from '@testing-library/react'; | ||
| import type { Alert } from '@kbn/alerting-types'; | ||
| import { TestProviders } from '../../../../common/mock'; | ||
| import { getEmptyValue } from '../../../../common/components/empty_value'; | ||
| import { DatetimeSchemaCellRenderer } from './datetime_schema_cell_renderer'; | ||
|
|
||
| describe('DatetimeSchemaCellRenderer', () => { | ||
| it('should handle missing field', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: 'value1', | ||
| }; | ||
| const field = 'field'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={field} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText(getEmptyValue())).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should handle string value', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: 'value1', | ||
| }; | ||
| const field = 'field1'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={field} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText('value1')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should handle number value', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: 123, | ||
| }; | ||
| const columnId = 'field1'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={columnId} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText('Jan 1, 1970 @ 00:00:00.123')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should handle array of booleans', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: [true, false], | ||
| }; | ||
| const field = 'field1'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={field} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText('true')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should handle array of numbers', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: [1, 2], | ||
| }; | ||
| const field = 'field1'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={field} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText('Jan 1, 1970 @ 00:00:00.001')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should handle array of null', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: [null, null], | ||
| }; | ||
| const field = 'field1'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={field} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText('—')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should join array of JsonObjects', () => { | ||
| const alert: Alert = { | ||
| _id: '_id', | ||
| _index: '_index', | ||
| field1: [{ subField1: 'value1', subField2: 'value2' }], | ||
| }; | ||
| const field = 'field1'; | ||
|
|
||
| const { getByText } = render( | ||
| <TestProviders> | ||
| <DatetimeSchemaCellRenderer alert={alert} field={field} /> | ||
| </TestProviders> | ||
| ); | ||
|
|
||
| expect(getByText('[object Object]')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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, { memo, useMemo } from 'react'; | ||
| import type { Alert } from '@kbn/alerting-types'; | ||
| import { FormattedDate } from '../../../../common/components/formatted_date'; | ||
| import { getAlertFieldValueAsStringOrNumberOrNull } from '../../../utils/type_utils'; | ||
|
|
||
| export interface DatetimeSchemaCellRendererProps { | ||
| /** | ||
| * Alert data passed from the renderCellValue callback via the AlertWithLegacyFormats interface | ||
| */ | ||
| alert: Alert; | ||
| /** | ||
| * Column id passed from the renderCellValue callback via EuiDataGridProps['renderCellValue'] interface | ||
| */ | ||
| field: string; | ||
| } | ||
|
|
||
| /** | ||
| * Renders the value of a field of type date (when the schema is 'datetime'). | ||
| * Component used in the AI for SOC alert summary table. | ||
| */ | ||
| export const DatetimeSchemaCellRenderer = memo( | ||
| ({ alert, field }: DatetimeSchemaCellRendererProps) => { | ||
| const displayValue: number | string | null = useMemo( | ||
| () => getAlertFieldValueAsStringOrNumberOrNull(alert, field), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what a function name 😅
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, yeah... at least it's clear? 😆 In a follow up PR (after RSA) I will move all the renderers into a folder and clean up these names a bit... |
||
| [alert, field] | ||
| ); | ||
|
|
||
| return <FormattedDate fieldName={field} value={displayValue} />; | ||
| } | ||
| ); | ||
|
|
||
| DatetimeSchemaCellRenderer.displayName = 'DatetimeSchemaCellRenderer'; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still don't know how to feel about this 😓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know...Problem for future me I think 🤣 The
FormattedDatecomponent I'm leveraging in this PR is actually a bit weird. It displays a date if it's valid, and if not it fallback to the original value. I would have expected it to display a date if valid, and if not, just display-...But this PR shouldn't be changing this logic as it's used in other places...