Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Provide a field, get its formatter
<b>Signature:</b>

```typescript
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType): FieldFormat;
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType, params?: Record<string, any>): FieldFormat;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| field | <code>IndexPatternField &#124; IndexPatternField['spec'] &#124; IFieldType</code> | |
| params | <code>Record&lt;string, any&gt;</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Get formatter for a given field name. Return undefined if none exists
<b>Signature:</b>

```typescript
getFormatterForFieldNoDefault(fieldname: string): FieldFormat | undefined;
getFormatterForFieldNoDefault(fieldname: string, params?: Record<string, any>): FieldFormat | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| fieldname | <code>string</code> | |
| params | <code>Record&lt;string, any&gt;</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export declare class IndexPattern implements IIndexPattern
| [getAsSavedObjectBody()](./kibana-plugin-plugins-data-public.indexpattern.getassavedobjectbody.md) | | Returns index pattern as saved object body for saving |
| [getComputedFields()](./kibana-plugin-plugins-data-public.indexpattern.getcomputedfields.md) | | |
| [getFieldByName(name)](./kibana-plugin-plugins-data-public.indexpattern.getfieldbyname.md) | | |
| [getFormatterForField(field)](./kibana-plugin-plugins-data-public.indexpattern.getformatterforfield.md) | | Provide a field, get its formatter |
| [getFormatterForFieldNoDefault(fieldname)](./kibana-plugin-plugins-data-public.indexpattern.getformatterforfieldnodefault.md) | | Get formatter for a given field name. Return undefined if none exists |
| [getFormatterForField(field, params)](./kibana-plugin-plugins-data-public.indexpattern.getformatterforfield.md) | | Provide a field, get its formatter |
| [getFormatterForFieldNoDefault(fieldname, params)](./kibana-plugin-plugins-data-public.indexpattern.getformatterforfieldnodefault.md) | | Get formatter for a given field name. Return undefined if none exists |
| [getNonScriptedFields()](./kibana-plugin-plugins-data-public.indexpattern.getnonscriptedfields.md) | | |
| [getScriptedFields()](./kibana-plugin-plugins-data-public.indexpattern.getscriptedfields.md) | | |
| [getSourceFiltering()](./kibana-plugin-plugins-data-public.indexpattern.getsourcefiltering.md) | | Get the source filtering configuration for that index. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Provide a field, get its formatter
<b>Signature:</b>

```typescript
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType): FieldFormat;
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType, params?: Record<string, any>): FieldFormat;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| field | <code>IndexPatternField &#124; IndexPatternField['spec'] &#124; IFieldType</code> | |
| params | <code>Record&lt;string, any&gt;</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Get formatter for a given field name. Return undefined if none exists
<b>Signature:</b>

```typescript
getFormatterForFieldNoDefault(fieldname: string): FieldFormat | undefined;
getFormatterForFieldNoDefault(fieldname: string, params?: Record<string, any>): FieldFormat | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| fieldname | <code>string</code> | |
| params | <code>Record&lt;string, any&gt;</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export declare class IndexPattern implements IIndexPattern
| [getAsSavedObjectBody()](./kibana-plugin-plugins-data-server.indexpattern.getassavedobjectbody.md) | | Returns index pattern as saved object body for saving |
| [getComputedFields()](./kibana-plugin-plugins-data-server.indexpattern.getcomputedfields.md) | | |
| [getFieldByName(name)](./kibana-plugin-plugins-data-server.indexpattern.getfieldbyname.md) | | |
| [getFormatterForField(field)](./kibana-plugin-plugins-data-server.indexpattern.getformatterforfield.md) | | Provide a field, get its formatter |
| [getFormatterForFieldNoDefault(fieldname)](./kibana-plugin-plugins-data-server.indexpattern.getformatterforfieldnodefault.md) | | Get formatter for a given field name. Return undefined if none exists |
| [getFormatterForField(field, params)](./kibana-plugin-plugins-data-server.indexpattern.getformatterforfield.md) | | Provide a field, get its formatter |
| [getFormatterForFieldNoDefault(fieldname, params)](./kibana-plugin-plugins-data-server.indexpattern.getformatterforfieldnodefault.md) | | Get formatter for a given field name. Return undefined if none exists |
| [getNonScriptedFields()](./kibana-plugin-plugins-data-server.indexpattern.getnonscriptedfields.md) | | |
| [getScriptedFields()](./kibana-plugin-plugins-data-server.indexpattern.getscriptedfields.md) | | |
| [getSourceFiltering()](./kibana-plugin-plugins-data-server.indexpattern.getsourcefiltering.md) | | Get the source filtering configuration for that index. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,18 @@ export class IndexPattern implements IIndexPattern {
* @param field
*/
getFormatterForField(
field: IndexPatternField | IndexPatternField['spec'] | IFieldType
field: IndexPatternField | IndexPatternField['spec'] | IFieldType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should allow overriding field format params on the index pattern level, that opens up a lot of new possibilities.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we investigate how to override uiSetting on the server ? if you could do something like:

 uiSettings.override('timezone', myStoredTimezone);
 const table = tabify(await searchSource.fetch());

would that solve the problems ?

params: Record<string, any> = {}
): FieldFormat {
const fieldFormat = this.getFormatterForFieldNoDefault(field.name);
const fieldFormat = this.getFormatterForFieldNoDefault(field.name, params);
if (fieldFormat) {
return fieldFormat;
}

return this.fieldFormats.getDefaultInstance(
field.type as KBN_FIELD_TYPES,
field.esTypes as ES_FIELD_TYPES[]
field.esTypes as ES_FIELD_TYPES[],
params
);
}

Expand Down Expand Up @@ -399,10 +401,14 @@ export class IndexPattern implements IIndexPattern {
* Get formatter for a given field name. Return undefined if none exists
* @param field
*/
getFormatterForFieldNoDefault(fieldname: string) {
getFormatterForFieldNoDefault(fieldname: string, params: Record<string, any> = {}) {
const formatSpec = this.fieldFormatMap[fieldname];
if (formatSpec?.id) {
return this.fieldFormats.getInstance(formatSpec.id, formatSpec.params);
const instanceParams = {
...formatSpec.params,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another possibility would be updating index pattern, so fieldFormatMap would contain 'correct' formatSpec (and of course not persisting that change):

// update the timezone for all date fields:
indexPatterns.fields.getByType('date').forEach(field => {
  indexPattern.setFieldFormat(field.name, { ...indexPattern.fieldFormatMap[field.name], timezone: myTimezone });
}

const table = tabify(await searchSource.fetch());

...params,
};
return this.fieldFormats.getInstance(formatSpec.id, instanceParams);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/plugins/data/common/search/tabify/tabify_docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { SearchResponse } from 'elasticsearch';

describe('tabifyDocs', () => {
const fieldFormats = {
getInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getDefaultInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getInstance: jest.fn().mockImplementation((id: string) => ({ toJSON: () => ({ id }) })),
getDefaultInstance: jest.fn().mockImplementation((id: string) => ({ toJSON: () => ({ id }) })),
};

const index = new IndexPattern({
Expand Down Expand Up @@ -63,4 +63,11 @@ describe('tabifyDocs', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot();
});

it('uses custom format params', () => {
tabifyDocs(response, index, {}, { timezone: `US/Alaska` });
expect(fieldFormats.getDefaultInstance).toBeCalledWith('number', undefined, {
timezone: 'US/Alaska',
});
});
});
5 changes: 3 additions & 2 deletions src/plugins/data/common/search/tabify/tabify_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export interface TabifyDocsOptions {
export const tabifyDocs = (
esResponse: SearchResponse<unknown>,
index?: IndexPattern,
params: TabifyDocsOptions = {}
params: TabifyDocsOptions = {},
formatParams: Record<string, any> = {}
): Datatable => {
const columns: DatatableColumn[] = [];

Expand All @@ -79,7 +80,7 @@ export const tabifyDocs = (
const fieldName = field?.name || key;
if (!columns.find((c) => c.id === fieldName)) {
const fieldType = (field?.type as DatatableColumnType) || typeof value;
const formatter = field && index?.getFormatterForField(field);
const formatter = field && index?.getFormatterForField(field, formatParams);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for https://github.com/elastic/kibana/pull/88303/files#diff-a86296d9011252ff17c1007bb3de66763c43af633f7daff1207151da34838e7fR225

But I am interested to know if there is a better way to set the timezone for field formatters in my other PR.

columns.push({
id: fieldName,
name: fieldName,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,8 +1323,8 @@ export class IndexPattern implements IIndexPattern {
};
// (undocumented)
getFieldByName(name: string): IndexPatternField | undefined;
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType): FieldFormat;
getFormatterForFieldNoDefault(fieldname: string): FieldFormat | undefined;
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType, params?: Record<string, any>): FieldFormat;
getFormatterForFieldNoDefault(fieldname: string, params?: Record<string, any>): FieldFormat | undefined;
// (undocumented)
getNonScriptedFields(): IndexPatternField[];
getOriginalSavedObjectBody: () => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,8 @@ export class IndexPattern implements IIndexPattern {
};
// (undocumented)
getFieldByName(name: string): IndexPatternField | undefined;
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType): FieldFormat;
getFormatterForFieldNoDefault(fieldname: string): FieldFormat | undefined;
getFormatterForField(field: IndexPatternField | IndexPatternField['spec'] | IFieldType, params?: Record<string, any>): FieldFormat;
getFormatterForFieldNoDefault(fieldname: string, params?: Record<string, any>): FieldFormat | undefined;
// Warning: (ae-forgotten-export) The symbol "IndexPatternField" needs to be exported by the entry point index.d.ts
//
// (undocumented)
Expand Down