-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data] Provide
datatable
utility functions (#126572)
* Extract datatable utilities into a separate service * Add getField utility function * Add getFieldFormat utility function * Add field format setter utility functions * Add data view field setter utility functions * Add getInterval utility function
- Loading branch information
Showing
23 changed files
with
363 additions
and
112 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
src/plugins/data/common/datatable_utilities/datatable_utilities_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createStubDataView } from 'src/plugins/data_views/common/mocks'; | ||
import type { DataViewsContract } from 'src/plugins/data_views/common'; | ||
import type { DatatableColumn } from 'src/plugins/expressions/common'; | ||
import { FieldFormat } from 'src/plugins/field_formats/common'; | ||
import { fieldFormatsMock } from 'src/plugins/field_formats/common/mocks'; | ||
import type { AggsCommonStart } from '../search'; | ||
import { DatatableUtilitiesService } from './datatable_utilities_service'; | ||
|
||
describe('DatatableUtilitiesService', () => { | ||
let aggs: jest.Mocked<AggsCommonStart>; | ||
let dataViews: jest.Mocked<DataViewsContract>; | ||
let datatableUtilitiesService: DatatableUtilitiesService; | ||
|
||
beforeEach(() => { | ||
aggs = { | ||
createAggConfigs: jest.fn(), | ||
types: { get: jest.fn() }, | ||
} as unknown as typeof aggs; | ||
dataViews = { | ||
get: jest.fn(), | ||
} as unknown as typeof dataViews; | ||
|
||
datatableUtilitiesService = new DatatableUtilitiesService(aggs, dataViews, fieldFormatsMock); | ||
}); | ||
|
||
describe('clearField', () => { | ||
it('should delete the field reference', () => { | ||
const column = { meta: { field: 'foo' } } as DatatableColumn; | ||
|
||
datatableUtilitiesService.clearField(column); | ||
|
||
expect(column).not.toHaveProperty('meta.field'); | ||
}); | ||
}); | ||
|
||
describe('clearFieldFormat', () => { | ||
it('should remove field format', () => { | ||
const column = { meta: { params: { id: 'number' } } } as DatatableColumn; | ||
datatableUtilitiesService.clearFieldFormat(column); | ||
|
||
expect(column).not.toHaveProperty('meta.params'); | ||
}); | ||
}); | ||
|
||
describe('getDataView', () => { | ||
it('should return a data view instance', async () => { | ||
const column = { meta: { index: 'index' } } as DatatableColumn; | ||
const dataView = {} as ReturnType<DataViewsContract['get']>; | ||
dataViews.get.mockReturnValue(dataView); | ||
|
||
await expect(datatableUtilitiesService.getDataView(column)).resolves.toBe(dataView); | ||
expect(dataViews.get).toHaveBeenCalledWith('index'); | ||
}); | ||
|
||
it('should return undefined when there is no index metadata', async () => { | ||
const column = { meta: {} } as DatatableColumn; | ||
|
||
await expect(datatableUtilitiesService.getDataView(column)).resolves.toBeUndefined(); | ||
expect(dataViews.get).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('getField', () => { | ||
it('should return a data view field instance', async () => { | ||
const column = { meta: { field: 'field', index: 'index' } } as DatatableColumn; | ||
const dataView = createStubDataView({ spec: {} }); | ||
const field = {}; | ||
spyOn(datatableUtilitiesService, 'getDataView').and.returnValue(dataView); | ||
spyOn(dataView, 'getFieldByName').and.returnValue(field); | ||
|
||
await expect(datatableUtilitiesService.getField(column)).resolves.toBe(field); | ||
expect(dataView.getFieldByName).toHaveBeenCalledWith('field'); | ||
}); | ||
|
||
it('should return undefined when there is no field metadata', async () => { | ||
const column = { meta: {} } as DatatableColumn; | ||
|
||
await expect(datatableUtilitiesService.getField(column)).resolves.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('getFieldFormat', () => { | ||
it('should deserialize field format', () => { | ||
const column = { meta: { params: { id: 'number' } } } as DatatableColumn; | ||
const fieldFormat = datatableUtilitiesService.getFieldFormat(column); | ||
|
||
expect(fieldFormat).toBeInstanceOf(FieldFormat); | ||
}); | ||
}); | ||
|
||
describe('getInterval', () => { | ||
it('should return a histogram interval', () => { | ||
const column = { | ||
meta: { sourceParams: { params: { interval: '1d' } } }, | ||
} as unknown as DatatableColumn; | ||
|
||
expect(datatableUtilitiesService.getInterval(column)).toBe('1d'); | ||
}); | ||
}); | ||
|
||
describe('setFieldFormat', () => { | ||
it('should set new field format', () => { | ||
const column = { meta: {} } as DatatableColumn; | ||
const fieldFormat = fieldFormatsMock.deserialize({ id: 'number' }); | ||
datatableUtilitiesService.setFieldFormat(column, fieldFormat); | ||
|
||
expect(column.meta.params).toEqual( | ||
expect.objectContaining({ | ||
id: expect.anything(), | ||
params: undefined, | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
93 changes: 93 additions & 0 deletions
93
src/plugins/data/common/datatable_utilities/datatable_utilities_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { DataView, DataViewsContract, DataViewField } from 'src/plugins/data_views/common'; | ||
import type { DatatableColumn } from 'src/plugins/expressions/common'; | ||
import type { FieldFormatsStartCommon, FieldFormat } from 'src/plugins/field_formats/common'; | ||
import type { AggsCommonStart, AggConfig, CreateAggConfigParams, IAggType } from '../search'; | ||
|
||
export class DatatableUtilitiesService { | ||
constructor( | ||
private aggs: AggsCommonStart, | ||
private dataViews: DataViewsContract, | ||
private fieldFormats: FieldFormatsStartCommon | ||
) { | ||
this.getAggConfig = this.getAggConfig.bind(this); | ||
this.getDataView = this.getDataView.bind(this); | ||
this.getField = this.getField.bind(this); | ||
this.isFilterable = this.isFilterable.bind(this); | ||
} | ||
|
||
clearField(column: DatatableColumn): void { | ||
delete column.meta.field; | ||
} | ||
|
||
clearFieldFormat(column: DatatableColumn): void { | ||
delete column.meta.params; | ||
} | ||
|
||
async getAggConfig(column: DatatableColumn): Promise<AggConfig | undefined> { | ||
const dataView = await this.getDataView(column); | ||
|
||
if (!dataView) { | ||
return; | ||
} | ||
|
||
const { aggs } = await this.aggs.createAggConfigs( | ||
dataView, | ||
column.meta.sourceParams && [column.meta.sourceParams as CreateAggConfigParams] | ||
); | ||
|
||
return aggs[0]; | ||
} | ||
|
||
async getDataView(column: DatatableColumn): Promise<DataView | undefined> { | ||
if (!column.meta.index) { | ||
return; | ||
} | ||
|
||
return this.dataViews.get(column.meta.index); | ||
} | ||
|
||
async getField(column: DatatableColumn): Promise<DataViewField | undefined> { | ||
if (!column.meta.field) { | ||
return; | ||
} | ||
|
||
const dataView = await this.getDataView(column); | ||
if (!dataView) { | ||
return; | ||
} | ||
|
||
return dataView.getFieldByName(column.meta.field); | ||
} | ||
|
||
getFieldFormat(column: DatatableColumn): FieldFormat | undefined { | ||
return this.fieldFormats.deserialize(column.meta.params); | ||
} | ||
|
||
getInterval(column: DatatableColumn): string | undefined { | ||
const params = column.meta.sourceParams?.params as { interval: string } | undefined; | ||
|
||
return params?.interval; | ||
} | ||
|
||
isFilterable(column: DatatableColumn): boolean { | ||
if (column.meta.source !== 'esaggs') { | ||
return false; | ||
} | ||
|
||
const aggType = this.aggs.types.get(column.meta.sourceParams?.type as string) as IAggType; | ||
|
||
return Boolean(aggType.createFilter); | ||
} | ||
|
||
setFieldFormat(column: DatatableColumn, fieldFormat: FieldFormat): void { | ||
column.meta.params = fieldFormat.toJSON(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export * from './datatable_utilities_service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { DatatableUtilitiesService } from './datatable_utilities_service'; | ||
|
||
export function createDatatableUtilitiesMock(): jest.Mocked<DatatableUtilitiesService> { | ||
return { | ||
clearField: jest.fn(), | ||
clearFieldFormat: jest.fn(), | ||
getAggConfig: jest.fn(), | ||
getDataView: jest.fn(), | ||
getField: jest.fn(), | ||
getFieldFormat: jest.fn(), | ||
isFilterable: jest.fn(), | ||
setFieldFormat: jest.fn(), | ||
} as unknown as jest.Mocked<DatatableUtilitiesService>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
*/ | ||
|
||
export * from '../../data_views/common/fields/fields.mocks'; | ||
export * from './datatable_utilities/mock'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
src/plugins/data/common/search/aggs/utils/datatable_column_meta.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.