Skip to content

Commit

Permalink
Index pattern edit field formatter API (#78352)
Browse files Browse the repository at this point in the history
* Index pattern edit field formatter API
  • Loading branch information
mattkime authored Oct 20, 2020
1 parent f57518a commit c8e40d5
Show file tree
Hide file tree
Showing 28 changed files with 226 additions and 213 deletions.
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

0 comments on commit c8e40d5

Please sign in to comment.