Skip to content

Commit

Permalink
Lens IndexPattern getFormatterForField (#203964)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Dec 19, 2024
1 parent 4e9ef87 commit ddb34ae
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions x-pack/plugins/lens/public/data_views_service/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/data_views_service/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const indexPattern1 = {
hasRestrictions: false,
isPersisted: () => true,
toSpec: () => ({}),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
fields: [
{
name: 'timestamp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const indexPatterns = {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: getFieldByNameFactory(fieldsOne),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand All @@ -175,6 +176,7 @@ const indexPatterns = {
hasRestrictions: true,
fields: fieldsTwo,
getFieldByName: getFieldByNameFactory(fieldsTwo),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const expectedIndexPatterns = {
hasRestrictions: false,
fields,
getFieldByName: getFieldByNameFactory(fields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down Expand Up @@ -2377,6 +2378,7 @@ describe('FormBasedDimensionEditor', () => {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const mockDataViews = (): IndexPatternMap => {
hasRestrictions: false,
fields,
getFieldByName: getFieldByNameFactory(fields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand All @@ -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: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const expectedIndexPatterns = {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: getFieldByNameFactory(fieldsOne),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
spec: {},
isPersisted: true,
},
Expand All @@ -181,6 +182,7 @@ const expectedIndexPatterns = {
hasRestrictions: true,
fields: fieldsTwo,
getFieldByName: getFieldByNameFactory(fieldsTwo),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
spec: {},
isPersisted: true,
},
Expand Down Expand Up @@ -3032,6 +3034,7 @@ describe('IndexPattern Data Source', () => {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: getFieldByNameFactory(fieldsOne),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
spec: {},
isPersisted: true,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const expectedIndexPatterns = {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: getFieldByNameFactory(fieldsOne),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand All @@ -166,6 +167,7 @@ const expectedIndexPatterns = {
timeFieldName: 'timestamp',
fields: fieldsTwo,
getFieldByName: getFieldByNameFactory(fieldsTwo),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down Expand Up @@ -420,6 +422,7 @@ describe('IndexPattern Data Source suggestions', () => {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down Expand Up @@ -664,6 +667,7 @@ describe('IndexPattern Data Source suggestions', () => {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down Expand Up @@ -2899,6 +2903,7 @@ describe('IndexPattern Data Source suggestions', () => {
hasRestrictions: false,
fields,
getFieldByName: getFieldByNameFactory(fields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down Expand Up @@ -2996,6 +3001,7 @@ describe('IndexPattern Data Source suggestions', () => {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down Expand Up @@ -3078,6 +3084,7 @@ describe('IndexPattern Data Source suggestions', () => {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ describe('Layer Data Panel', () => {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: getFieldByNameFactory(fieldsOne),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand All @@ -204,6 +205,7 @@ describe('Layer Data Panel', () => {
timeFieldName: 'timestamp',
fields: fieldsTwo,
getFieldByName: getFieldByNameFactory(fieldsTwo),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand All @@ -214,6 +216,7 @@ describe('Layer Data Panel', () => {
hasRestrictions: false,
fields: fieldsThree,
getFieldByName: getFieldByNameFactory(fieldsThree),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/lens/public/datasources/form_based/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const createMockedIndexPattern = (
hasRestrictions: false,
fields,
getFieldByName: getFieldByNameFactory(fields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
...someProps,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -188,6 +190,7 @@ export const createMockedIndexPatternWithoutType = (
...otherIndexPatternProps,
fields: filteredFields,
getFieldByName: getFieldByNameFactory(filteredFields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
};
};

Expand All @@ -200,5 +203,6 @@ export const createMockedIndexPatternWithAdditionalFields = (
...otherIndexPatternProps,
fields: completeFields,
getFieldByName: getFieldByNameFactory(completeFields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const indexPattern = {
hasRestrictions: false,
fields: indexPatternFields,
getFieldByName: getFieldByNameFactory([...indexPatternFields]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const indexPattern1: IndexPattern = {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
};
Expand Down Expand Up @@ -88,6 +89,7 @@ const indexPattern2: IndexPattern = {
searchable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
};
Expand Down Expand Up @@ -248,6 +250,7 @@ describe('date_histogram', () => {
},
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
},
layer,
uiSettingsMock,
Expand Down Expand Up @@ -709,6 +712,7 @@ describe('date_histogram', () => {
},
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
};

const instance = shallow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const defaultOptions = {
aggregatable: true,
},
]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const indexPattern = {
hasRestrictions: false,
fields: indexPatternFields,
getFieldByName: getFieldByNameFactory([...indexPatternFields, documentField]),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
};
Expand Down Expand Up @@ -2902,6 +2903,7 @@ describe('state_helpers', () => {
title: '',
hasRestrictions: true,
getFieldByName: getFieldByNameFactory(fields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
fields,
isPersisted: true,
spec: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const expectedIndexPatterns = {
hasRestrictions: false,
fields,
getFieldByName: getFieldByNameFactory(fields),
getFormatterForField: () => ({ convert: (v: unknown) => v }),
isPersisted: true,
spec: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ describe('TextBased Query Languages Data Panel', () => {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: jest.fn(),
getFormatterForField: jest.fn(),
isPersisted: true,
spec: {},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const expectedIndexPatterns = {
hasRestrictions: false,
fields: fieldsOne,
getFieldByName: jest.fn(),
getFormatterForField: jest.fn(),
spec: {},
isPersisted: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ describe('Dashboard actions', () => {
},
],
getFieldByName: jest.fn(),
getFormatterForField: jest.fn(),
isPersisted: true,
spec: {},
},
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ddb34ae

Please sign in to comment.