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

Field Formats namespace #56975

Merged
Merged
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
6 changes: 3 additions & 3 deletions src/legacy/core_plugins/data/public/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { Schema } from './schemas';
import {
ISearchSource,
FetchOptions,
fieldFormats,
FieldFormatsContentType,
KBN_FIELD_TYPES,
} from '../../../../../../plugins/data/public';

Expand Down Expand Up @@ -383,7 +383,7 @@ export class AggConfig {
return this.aggConfigs.timeRange;
}

fieldFormatter(contentType?: fieldFormats.ContentType, defaultFormat?: any) {
fieldFormatter(contentType?: FieldFormatsContentType, defaultFormat?: any) {
const format = this.type && this.type.getFormat(this);

if (format) {
Expand All @@ -393,7 +393,7 @@ export class AggConfig {
return this.fieldOwnFormatter(contentType, defaultFormat);
}

fieldOwnFormatter(contentType?: fieldFormats.ContentType, defaultFormat?: any) {
fieldOwnFormatter(contentType?: FieldFormatsContentType, defaultFormat?: any) {
const fieldFormatsService = npStart.plugins.data.fieldFormats;
const field = this.getField();
let format = field && field.format;
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/core_plugins/data/public/search/aggs/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { BaseParamType } from './param_types/base';
import { AggParamType } from './param_types/agg';
import {
KBN_FIELD_TYPES,
fieldFormats,
IFieldFormat,
ISearchSource,
} from '../../../../../../plugins/data/public';

Expand Down Expand Up @@ -58,7 +58,7 @@ export interface AggTypeConfig<
inspectorAdapters: Adapters,
abortSignal?: AbortSignal
) => Promise<any>;
getFormat?: (agg: TAggConfig) => fieldFormats.FieldFormat;
getFormat?: (agg: TAggConfig) => IFieldFormat;
getValue?: (agg: TAggConfig, bucket: any) => any;
getKey?: (bucket: any, key: any, agg: TAggConfig) => any;
}
Expand Down Expand Up @@ -199,7 +199,7 @@ export class AggType<
* @param {agg} agg - the agg to pick a format for
* @return {FieldFormat}
*/
getFormat: (agg: TAggConfig) => fieldFormats.FieldFormat;
getFormat: (agg: TAggConfig) => IFieldFormat;

getValue: (agg: TAggConfig, bucket: any) => any;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import moment from 'moment';
import { createFilterDateRange } from './date_range';
import { fieldFormats } from '../../../../../../../../plugins/data/public';
import { fieldFormats, FieldFormatsGetConfigFn } from '../../../../../../../../plugins/data/public';
import { AggConfigs } from '../../agg_configs';
import { BUCKET_TYPES } from '../bucket_agg_types';
import { IBucketAggConfig } from '../_bucket_agg_type';
Expand All @@ -28,7 +28,7 @@ jest.mock('ui/new_platform');

describe('AggConfig Filters', () => {
describe('Date range', () => {
const getConfig = (() => {}) as fieldFormats.GetConfigFn;
const getConfig = (() => {}) as FieldFormatsGetConfigFn;
const getAggConfigs = () => {
const field = {
name: '@timestamp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import { createFilterHistogram } from './histogram';
import { AggConfigs } from '../../agg_configs';
import { BUCKET_TYPES } from '../bucket_agg_types';
import { IBucketAggConfig } from '../_bucket_agg_type';
import { fieldFormats } from '../../../../../../../../plugins/data/public';
import { fieldFormats, FieldFormatsGetConfigFn } from '../../../../../../../../plugins/data/public';

jest.mock('ui/new_platform');

describe('AggConfig Filters', () => {
describe('histogram', () => {
const getConfig = (() => {}) as fieldFormats.GetConfigFn;
const getConfig = (() => {}) as FieldFormatsGetConfigFn;
const getAggConfigs = () => {
const field = {
name: 'bytes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { createFilterRange } from './range';
import { fieldFormats } from '../../../../../../../../plugins/data/public';
import { fieldFormats, FieldFormatsGetConfigFn } from '../../../../../../../../plugins/data/public';
import { AggConfigs } from '../../agg_configs';
import { BUCKET_TYPES } from '../bucket_agg_types';
import { IBucketAggConfig } from '../_bucket_agg_type';
Expand All @@ -27,7 +27,7 @@ jest.mock('ui/new_platform');

describe('AggConfig Filters', () => {
describe('range', () => {
const getConfig = (() => {}) as fieldFormats.GetConfigFn;
const getConfig = (() => {}) as FieldFormatsGetConfigFn;
const getAggConfigs = () => {
const field = {
name: 'bytes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { AggConfigs } from '../agg_configs';
import { BUCKET_TYPES } from './bucket_agg_types';
import { fieldFormats } from '../../../../../../../plugins/data/public';
import { FieldFormatsGetConfigFn, fieldFormats } from '../../../../../../../plugins/data/public';

jest.mock('ui/new_platform');

Expand All @@ -44,7 +44,7 @@ const buckets = [
];

describe('Range Agg', () => {
const getConfig = (() => {}) as fieldFormats.GetConfigFn;
const getConfig = (() => {}) as FieldFormatsGetConfigFn;
const getAggConfigs = () => {
const field = {
name: 'bytes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { IAggConfigs } from '../agg_configs';
import { Adapters } from '../../../../../../../plugins/inspector/public';
import {
ISearchSource,
fieldFormats,
IFieldFormat,
FieldFormatsContentType,
KBN_FIELD_TYPES,
} from '../../../../../../../plugins/data/public';

Expand Down Expand Up @@ -80,9 +81,9 @@ export const termsBucketAgg = new BucketAggType({
const params = agg.params;
return agg.getFieldDisplayName() + ': ' + params.order.text;
},
getFormat(bucket): fieldFormats.FieldFormat {
getFormat(bucket): IFieldFormat {
return {
getConverterFor: (type: fieldFormats.ContentType) => {
getConverterFor: (type: FieldFormatsContentType) => {
return (val: any) => {
if (val === '__other__') {
return bucket.params.otherBucketLabel;
Expand All @@ -94,7 +95,7 @@ export const termsBucketAgg = new BucketAggType({
return bucket.params.field.format.convert(val, type);
};
},
} as fieldFormats.FieldFormat;
} as IFieldFormat;
},
createFilter: createFilterTerms,
postFlightRequest: async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { isColorDark } from '@elastic/eui';

import { getFormat } from '../legacy_imports';
import { MetricVisValue } from './metric_vis_value';
import { fieldFormats } from '../../../../../plugins/data/public';
import { FieldFormatsContentType, IFieldFormat } from '../../../../../plugins/data/public';
import { Context } from '../metric_vis_fn';
import { KibanaDatatable } from '../../../../../plugins/expressions/public';
import { getHeatmapColors } from '../../../../../plugins/charts/public';
Expand Down Expand Up @@ -100,9 +100,9 @@ export class MetricVisComponent extends Component<MetricVisComponentProps> {
}

private getFormattedValue = (
fieldFormatter: fieldFormats.FieldFormat,
fieldFormatter: IFieldFormat,
value: any,
format: fieldFormats.ContentType = 'text'
format: FieldFormatsContentType = 'text'
) => {
if (isNaN(value)) return '-';
return fieldFormatter.convert(value, format);
Expand All @@ -119,7 +119,7 @@ export class MetricVisComponent extends Component<MetricVisComponentProps> {
const metrics: MetricVisMetric[] = [];

let bucketColumnId: string;
let bucketFormatter: fieldFormats.FieldFormat;
let bucketFormatter: IFieldFormat;

if (dimensions.bucket) {
bucketColumnId = table.columns[dimensions.bucket.accessor].id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import { identity } from 'lodash';
import { IAggConfig } from 'ui/agg_types';
import { npStart } from 'ui/new_platform';
import { SerializedFieldFormat } from 'src/plugins/expressions/public';
import { fieldFormats } from '../../../../../../plugins/data/public';
import {
fieldFormats,
IFieldFormat,
FieldFormatId,
FieldFormatsContentType,
} from '../../../../../../plugins/data/public';
import { Vis } from '../../../../../core_plugins/visualizations/public';

import { tabifyGetColumns } from '../../../agg_response/tabify/_get_columns';
Expand All @@ -45,10 +50,7 @@ const getConfig = (key: string, defaultOverride?: any): any =>
npStart.core.uiSettings.get(key, defaultOverride);
const DefaultFieldFormat = fieldFormats.FieldFormat.from(identity);

const getFieldFormat = (
id?: fieldFormats.IFieldFormatId,
params: object = {}
): fieldFormats.FieldFormat => {
const getFieldFormat = (id?: FieldFormatId, params: object = {}): IFieldFormat => {
const fieldFormatsService = npStart.plugins.data.fieldFormats;

if (id) {
Expand Down Expand Up @@ -94,7 +96,7 @@ export const createFormat = (agg: IAggConfig): SerializedFieldFormat => {
return formats[agg.type.name] ? formats[agg.type.name]() : format;
};

export type FormatFactory = (mapping?: SerializedFieldFormat) => fieldFormats.FieldFormat;
export type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat;

export const getFormat: FormatFactory = mapping => {
if (!mapping) {
Expand Down Expand Up @@ -133,7 +135,7 @@ export const getFormat: FormatFactory = mapping => {
return new IpRangeFormat();
} else if (isTermsFieldFormat(mapping) && mapping.params) {
const { params } = mapping;
const convert = (val: string, type: fieldFormats.ContentType) => {
const convert = (val: string, type: FieldFormatsContentType) => {
const format = getFieldFormat(params.id, mapping.params);

if (val === '__other__') {
Expand All @@ -148,8 +150,8 @@ export const getFormat: FormatFactory = mapping => {

return {
convert,
getConverterFor: (type: fieldFormats.ContentType) => (val: string) => convert(val, type),
} as fieldFormats.FieldFormat;
getConverterFor: (type: FieldFormatsContentType) => (val: string) => convert(val, type),
} as IFieldFormat;
} else {
return getFieldFormat(id, mapping.params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* under the License.
*/
import { escape, isFunction } from 'lodash';
import { IFieldFormat, HtmlContextTypeConvert } from '../types';
import { IFieldFormat, HtmlContextTypeConvert, FieldFormatsContentType } from '../types';
import { asPrettyString, getHighlightHtml } from '../utils';

export const HTML_CONTEXT_TYPE = 'html';
export const HTML_CONTEXT_TYPE: FieldFormatsContentType = 'html';

const getConvertFn = (
format: IFieldFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/

import { isFunction } from 'lodash';
import { IFieldFormat, TextContextTypeConvert } from '../types';
import { IFieldFormat, TextContextTypeConvert, FieldFormatsContentType } from '../types';
import { asPrettyString } from '../utils';

export const TEXT_CONTEXT_TYPE = 'text';
export const TEXT_CONTEXT_TYPE: FieldFormatsContentType = 'text';

export const setup = (
format: IFieldFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { FieldFormat } from '../field_format';
import {
TextContextTypeConvert,
FIELD_FORMAT_IDS,
GetConfigFn,
FieldFormatsGetConfigFn,
IFieldFormatMetaParams,
} from '../types';

Expand All @@ -40,7 +40,7 @@ export class DateFormat extends FieldFormat {
private memoizedPattern: string = '';
private timeZone: string = '';

constructor(params: IFieldFormatMetaParams, getConfig?: GetConfigFn) {
constructor(params: IFieldFormatMetaParams, getConfig?: FieldFormatsGetConfigFn) {
super(params, getConfig);

this.memoizedConverter = memoize((val: any) => {
Expand Down
23 changes: 9 additions & 14 deletions src/plugins/data/common/field_formats/field_format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@
import { transform, size, cloneDeep, get, defaults } from 'lodash';
import { createCustomFieldFormat } from './converters/custom';
import {
GetConfigFn,
ContentType,
FieldFormatsGetConfigFn,
FieldFormatsContentType,
IFieldFormatType,
FieldFormatConvert,
FieldFormatConvertFunction,
HtmlContextTypeOptions,
TextContextTypeOptions,
IFieldFormatMetaParams,
} from './types';
import {
htmlContentTypeSetup,
textContentTypeSetup,
TEXT_CONTEXT_TYPE,
HTML_CONTEXT_TYPE,
} from './content_types';
import { htmlContentTypeSetup, textContentTypeSetup, TEXT_CONTEXT_TYPE } from './content_types';
import { HtmlContextTypeConvert, TextContextTypeConvert } from './types';

const DEFAULT_CONTEXT_TYPE = TEXT_CONTEXT_TYPE;
Expand Down Expand Up @@ -90,9 +85,9 @@ export abstract class FieldFormat {
public type: any = this.constructor;

protected readonly _params: any;
protected getConfig: GetConfigFn | undefined;
protected getConfig: FieldFormatsGetConfigFn | undefined;

constructor(_params: IFieldFormatMetaParams = {}, getConfig?: GetConfigFn) {
constructor(_params: IFieldFormatMetaParams = {}, getConfig?: FieldFormatsGetConfigFn) {
this._params = _params;

if (getConfig) {
Expand All @@ -112,7 +107,7 @@ export abstract class FieldFormat {
*/
convert(
value: any,
contentType: ContentType = DEFAULT_CONTEXT_TYPE,
contentType: FieldFormatsContentType = DEFAULT_CONTEXT_TYPE,
options?: HtmlContextTypeOptions | TextContextTypeOptions
): string {
const converter = this.getConverterFor(contentType);
Expand All @@ -131,7 +126,7 @@ export abstract class FieldFormat {
* @public
*/
getConverterFor(
contentType: ContentType = DEFAULT_CONTEXT_TYPE
contentType: FieldFormatsContentType = DEFAULT_CONTEXT_TYPE
): FieldFormatConvertFunction | null {
if (!this.convertObject) {
this.convertObject = this.setupContentType();
Expand Down Expand Up @@ -210,8 +205,8 @@ export abstract class FieldFormat {

setupContentType(): FieldFormatConvert {
return {
[TEXT_CONTEXT_TYPE]: textContentTypeSetup(this, this.textConvert),
[HTML_CONTEXT_TYPE]: htmlContentTypeSetup(this, this.htmlConvert),
text: textContentTypeSetup(this, this.textConvert),
html: htmlContentTypeSetup(this, this.htmlConvert),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
import { FieldFormatsRegistry } from './field_formats_registry';
import { BoolFormat, PercentFormat, StringFormat } from './converters';
import { GetConfigFn, IFieldFormatType } from './types';
import { FieldFormatsGetConfigFn, IFieldFormatType } from './types';
import { KBN_FIELD_TYPES } from '../../common';

const getValueOfPrivateField = (instance: any, field: string) => instance[field];

describe('FieldFormatsRegistry', () => {
let fieldFormatsRegistry: FieldFormatsRegistry;
let defaultMap = {};
const getConfig = (() => defaultMap) as GetConfigFn;
const getConfig = (() => defaultMap) as FieldFormatsGetConfigFn;

beforeEach(() => {
fieldFormatsRegistry = new FieldFormatsRegistry();
Expand Down
Loading