Skip to content

Commit 1d8232b

Browse files
[Discover] Unify definition of field names and field descriptions (#134463) (#134637)
* [Discover] Address "Don't call Hooks" React error message * [Discover] Unify definition of field names and field descriptions * [Discover] Bring back source message * [Discover] Update name function * [Discover] Revert source logic * [Discover] Update code style Co-authored-by: Kibana Machine <[email protected]> (cherry picked from commit c81da02) Co-authored-by: Julia Rechkunova <[email protected]>
1 parent 202efcd commit 1d8232b

File tree

5 files changed

+104
-54
lines changed

5 files changed

+104
-54
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
export enum KNOWN_FIELD_TYPES {
10+
BOOLEAN = 'boolean',
11+
CONFLICT = 'conflict',
12+
DATE = 'date',
13+
DATE_RANGE = 'date_range',
14+
GEO_POINT = 'geo_point',
15+
GEO_SHAPE = 'geo_shape',
16+
HISTOGRAM = 'histogram',
17+
IP = 'ip',
18+
IP_RANGE = 'ip_range',
19+
KEYWORD = 'keyword',
20+
MURMUR3 = 'murmur3',
21+
NUMBER = 'number',
22+
NESTED = 'nested',
23+
STRING = 'string',
24+
TEXT = 'text',
25+
VERSION = 'version',
26+
}

src/plugins/discover/public/application/main/components/sidebar/discover_field_search.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
import { FormattedMessage } from '@kbn/i18n-react';
3737
import { FieldIcon } from '@kbn/react-field';
3838
import { getFieldTypeDescription } from './lib/get_field_type_description';
39+
import { KNOWN_FIELD_TYPES } from '../../../../../common/field_types';
3940
import { useDiscoverServices } from '../../../../utils/use_discover_services';
4041

4142
export interface State {
@@ -107,7 +108,9 @@ export function DiscoverFieldSearch({ onChange, value, types, presentFieldTypes
107108
const { docLinks } = useDiscoverServices();
108109

109110
const items: FieldTypeTableItem[] = useMemo(() => {
111+
const knownTypes = Object.values(KNOWN_FIELD_TYPES) as string[];
110112
return presentFieldTypes
113+
.filter((element) => knownTypes.includes(element))
111114
.sort((one, another) => one.localeCompare(another))
112115
.map((element, index) => ({
113116
id: index,
@@ -122,7 +125,9 @@ export function DiscoverFieldSearch({ onChange, value, types, presentFieldTypes
122125
const columnsSidebar: Array<EuiBasicTableColumn<FieldTypeTableItem>> = [
123126
{
124127
field: 'dataType',
125-
name: 'Data type',
128+
name: i18n.translate('discover.fieldTypesPopover.dataTypeColumnTitle', {
129+
defaultMessage: 'Data type',
130+
}),
126131
width: '110px',
127132
render: (name: string) => (
128133
<EuiFlexGroup alignItems="center" responsive={false} gutterSize="xs">
@@ -135,7 +140,9 @@ export function DiscoverFieldSearch({ onChange, value, types, presentFieldTypes
135140
},
136141
{
137142
field: 'description',
138-
name: 'Description',
143+
name: i18n.translate('discover.fieldTypesPopover.descriptionColumnTitle', {
144+
defaultMessage: 'Description',
145+
}),
139146
// eslint-disable-next-line react/no-danger
140147
render: (description: string) => <div dangerouslySetInnerHTML={{ __html: description }} />,
141148
},

src/plugins/discover/public/application/main/components/sidebar/lib/get_field_type_description.ts

+29-20
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@
88

99
import type { DocLinksStart } from '@kbn/core/public';
1010
import { i18n } from '@kbn/i18n';
11+
import { KNOWN_FIELD_TYPES } from '../../../../../../common/field_types';
12+
13+
const UNKNOWN_FIELD_TYPE_DESC = i18n.translate('discover.fieldNameDescription.unknownField', {
14+
defaultMessage: 'Unknown field',
15+
});
1116

1217
export function getFieldTypeDescription(type: string, docLinks: DocLinksStart) {
13-
switch (type) {
14-
case 'boolean':
18+
const knownType: KNOWN_FIELD_TYPES = type as KNOWN_FIELD_TYPES;
19+
switch (knownType) {
20+
case KNOWN_FIELD_TYPES.BOOLEAN:
1521
return i18n.translate('discover.fieldNameDescription.booleanField', {
1622
defaultMessage: 'True and false values.',
1723
});
18-
case 'conflict':
24+
case KNOWN_FIELD_TYPES.CONFLICT:
1925
return i18n.translate('discover.fieldNameDescription.conflictField', {
2026
defaultMessage: 'Field has values of different types. Resolve in Management > Data Views.',
2127
});
22-
case 'date':
28+
case KNOWN_FIELD_TYPES.DATE:
2329
return i18n.translate('discover.fieldNameDescription.dateField', {
2430
defaultMessage: 'A date string or the number of seconds or milliseconds since 1/1/1970.',
2531
});
26-
case 'date_range':
32+
case KNOWN_FIELD_TYPES.DATE_RANGE:
2733
return i18n.translate('discover.fieldNameDescription.dateRangeField', {
2834
defaultMessage: 'Range of {dateFieldTypeLink} values. {viewSupportedDateFormatsLink}',
2935
values: {
@@ -43,49 +49,52 @@ export function getFieldTypeDescription(type: string, docLinks: DocLinksStart) {
4349
'</a>',
4450
},
4551
});
46-
case 'geo_point':
52+
case KNOWN_FIELD_TYPES.GEO_POINT:
4753
return i18n.translate('discover.fieldNameDescription.geoPointField', {
4854
defaultMessage: 'Latitude and longitude points.',
4955
});
50-
case 'geo_shape':
56+
case KNOWN_FIELD_TYPES.GEO_SHAPE:
5157
return i18n.translate('discover.fieldNameDescription.geoShapeField', {
5258
defaultMessage: 'Complex shapes, such as polygons.',
5359
});
54-
case 'ip':
60+
case KNOWN_FIELD_TYPES.HISTOGRAM:
61+
return i18n.translate('discover.fieldNameDescription.histogramField', {
62+
defaultMessage: 'Pre-aggregated numerical values in the form of a histogram.',
63+
});
64+
case KNOWN_FIELD_TYPES.IP:
5565
return i18n.translate('discover.fieldNameDescription.ipAddressField', {
5666
defaultMessage: 'IPv4 and IPv6 addresses.',
5767
});
58-
case 'ip_range':
68+
case KNOWN_FIELD_TYPES.IP_RANGE:
5969
return i18n.translate('discover.fieldNameDescription.ipAddressRangeField', {
6070
defaultMessage: 'Range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.',
6171
});
62-
case 'murmur3':
72+
case KNOWN_FIELD_TYPES.MURMUR3:
6373
return i18n.translate('discover.fieldNameDescription.murmur3Field', {
6474
defaultMessage: 'Field that computes and stores hashes of values.',
6575
});
66-
case 'number':
76+
case KNOWN_FIELD_TYPES.NUMBER:
6777
return i18n.translate('discover.fieldNameDescription.numberField', {
6878
defaultMessage: 'Long, integer, short, byte, double, and float values.',
6979
});
70-
case 'string':
80+
case KNOWN_FIELD_TYPES.STRING:
7181
return i18n.translate('discover.fieldNameDescription.stringField', {
7282
defaultMessage: 'Full text such as the body of an email or a product description.',
7383
});
74-
case 'text':
84+
case KNOWN_FIELD_TYPES.TEXT:
7585
return i18n.translate('discover.fieldNameDescription.textField', {
7686
defaultMessage: 'Full text such as the body of an email or a product description.',
7787
});
78-
case 'keyword':
88+
case KNOWN_FIELD_TYPES.KEYWORD:
7989
return i18n.translate('discover.fieldNameDescription.keywordField', {
8090
defaultMessage:
8191
'Structured content such as an ID, email address, hostname, status code, or tag.',
8292
});
83-
84-
case 'nested':
93+
case KNOWN_FIELD_TYPES.NESTED:
8594
return i18n.translate('discover.fieldNameDescription.nestedField', {
8695
defaultMessage: 'JSON object that preserves the relationship between its subfields.',
8796
});
88-
case 'version':
97+
case KNOWN_FIELD_TYPES.VERSION:
8998
return i18n.translate('discover.fieldNameDescription.versionField', {
9099
defaultMessage: 'Software versions. Supports {SemanticVersioningLink} precedence rules.',
91100
values: {
@@ -102,8 +111,8 @@ export function getFieldTypeDescription(type: string, docLinks: DocLinksStart) {
102111
},
103112
});
104113
default:
105-
return i18n.translate('discover.fieldNameDescription.unknownField', {
106-
defaultMessage: 'Unknown field',
107-
});
114+
// If you see a typescript error here, that's a sign that there are missing switch cases ^^
115+
const _exhaustiveCheck: never = knownType;
116+
return UNKNOWN_FIELD_TYPE_DESC || _exhaustiveCheck;
108117
}
109118
}

src/plugins/discover/public/utils/get_field_type_name.test.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
* Side Public License, v 1.
77
*/
88

9-
import {
10-
getFieldTypeName,
11-
KNOWN_FIELD_TYPES,
12-
UNKNOWN_FIELD_TYPE_MESSAGE,
13-
} from './get_field_type_name';
9+
import { getFieldTypeName, UNKNOWN_FIELD_TYPE_MESSAGE } from './get_field_type_name';
10+
import { KNOWN_FIELD_TYPES } from '../../common/field_types';
1411

1512
describe('getFieldTypeName', () => {
1613
describe('known field types should be recognized', () => {
@@ -28,7 +25,11 @@ describe('getFieldTypeName', () => {
2825
expect(getFieldTypeName(undefined)).toBe(UNKNOWN_FIELD_TYPE_MESSAGE);
2926
});
3027

31-
it(`should return '${UNKNOWN_FIELD_TYPE_MESSAGE}' when passed an unknown field type`, () => {
32-
expect(getFieldTypeName('unknown_field_type')).toBe(UNKNOWN_FIELD_TYPE_MESSAGE);
28+
it(`should return '${UNKNOWN_FIELD_TYPE_MESSAGE}' when passed 'unknown'`, () => {
29+
expect(getFieldTypeName('unknown')).toBe(UNKNOWN_FIELD_TYPE_MESSAGE);
30+
});
31+
32+
it('should return the original type string back when passed an unknown field type', () => {
33+
expect(getFieldTypeName('unknown_field_type')).toBe('unknown_field_type');
3334
});
3435
});

src/plugins/discover/public/utils/get_field_type_name.ts

+32-25
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,8 @@
77
*/
88

99
import { i18n } from '@kbn/i18n';
10-
import { KBN_FIELD_TYPES, ES_FIELD_TYPES } from '@kbn/data-plugin/public';
11-
12-
export const KNOWN_FIELD_TYPES = {
13-
BOOLEAN: KBN_FIELD_TYPES.BOOLEAN,
14-
CONFLICT: KBN_FIELD_TYPES.CONFLICT,
15-
DATE: KBN_FIELD_TYPES.DATE,
16-
GEO_POINT: KBN_FIELD_TYPES.GEO_POINT,
17-
GEO_SHAPE: KBN_FIELD_TYPES.GEO_SHAPE,
18-
IP: KBN_FIELD_TYPES.IP,
19-
KEYWORD: ES_FIELD_TYPES.KEYWORD,
20-
MURMUR3: KBN_FIELD_TYPES.MURMUR3,
21-
NUMBER: KBN_FIELD_TYPES.NUMBER,
22-
NESTED: KBN_FIELD_TYPES.NESTED,
23-
SOURCE: 'source',
24-
STRING: KBN_FIELD_TYPES.STRING,
25-
TEXT: ES_FIELD_TYPES.TEXT,
26-
VERSION: ES_FIELD_TYPES.VERSION,
27-
};
10+
import { KBN_FIELD_TYPES } from '@kbn/data-plugin/public';
11+
import { KNOWN_FIELD_TYPES } from '../../common/field_types';
2812

2913
export const UNKNOWN_FIELD_TYPE_MESSAGE = i18n.translate(
3014
'discover.fieldNameIcons.unknownFieldAriaLabel',
@@ -34,7 +18,21 @@ export const UNKNOWN_FIELD_TYPE_MESSAGE = i18n.translate(
3418
);
3519

3620
export function getFieldTypeName(type?: string) {
37-
switch (type) {
21+
if (!type || type === KBN_FIELD_TYPES.UNKNOWN) {
22+
return UNKNOWN_FIELD_TYPE_MESSAGE;
23+
}
24+
25+
if (type === 'source') {
26+
// TODO: check if we can remove this logic as outdated
27+
28+
// Note that this type is currently not provided, type for _source is undefined
29+
return i18n.translate('discover.fieldNameIcons.sourceFieldAriaLabel', {
30+
defaultMessage: 'Source field',
31+
});
32+
}
33+
34+
const knownType: KNOWN_FIELD_TYPES = type as KNOWN_FIELD_TYPES;
35+
switch (knownType) {
3836
case KNOWN_FIELD_TYPES.BOOLEAN:
3937
return i18n.translate('discover.fieldNameIcons.booleanAriaLabel', {
4038
defaultMessage: 'Boolean field',
@@ -47,6 +45,10 @@ export function getFieldTypeName(type?: string) {
4745
return i18n.translate('discover.fieldNameIcons.dateFieldAriaLabel', {
4846
defaultMessage: 'Date field',
4947
});
48+
case KNOWN_FIELD_TYPES.DATE_RANGE:
49+
return i18n.translate('discover.fieldNameIcons.dateRangeFieldAriaLabel', {
50+
defaultMessage: 'Date range field',
51+
});
5052
case KNOWN_FIELD_TYPES.GEO_POINT:
5153
return i18n.translate('discover.fieldNameIcons.geoPointFieldAriaLabel', {
5254
defaultMessage: 'Geo point field',
@@ -55,10 +57,18 @@ export function getFieldTypeName(type?: string) {
5557
return i18n.translate('discover.fieldNameIcons.geoShapeFieldAriaLabel', {
5658
defaultMessage: 'Geo shape field',
5759
});
60+
case KNOWN_FIELD_TYPES.HISTOGRAM:
61+
return i18n.translate('discover.fieldNameIcons.histogramFieldAriaLabel', {
62+
defaultMessage: 'Histogram field',
63+
});
5864
case KNOWN_FIELD_TYPES.IP:
5965
return i18n.translate('discover.fieldNameIcons.ipAddressFieldAriaLabel', {
6066
defaultMessage: 'IP address field',
6167
});
68+
case KNOWN_FIELD_TYPES.IP_RANGE:
69+
return i18n.translate('discover.fieldNameIcons.ipRangeFieldAriaLabel', {
70+
defaultMessage: 'IP range field',
71+
});
6272
case KNOWN_FIELD_TYPES.MURMUR3:
6373
return i18n.translate('discover.fieldNameIcons.murmur3FieldAriaLabel', {
6474
defaultMessage: 'Murmur3 field',
@@ -67,11 +77,6 @@ export function getFieldTypeName(type?: string) {
6777
return i18n.translate('discover.fieldNameIcons.numberFieldAriaLabel', {
6878
defaultMessage: 'Number field',
6979
});
70-
case KNOWN_FIELD_TYPES.SOURCE:
71-
// Note that this type is currently not provided, type for _source is undefined
72-
return i18n.translate('discover.fieldNameIcons.sourceFieldAriaLabel', {
73-
defaultMessage: 'Source field',
74-
});
7580
case KNOWN_FIELD_TYPES.STRING:
7681
return i18n.translate('discover.fieldNameIcons.stringFieldAriaLabel', {
7782
defaultMessage: 'String field',
@@ -93,6 +98,8 @@ export function getFieldTypeName(type?: string) {
9398
defaultMessage: 'Version field',
9499
});
95100
default:
96-
return UNKNOWN_FIELD_TYPE_MESSAGE;
101+
// If you see a typescript error here, that's a sign that there are missing switch cases ^^
102+
const _exhaustiveCheck: never = knownType;
103+
return knownType || _exhaustiveCheck;
97104
}
98105
}

0 commit comments

Comments
 (0)