-
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.
[Discover] Add log level badge cell renderer for logs profile (#188281)
## Summary This PR adds a log level badge cell render for `log.level` and `log_level` fields when the logs data source profile is active in Discover: ![log_level_badge](https://github.com/user-attachments/assets/fa90d5d6-538c-4b9e-bad5-912db23ee41c) Resolves #186567. ### Checklist - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
- Loading branch information
1 parent
5f843a8
commit ab8bfda
Showing
17 changed files
with
417 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
63 changes: 63 additions & 0 deletions
63
src/plugins/discover/public/components/data_types/logs/log_level_badge_cell.test.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,63 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { buildDataTableRecord, DataTableRecord } from '@kbn/discover-utils'; | ||
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__'; | ||
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks'; | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { getLogLevelBadgeCell } from './log_level_badge_cell'; | ||
|
||
const renderCell = (logLevelField: string, record: DataTableRecord) => { | ||
const LogLevelBadgeCell = getLogLevelBadgeCell(logLevelField); | ||
render( | ||
<LogLevelBadgeCell | ||
rowIndex={0} | ||
colIndex={0} | ||
columnId="log.level" | ||
isExpandable={false} | ||
isExpanded={false} | ||
isDetails={false} | ||
row={record} | ||
dataView={dataViewMock} | ||
fieldFormats={fieldFormatsMock} | ||
setCellProps={() => {}} | ||
closePopover={() => {}} | ||
/> | ||
); | ||
}; | ||
|
||
describe('getLogLevelBadgeCell', () => { | ||
it('renders badge with color based on provided logLevelField', () => { | ||
const record = buildDataTableRecord({ fields: { 'log.level': 'info' } }, dataViewMock); | ||
renderCell('log.level', record); | ||
const badge = screen.getByTestId('logLevelBadgeCell-info'); | ||
expect(badge).toBeInTheDocument(); | ||
expect(badge).toHaveTextContent('info'); | ||
expect(getComputedStyle(badge).getPropertyValue('--euiBadgeBackgroundColor')).toEqual( | ||
'#90b0d1' | ||
); | ||
}); | ||
|
||
it('renders unknown badge if logLevelField is not recognized', () => { | ||
const record = buildDataTableRecord({ fields: { 'log.level': 'unknown_level' } }, dataViewMock); | ||
renderCell('log.level', record); | ||
const badge = screen.getByTestId('logLevelBadgeCell-unknown'); | ||
expect(badge).toBeInTheDocument(); | ||
expect(badge).toHaveTextContent('unknown_level'); | ||
expect(getComputedStyle(badge).getPropertyValue('--euiBadgeBackgroundColor')).toEqual(''); | ||
}); | ||
|
||
it('renders empty if no matching logLevelField is found', () => { | ||
const record = buildDataTableRecord({ fields: { 'log.level': 'info' } }, dataViewMock); | ||
renderCell('log_level', record); | ||
const badge = screen.getByTestId('logLevelBadgeCell-empty'); | ||
expect(badge).toBeInTheDocument(); | ||
expect(badge).toHaveTextContent('-'); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/plugins/discover/public/components/data_types/logs/log_level_badge_cell.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,42 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { EuiBadge, mathWithUnits, useEuiTheme } from '@elastic/eui'; | ||
import type { CSSObject } from '@emotion/react'; | ||
import { getLogLevelCoalescedValue, getLogLevelColor } from '@kbn/discover-utils'; | ||
import { euiThemeVars } from '@kbn/ui-theme'; | ||
import type { DataGridCellValueElementProps } from '@kbn/unified-data-table'; | ||
import React from 'react'; | ||
|
||
const badgeCss: CSSObject = { | ||
marginTop: '-4px', | ||
maxWidth: mathWithUnits(euiThemeVars.euiSize, (size) => size * 7.5), | ||
}; | ||
|
||
export const getLogLevelBadgeCell = | ||
(logLevelField: string) => (props: DataGridCellValueElementProps) => { | ||
const { euiTheme } = useEuiTheme(); | ||
const value = props.row.flattened[logLevelField]; | ||
|
||
if (!value) { | ||
return <span data-test-subj="logLevelBadgeCell-empty">-</span>; | ||
} | ||
|
||
const coalescedValue = getLogLevelCoalescedValue(value); | ||
const color = coalescedValue ? getLogLevelColor(coalescedValue, euiTheme) : undefined; | ||
|
||
if (!color || !coalescedValue) { | ||
return <span data-test-subj="logLevelBadgeCell-unknown">{value}</span>; | ||
} | ||
|
||
return ( | ||
<EuiBadge color={color} data-test-subj={`logLevelBadgeCell-${coalescedValue}`} css={badgeCss}> | ||
{value} | ||
</EuiBadge> | ||
); | ||
}; |
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
24 changes: 24 additions & 0 deletions
24
...ext_awareness/profile_providers/logs_data_source_profile/accessors/get_cell_renderers.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,24 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { LOG_LEVEL_FIELDS } from '../../../../../common/data_types/logs/constants'; | ||
import { getLogLevelBadgeCell } from '../../../../components/data_types/logs/log_level_badge_cell'; | ||
import type { DataSourceProfileProvider } from '../../../profiles'; | ||
|
||
export const getCellRenderers: DataSourceProfileProvider['profile']['getCellRenderers'] = | ||
(prev) => () => ({ | ||
...prev(), | ||
...LOG_LEVEL_FIELDS.reduce( | ||
(acc, field) => ({ | ||
...acc, | ||
[field]: getLogLevelBadgeCell(field), | ||
[`${field}.keyword`]: getLogLevelBadgeCell(`${field}.keyword`), | ||
}), | ||
{} | ||
), | ||
}); |
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
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
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
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
Oops, something went wrong.