From ddb34ae6a8d3d1649651f775b31a1cba05d2866d Mon Sep 17 00:00:00 2001 From: Peter Pisljar Date: Thu, 19 Dec 2024 18:49:36 +0100 Subject: [PATCH] Lens IndexPattern getFormatterForField (#203964) --- x-pack/plugins/lens/public/data_views_service/loader.ts | 6 ++++++ x-pack/plugins/lens/public/data_views_service/mocks.ts | 1 + .../lens/public/datasources/form_based/datapanel.test.tsx | 2 ++ .../form_based/dimension_panel/dimension_panel.test.tsx | 2 ++ .../form_based/dimension_panel/droppable/mocks.ts | 2 ++ .../lens/public/datasources/form_based/form_based.test.ts | 3 +++ .../datasources/form_based/form_based_suggestions.test.tsx | 7 +++++++ .../lens/public/datasources/form_based/layerpanel.test.tsx | 3 +++ x-pack/plugins/lens/public/datasources/form_based/mocks.ts | 4 ++++ .../datasources/form_based/operations/definitions.test.ts | 1 + .../operations/definitions/date_histogram.test.tsx | 4 ++++ .../operations/definitions/ranges/ranges.test.tsx | 1 + .../form_based/operations/layer_helpers.test.ts | 2 ++ .../datasources/form_based/operations/operations.test.ts | 1 + .../datasources/text_based/components/datapanel.test.tsx | 1 + .../datasources/text_based/text_based_languages.test.ts | 1 + .../initializers/initialize_actions.test.ts | 1 + x-pack/plugins/lens/public/types.ts | 3 +++ 18 files changed, 45 insertions(+) diff --git a/x-pack/plugins/lens/public/data_views_service/loader.ts b/x-pack/plugins/lens/public/data_views_service/loader.ts index 37c768de6ef8f..16468ff01a22d 100644 --- a/x-pack/plugins/lens/public/data_views_service/loader.ts +++ b/x-pack/plugins/lens/public/data_views_service/loader.ts @@ -72,6 +72,12 @@ export function convertDataViewIntoLensIndexPattern( ]) ), fields: newFields, + getFormatterForField(sourceField: string): unknown { + const dvField = dataView.getFieldByName(sourceField); + if (dvField) { + return dataView.getFormatterForField(dvField); + } + }, getFieldByName: getFieldByNameFactory(newFields), hasRestrictions: !!typeMeta?.aggs, spec: dataView.toSpec(false), diff --git a/x-pack/plugins/lens/public/data_views_service/mocks.ts b/x-pack/plugins/lens/public/data_views_service/mocks.ts index b4acacbe98b73..b5db98c16ec18 100644 --- a/x-pack/plugins/lens/public/data_views_service/mocks.ts +++ b/x-pack/plugins/lens/public/data_views_service/mocks.ts @@ -48,6 +48,7 @@ const indexPattern1 = { hasRestrictions: false, isPersisted: () => true, toSpec: () => ({}), + getFormatterForField: () => ({ convert: (v: unknown) => v }), fields: [ { name: 'timestamp', diff --git a/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx index c8fec4184190d..9208b0f4f7f25 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx @@ -165,6 +165,7 @@ const indexPatterns = { hasRestrictions: false, fields: fieldsOne, getFieldByName: getFieldByNameFactory(fieldsOne), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -175,6 +176,7 @@ const indexPatterns = { hasRestrictions: true, fields: fieldsTwo, getFieldByName: getFieldByNameFactory(fieldsTwo), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx index aa6837a7e3393..ff08447f2a247 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx @@ -150,6 +150,7 @@ const expectedIndexPatterns = { hasRestrictions: false, fields, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -2377,6 +2378,7 @@ describe('FormBasedDimensionEditor', () => { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts index ceb16345f475c..4cf0171de9e0f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts @@ -77,6 +77,7 @@ export const mockDataViews = (): IndexPatternMap => { hasRestrictions: false, fields, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -87,6 +88,7 @@ export const mockDataViews = (): IndexPatternMap => { timeFieldName: 'timestamp', fields: [fields[0]], getFieldByName: getFieldByNameFactory([fields[0]]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts index cd26abe0fdd86..77063a2e1a739 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts @@ -171,6 +171,7 @@ const expectedIndexPatterns = { hasRestrictions: false, fields: fieldsOne, getFieldByName: getFieldByNameFactory(fieldsOne), + getFormatterForField: () => ({ convert: (v: unknown) => v }), spec: {}, isPersisted: true, }, @@ -181,6 +182,7 @@ const expectedIndexPatterns = { hasRestrictions: true, fields: fieldsTwo, getFieldByName: getFieldByNameFactory(fieldsTwo), + getFormatterForField: () => ({ convert: (v: unknown) => v }), spec: {}, isPersisted: true, }, @@ -3032,6 +3034,7 @@ describe('IndexPattern Data Source', () => { hasRestrictions: false, fields: fieldsOne, getFieldByName: getFieldByNameFactory(fieldsOne), + getFormatterForField: () => ({ convert: (v: unknown) => v }), spec: {}, isPersisted: true, }; diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx index 0a0c0dcc05eeb..923c0c814c326 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx @@ -156,6 +156,7 @@ const expectedIndexPatterns = { hasRestrictions: false, fields: fieldsOne, getFieldByName: getFieldByNameFactory(fieldsOne), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -166,6 +167,7 @@ const expectedIndexPatterns = { timeFieldName: 'timestamp', fields: fieldsTwo, getFieldByName: getFieldByNameFactory(fieldsTwo), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -420,6 +422,7 @@ describe('IndexPattern Data Source suggestions', () => { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -664,6 +667,7 @@ describe('IndexPattern Data Source suggestions', () => { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -2899,6 +2903,7 @@ describe('IndexPattern Data Source suggestions', () => { hasRestrictions: false, fields, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -2996,6 +3001,7 @@ describe('IndexPattern Data Source suggestions', () => { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -3078,6 +3084,7 @@ describe('IndexPattern Data Source suggestions', () => { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx index bbcecf15d2bd1..93902547dd603 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx @@ -194,6 +194,7 @@ describe('Layer Data Panel', () => { hasRestrictions: false, fields: fieldsOne, getFieldByName: getFieldByNameFactory(fieldsOne), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -204,6 +205,7 @@ describe('Layer Data Panel', () => { timeFieldName: 'timestamp', fields: fieldsTwo, getFieldByName: getFieldByNameFactory(fieldsTwo), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, @@ -214,6 +216,7 @@ describe('Layer Data Panel', () => { hasRestrictions: false, fields: fieldsThree, getFieldByName: getFieldByNameFactory(fieldsThree), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/form_based/mocks.ts b/x-pack/plugins/lens/public/datasources/form_based/mocks.ts index f98107eebbcca..f625c8b192d66 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/mocks.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/mocks.ts @@ -94,6 +94,7 @@ export const createMockedIndexPattern = ( hasRestrictions: false, fields, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, ...someProps, @@ -128,6 +129,7 @@ export const createMockedRestrictedIndexPattern = () => { fieldFormatMap: { bytes: { id: 'bytes', params: { pattern: '0.0' } } }, fields, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, typeMeta: { @@ -188,6 +190,7 @@ export const createMockedIndexPatternWithoutType = ( ...otherIndexPatternProps, fields: filteredFields, getFieldByName: getFieldByNameFactory(filteredFields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), }; }; @@ -200,5 +203,6 @@ export const createMockedIndexPatternWithAdditionalFields = ( ...otherIndexPatternProps, fields: completeFields, getFieldByName: getFieldByNameFactory(completeFields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), }; }; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts index 3dc58b7f1ef6c..94b406039284c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts @@ -76,6 +76,7 @@ const indexPattern = { hasRestrictions: false, fields: indexPatternFields, getFieldByName: getFieldByNameFactory([...indexPatternFields]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx index fb1c95f57a8f3..0a49a10ada25f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx @@ -60,6 +60,7 @@ const indexPattern1: IndexPattern = { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }; @@ -88,6 +89,7 @@ const indexPattern2: IndexPattern = { searchable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }; @@ -248,6 +250,7 @@ describe('date_histogram', () => { }, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), }, layer, uiSettingsMock, @@ -709,6 +712,7 @@ describe('date_histogram', () => { }, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), }; const instance = shallow( diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx index 8bf71089b21e0..a7c6e436dcfab 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx @@ -116,6 +116,7 @@ const defaultOptions = { aggregatable: true, }, ]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts b/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts index 11ea2a7b18414..a7aafc0c272d7 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts @@ -111,6 +111,7 @@ const indexPattern = { hasRestrictions: false, fields: indexPatternFields, getFieldByName: getFieldByNameFactory([...indexPatternFields, documentField]), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }; @@ -2902,6 +2903,7 @@ describe('state_helpers', () => { title: '', hasRestrictions: true, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), fields, isPersisted: true, spec: {}, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts b/x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts index 1f6f041b4eee2..fa9063183eb9d 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts @@ -42,6 +42,7 @@ const expectedIndexPatterns = { hasRestrictions: false, fields, getFieldByName: getFieldByNameFactory(fields), + getFormatterForField: () => ({ convert: (v: unknown) => v }), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/text_based/components/datapanel.test.tsx b/x-pack/plugins/lens/public/datasources/text_based/components/datapanel.test.tsx index 878abc2e7f66a..f66ef72296b13 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/components/datapanel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/text_based/components/datapanel.test.tsx @@ -144,6 +144,7 @@ describe('TextBased Query Languages Data Panel', () => { hasRestrictions: false, fields: fieldsOne, getFieldByName: jest.fn(), + getFormatterForField: jest.fn(), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts b/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts index ab96f6d802a97..7c2d546587c3e 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts +++ b/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts @@ -69,6 +69,7 @@ const expectedIndexPatterns = { hasRestrictions: false, fields: fieldsOne, getFieldByName: jest.fn(), + getFormatterForField: jest.fn(), spec: {}, isPersisted: true, }, diff --git a/x-pack/plugins/lens/public/react_embeddable/initializers/initialize_actions.test.ts b/x-pack/plugins/lens/public/react_embeddable/initializers/initialize_actions.test.ts index d7a073f10e024..ae2e170e5422f 100644 --- a/x-pack/plugins/lens/public/react_embeddable/initializers/initialize_actions.test.ts +++ b/x-pack/plugins/lens/public/react_embeddable/initializers/initialize_actions.test.ts @@ -109,6 +109,7 @@ describe('Dashboard actions', () => { }, ], getFieldByName: jest.fn(), + getFormatterForField: jest.fn(), isPersisted: true, spec: {}, }, diff --git a/x-pack/plugins/lens/public/types.ts b/x-pack/plugins/lens/public/types.ts index 9b44a951cfcd6..4aff41450df4c 100644 --- a/x-pack/plugins/lens/public/types.ts +++ b/x-pack/plugins/lens/public/types.ts @@ -83,6 +83,9 @@ export interface IndexPatternRef { } export interface IndexPattern { + getFormatterForField( // used extensively in lens + sourceField: string + ): unknown; id: string; fields: IndexPatternField[]; getFieldByName(name: string): IndexPatternField | undefined;