Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index pattern edit field formatter API #78352

Merged
merged 25 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b2c9fec
partial progress
mattkime Sep 23, 2020
8086b3f
Merge branch 'master' into index_pattern_edit_field_formatter
mattkime Sep 23, 2020
d254479
indexPattern instantiates fieldFormats only when requested
mattkime Sep 23, 2020
9833209
fix formatter lookup
mattkime Sep 23, 2020
e249c37
fix tests and docs
mattkime Sep 24, 2020
537b89b
Merge branch 'master' into index_pattern_edit_field_formatter
mattkime Sep 25, 2020
11c7967
partial progress
mattkime Sep 25, 2020
5d8aa6f
asdf
mattkime Sep 25, 2020
75aecc8
only saving to fieldFormatMap
mattkime Sep 25, 2020
ab832f4
Merge branch 'master' into index_pattern_edit_field_formatter
mattkime Oct 6, 2020
959a138
fix unit and functional tests
mattkime Oct 7, 2020
d1e1160
update docs
mattkime Oct 7, 2020
efdbc60
simplify getformatterforfieldnodefault
mattkime Oct 7, 2020
3fe61ac
Merge branch 'master' into index_pattern_edit_field_formatter
mattkime Oct 7, 2020
646b52a
only use formatters on index pattern, none on field
mattkime Oct 7, 2020
8013450
move field_mapping util to saved_objects plugin
mattkime Oct 7, 2020
604afd3
update docs
mattkime Oct 7, 2020
7041ddb
fix static dependencies
mattkime Oct 7, 2020
cc16bfa
add test
mattkime Oct 7, 2020
e5cf562
Merge branch 'move_field_mappings_util_to_saved_objects' into index_p…
mattkime Oct 7, 2020
361cc64
Merge branch 'master' into index_pattern_edit_field_formatter
kibanamachine Oct 8, 2020
c5de4b5
Merge branch 'master' into index_pattern_edit_field_formatter
mattkime Oct 11, 2020
f71d35a
Merge branch 'index_pattern_edit_field_formatter' of github.com:mattk…
mattkime Oct 11, 2020
c483706
Merge branch 'master' into index_pattern_edit_field_formatter
kibanamachine Oct 13, 2020
364fd2f
Merge branch 'master' into index_pattern_edit_field_formatter
kibanamachine Oct 19, 2020
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
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IndexPattern](./kibana-plugin-plugins-data-public.indexpattern.md) &gt; [deleteFieldFormat](./kibana-plugin-plugins-data-public.indexpattern.deletefieldformat.md)

## IndexPattern.deleteFieldFormat property

<b>Signature:</b>

```typescript
deleteFieldFormat: (fieldName: string) => void;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IndexPattern](./kibana-plugin-plugins-data-public.indexpattern.md) &gt; [getFormatterForFieldNoDefault](./kibana-plugin-plugins-data-public.indexpattern.getformatterforfieldnodefault.md)

## IndexPattern.getFormatterForFieldNoDefault() method

Get formatter for a given field name. Return undefined if none exists

<b>Signature:</b>

```typescript
getFormatterForFieldNoDefault(fieldname: string): FieldFormat | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| fieldname | <code>string</code> | |

<b>Returns:</b>

`FieldFormat | undefined`

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare class IndexPattern implements IIndexPattern

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [deleteFieldFormat](./kibana-plugin-plugins-data-public.indexpattern.deletefieldformat.md) | | <code>(fieldName: string) =&gt; void</code> | |
| [fieldFormatMap](./kibana-plugin-plugins-data-public.indexpattern.fieldformatmap.md) | | <code>Record&lt;string, any&gt;</code> | |
| [fields](./kibana-plugin-plugins-data-public.indexpattern.fields.md) | | <code>IIndexPatternFieldList &amp; {</code><br/><code> toSpec: () =&gt; IndexPatternFieldMap;</code><br/><code> }</code> | |
| [flattenHit](./kibana-plugin-plugins-data-public.indexpattern.flattenhit.md) | | <code>(hit: Record&lt;string, any&gt;, deep?: boolean) =&gt; Record&lt;string, any&gt;</code> | |
Expand All @@ -30,6 +31,7 @@ export declare class IndexPattern implements IIndexPattern
| [intervalName](./kibana-plugin-plugins-data-public.indexpattern.intervalname.md) | | <code>string &#124; undefined</code> | |
| [metaFields](./kibana-plugin-plugins-data-public.indexpattern.metafields.md) | | <code>string[]</code> | |
| [resetOriginalSavedObjectBody](./kibana-plugin-plugins-data-public.indexpattern.resetoriginalsavedobjectbody.md) | | <code>() =&gt; void</code> | Reset last saved saved object fields. used after saving |
| [setFieldFormat](./kibana-plugin-plugins-data-public.indexpattern.setfieldformat.md) | | <code>(fieldName: string, format: SerializedFieldFormat) =&gt; void</code> | |
| [sourceFilters](./kibana-plugin-plugins-data-public.indexpattern.sourcefilters.md) | | <code>SourceFilter[]</code> | |
| [timeFieldName](./kibana-plugin-plugins-data-public.indexpattern.timefieldname.md) | | <code>string &#124; undefined</code> | |
| [title](./kibana-plugin-plugins-data-public.indexpattern.title.md) | | <code>string</code> | |
Expand All @@ -47,6 +49,7 @@ export declare class IndexPattern implements IIndexPattern
| [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 |
| [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
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IndexPattern](./kibana-plugin-plugins-data-public.indexpattern.md) &gt; [setFieldFormat](./kibana-plugin-plugins-data-public.indexpattern.setfieldformat.md)

## IndexPattern.setFieldFormat property

<b>Signature:</b>

```typescript
setFieldFormat: (fieldName: string, format: SerializedFieldFormat) => void;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IndexPatternSpec](./kibana-plugin-plugins-data-public.indexpatternspec.md) &gt; [fieldFormats](./kibana-plugin-plugins-data-public.indexpatternspec.fieldformats.md)

## IndexPatternSpec.fieldFormats property

<b>Signature:</b>

```typescript
fieldFormats?: Record<string, SerializedFieldFormat>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IndexPatternSpec

| Property | Type | Description |
| --- | --- | --- |
| [fieldFormats](./kibana-plugin-plugins-data-public.indexpatternspec.fieldformats.md) | <code>Record&lt;string, SerializedFieldFormat&gt;</code> | |
| [fields](./kibana-plugin-plugins-data-public.indexpatternspec.fields.md) | <code>IndexPatternFieldMap</code> | |
| [id](./kibana-plugin-plugins-data-public.indexpatternspec.id.md) | <code>string</code> | |
| [intervalName](./kibana-plugin-plugins-data-public.indexpatternspec.intervalname.md) | <code>string</code> | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [IndexPattern](./kibana-plugin-plugins-data-server.indexpattern.md) &gt; [deleteFieldFormat](./kibana-plugin-plugins-data-server.indexpattern.deletefieldformat.md)

## IndexPattern.deleteFieldFormat property

<b>Signature:</b>

```typescript
deleteFieldFormat: (fieldName: string) => void;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [IndexPattern](./kibana-plugin-plugins-data-server.indexpattern.md) &gt; [getFormatterForFieldNoDefault](./kibana-plugin-plugins-data-server.indexpattern.getformatterforfieldnodefault.md)

## IndexPattern.getFormatterForFieldNoDefault() method

Get formatter for a given field name. Return undefined if none exists

<b>Signature:</b>

```typescript
getFormatterForFieldNoDefault(fieldname: string): FieldFormat | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| fieldname | <code>string</code> | |

<b>Returns:</b>

`FieldFormat | undefined`

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare class IndexPattern implements IIndexPattern

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [deleteFieldFormat](./kibana-plugin-plugins-data-server.indexpattern.deletefieldformat.md) | | <code>(fieldName: string) =&gt; void</code> | |
| [fieldFormatMap](./kibana-plugin-plugins-data-server.indexpattern.fieldformatmap.md) | | <code>Record&lt;string, any&gt;</code> | |
| [fields](./kibana-plugin-plugins-data-server.indexpattern.fields.md) | | <code>IIndexPatternFieldList &amp; {</code><br/><code> toSpec: () =&gt; IndexPatternFieldMap;</code><br/><code> }</code> | |
| [flattenHit](./kibana-plugin-plugins-data-server.indexpattern.flattenhit.md) | | <code>(hit: Record&lt;string, any&gt;, deep?: boolean) =&gt; Record&lt;string, any&gt;</code> | |
Expand All @@ -30,6 +31,7 @@ export declare class IndexPattern implements IIndexPattern
| [intervalName](./kibana-plugin-plugins-data-server.indexpattern.intervalname.md) | | <code>string &#124; undefined</code> | |
| [metaFields](./kibana-plugin-plugins-data-server.indexpattern.metafields.md) | | <code>string[]</code> | |
| [resetOriginalSavedObjectBody](./kibana-plugin-plugins-data-server.indexpattern.resetoriginalsavedobjectbody.md) | | <code>() =&gt; void</code> | Reset last saved saved object fields. used after saving |
| [setFieldFormat](./kibana-plugin-plugins-data-server.indexpattern.setfieldformat.md) | | <code>(fieldName: string, format: SerializedFieldFormat) =&gt; void</code> | |
| [sourceFilters](./kibana-plugin-plugins-data-server.indexpattern.sourcefilters.md) | | <code>SourceFilter[]</code> | |
| [timeFieldName](./kibana-plugin-plugins-data-server.indexpattern.timefieldname.md) | | <code>string &#124; undefined</code> | |
| [title](./kibana-plugin-plugins-data-server.indexpattern.title.md) | | <code>string</code> | |
Expand All @@ -47,6 +49,7 @@ export declare class IndexPattern implements IIndexPattern
| [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 |
| [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
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [IndexPattern](./kibana-plugin-plugins-data-server.indexpattern.md) &gt; [setFieldFormat](./kibana-plugin-plugins-data-server.indexpattern.setfieldformat.md)

## IndexPattern.setFieldFormat property

<b>Signature:</b>

```typescript
setFieldFormat: (fieldName: string, format: SerializedFieldFormat) => void;
```

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ describe('IndexPattern', () => {
});
});

describe('setFieldFormat and deleteFieldFormaat', () => {
test('should persist changes', () => {
const formatter = {
toJSON: () => ({ id: 'bytes' }),
} as FieldFormat;
indexPattern.getFormatterForField = () => formatter;
indexPattern.setFieldFormat('bytes', { id: 'bytes' });
expect(indexPattern.toSpec().fieldFormats).toEqual({ bytes: { id: 'bytes' } });

indexPattern.deleteFieldFormat('bytes');
expect(indexPattern.toSpec().fieldFormats).toEqual({});
});
});

describe('toSpec', () => {
test('should match snapshot', () => {
const formatter = {
Expand All @@ -209,7 +223,6 @@ describe('IndexPattern', () => {
expect(restoredPattern.title).toEqual(indexPattern.title);
expect(restoredPattern.timeFieldName).toEqual(indexPattern.timeFieldName);
expect(restoredPattern.fields.length).toEqual(indexPattern.fields.length);
expect(restoredPattern.fieldFormatMap.bytes instanceof MockFieldFormatter).toEqual(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ import _, { each, reject } from 'lodash';
import { SavedObjectsClientCommon } from '../..';
import { DuplicateField } from '../../../../kibana_utils/common';

import {
ES_FIELD_TYPES,
KBN_FIELD_TYPES,
IIndexPattern,
FieldFormatNotFoundError,
IFieldType,
} from '../../../common';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES, IIndexPattern, IFieldType } from '../../../common';
import { IndexPatternField, IIndexPatternFieldList, fieldList } from '../fields';
import { formatHitProvider } from './format_hit';
import { flattenHitWrapper } from './flatten_hit';
Expand Down Expand Up @@ -102,7 +96,7 @@ export class IndexPattern implements IIndexPattern {

// set values
this.id = spec.id;
const fieldFormatMap = this.fieldSpecsToFieldFormatMap(spec.fields);
this.fieldFormatMap = spec.fieldFormats || {};

this.version = spec.version;

Expand All @@ -113,12 +107,16 @@ export class IndexPattern implements IIndexPattern {
this.fields.replaceAll(Object.values(spec.fields || {}));
this.type = spec.type;
this.typeMeta = spec.typeMeta;

this.fieldFormatMap = _.mapValues(fieldFormatMap, (mapping) => {
return this.deserializeFieldFormatMap(mapping);
});
}

setFieldFormat = (fieldName: string, format: SerializedFieldFormat) => {
this.fieldFormatMap[fieldName] = format;
};

deleteFieldFormat = (fieldName: string) => {
delete this.fieldFormatMap[fieldName];
};

/**
* Get last saved saved object fields
*/
Expand All @@ -131,34 +129,6 @@ export class IndexPattern implements IIndexPattern {
this.originalSavedObjectBody = this.getAsSavedObjectBody();
};

/**
* Converts field format spec to field format instance
* @param mapping
*/
private deserializeFieldFormatMap(mapping: SerializedFieldFormat<Record<string, any>>) {
try {
return this.fieldFormats.getInstance(mapping.id as string, mapping.params);
} catch (err) {
if (err instanceof FieldFormatNotFoundError) {
return undefined;
} else {
throw err;
}
}
}

/**
* Extracts FieldFormatMap from FieldSpec map
* @param fldList FieldSpec map
*/
private fieldSpecsToFieldFormatMap = (fldList: IndexPatternSpec['fields'] = {}) =>
Object.values(fldList).reduce<Record<string, SerializedFieldFormat>>((col, fieldSpec) => {
if (fieldSpec.format) {
col[fieldSpec.name] = { ...fieldSpec.format };
}
return col;
}, {});

getComputedFields() {
const scriptFields: any = {};
if (!this.fields) {
Expand Down Expand Up @@ -211,6 +181,7 @@ export class IndexPattern implements IIndexPattern {
fields: this.fields.toSpec({ getFormatterForField: this.getFormatterForField.bind(this) }),
typeMeta: this.typeMeta,
type: this.type,
fieldFormats: this.fieldFormatMap,
};
}

Expand Down Expand Up @@ -299,17 +270,9 @@ export class IndexPattern implements IIndexPattern {
* Returns index pattern as saved object body for saving
*/
getAsSavedObjectBody() {
const serializeFieldFormatMap = (
flat: any,
format: FieldFormat | undefined,
field: string | undefined
) => {
if (format && field) {
flat[field] = format;
}
};
const serialized = _.transform(this.fieldFormatMap, serializeFieldFormatMap);
const fieldFormatMap = _.isEmpty(serialized) ? undefined : JSON.stringify(serialized);
const fieldFormatMap = _.isEmpty(this.fieldFormatMap)
? undefined
: JSON.stringify(this.fieldFormatMap);

return {
title: this.title,
Expand All @@ -330,12 +293,25 @@ export class IndexPattern implements IIndexPattern {
getFormatterForField(
field: IndexPatternField | IndexPatternField['spec'] | IFieldType
): FieldFormat {
return (
this.fieldFormatMap[field.name] ||
this.fieldFormats.getDefaultInstance(
const formatSpec = this.fieldFormatMap[field.name];
if (formatSpec) {
return this.fieldFormats.getInstance(formatSpec.id, formatSpec.params);
} else {
return this.fieldFormats.getDefaultInstance(
field.type as KBN_FIELD_TYPES,
field.esTypes as ES_FIELD_TYPES[]
)
);
);
}
}

/**
* Get formatter for a given field name. Return undefined if none exists
* @param field
*/
getFormatterForFieldNoDefault(fieldname: string) {
const formatSpec = this.fieldFormatMap[fieldname];
if (formatSpec) {
return this.fieldFormats.getInstance(formatSpec.id, formatSpec.params);
}
}
}
Loading