From ce813f012d1f69c9e2a3f8c65e0921c29d421960 Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Fri, 26 Mar 2021 14:29:14 -0500 Subject: [PATCH] [ML] Update to use some shared common functions between ml and transform --- x-pack/plugins/ml/common/index.ts | 1 + .../ml/common/util/object_utils.test.ts | 18 +++++++++ .../common/util/runtime_field_utils.test.ts | 2 +- .../ml/common/util/runtime_field_utils.ts | 7 +--- .../transform/common/api_schemas/common.ts | 31 ++++++---------- .../common/api_schemas/type_guards.ts | 3 +- .../transform/common/shared_imports.ts | 4 ++ .../transform/common/types/index_pattern.ts | 3 +- .../transform/common/types/transform.ts | 2 +- .../transform/common/types/transform_stats.ts | 2 +- .../plugins/transform/common/utils/errors.ts | 2 +- .../common/utils/object_utils.test.ts | 10 +---- .../transform/common/utils/object_utils.ts | 4 -- .../transform/public/app/common/pivot_aggs.ts | 2 +- .../public/app/common/pivot_group_by.ts | 2 +- .../public/app/common/request.test.ts | 5 +-- .../transform/public/app/common/request.ts | 2 +- .../public/app/hooks/use_index_data.test.tsx | 4 +- .../lib/authorization/components/common.ts | 2 +- .../advanced_runtime_mappings_editor.tsx | 2 +- .../step_create/step_create_form.tsx | 2 +- .../step_define/common/common.test.ts | 4 +- .../components/filter_agg_form.test.tsx | 8 ++-- .../filter_agg/components/filter_agg_form.tsx | 6 ++- .../common/get_pivot_dropdown_options.ts | 2 +- .../components/step_define/common/types.ts | 37 +------------------ .../components/wizard/wizard.tsx | 2 +- .../server/routes/api/field_histograms.ts | 1 - .../server/routes/api/transforms_nodes.ts | 3 +- 29 files changed, 66 insertions(+), 107 deletions(-) create mode 100644 x-pack/plugins/ml/common/util/object_utils.test.ts diff --git a/x-pack/plugins/ml/common/index.ts b/x-pack/plugins/ml/common/index.ts index bea7f950b52189..ab98d6513c4f62 100644 --- a/x-pack/plugins/ml/common/index.ts +++ b/x-pack/plugins/ml/common/index.ts @@ -11,5 +11,6 @@ export { ANOMALY_SEVERITY, ANOMALY_THRESHOLD, SEVERITY_COLORS } from './constant export { getSeverityColor, getSeverityType } from './util/anomaly_utils'; export { composeValidators, patternValidator } from './util/validators'; export { isRuntimeMappings, isRuntimeField } from './util/runtime_field_utils'; +export { isPopulatedObject } from './util/object_utils'; export { extractErrorMessage } from './util/errors'; export type { RuntimeMappings } from './types/fields'; diff --git a/x-pack/plugins/ml/common/util/object_utils.test.ts b/x-pack/plugins/ml/common/util/object_utils.test.ts new file mode 100644 index 00000000000000..27e5986d5f528f --- /dev/null +++ b/x-pack/plugins/ml/common/util/object_utils.test.ts @@ -0,0 +1,18 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { isPopulatedObject } from './object_utils'; + +describe('object_utils', () => { + test('isPopulatedObject()', () => { + expect(isPopulatedObject(0)).toBe(false); + expect(isPopulatedObject('')).toBe(false); + expect(isPopulatedObject(null)).toBe(false); + expect(isPopulatedObject({})).toBe(false); + expect(isPopulatedObject({ attribute: 'value' })).toBe(true); + }); +}); diff --git a/x-pack/plugins/ml/common/util/runtime_field_utils.test.ts b/x-pack/plugins/ml/common/util/runtime_field_utils.test.ts index 50765b4a8ef568..330a99f9ee2aa4 100644 --- a/x-pack/plugins/ml/common/util/runtime_field_utils.test.ts +++ b/x-pack/plugins/ml/common/util/runtime_field_utils.test.ts @@ -7,7 +7,7 @@ import { isRuntimeField, isRuntimeMappings } from './runtime_field_utils'; -describe('Transform: step_define type guards', () => { +describe('runtime_field_utiles', () => { it('isRuntimeField()', () => { expect(isRuntimeField(1)).toBe(false); expect(isRuntimeField(null)).toBe(false); diff --git a/x-pack/plugins/ml/common/util/runtime_field_utils.ts b/x-pack/plugins/ml/common/util/runtime_field_utils.ts index 4e55a36a53f0d8..70bedc16b02e4c 100644 --- a/x-pack/plugins/ml/common/util/runtime_field_utils.ts +++ b/x-pack/plugins/ml/common/util/runtime_field_utils.ts @@ -7,10 +7,7 @@ import { estypes } from '@elastic/elasticsearch'; import { isPopulatedObject } from './object_utils'; -import { - RUNTIME_FIELD_TYPES, - RuntimeType, -} from '../../../../../src/plugins/data/common/index_patterns'; +import { RUNTIME_FIELD_TYPES } from '../../../../../src/plugins/data/common'; import type { RuntimeMappings } from '../types/fields'; export function isRuntimeField(arg: unknown): arg is estypes.RuntimeField { @@ -25,7 +22,7 @@ export function isRuntimeField(arg: unknown): arg is estypes.RuntimeField { Object.keys(arg.script).length === 1 && arg.script.hasOwnProperty('source') && typeof arg.script.source === 'string')))) && - RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType) + RUNTIME_FIELD_TYPES.includes(arg.type) ); } diff --git a/x-pack/plugins/transform/common/api_schemas/common.ts b/x-pack/plugins/transform/common/api_schemas/common.ts index 3651af69359a98..ee856d84da1169 100644 --- a/x-pack/plugins/transform/common/api_schemas/common.ts +++ b/x-pack/plugins/transform/common/api_schemas/common.ts @@ -8,6 +8,7 @@ import { schema, TypeOf } from '@kbn/config-schema'; import { TRANSFORM_STATE } from '../constants'; +import { isRuntimeField } from '../shared_imports'; export const transformIdsSchema = schema.arrayOf( schema.object({ @@ -55,25 +56,15 @@ export interface CommonResponseStatusSchema { } export const runtimeMappingsSchema = schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - type: schema.oneOf([ - schema.literal('keyword'), - schema.literal('long'), - schema.literal('double'), - schema.literal('date'), - schema.literal('ip'), - schema.literal('boolean'), - ]), - script: schema.maybe( - schema.oneOf([ - schema.string(), - schema.object({ - source: schema.string(), - }), - ]) - ), - }) + schema.object( + {}, + { + unknowns: 'allow', + validate: (v: object) => { + if (Object.values(v).some((o) => !isRuntimeField(o))) { + return 'Invalid runtime field'; + } + }, + } ) ); diff --git a/x-pack/plugins/transform/common/api_schemas/type_guards.ts b/x-pack/plugins/transform/common/api_schemas/type_guards.ts index 476e2bad853c95..a794cea5a6a820 100644 --- a/x-pack/plugins/transform/common/api_schemas/type_guards.ts +++ b/x-pack/plugins/transform/common/api_schemas/type_guards.ts @@ -6,9 +6,8 @@ */ import type { SearchResponse7 } from '../../../ml/common'; - import type { EsIndex } from '../types/es_index'; -import { isPopulatedObject } from '../utils/object_utils'; +import { isPopulatedObject } from '../../common/shared_imports'; // To be able to use the type guards on the client side, we need to make sure we don't import // the code of '@kbn/config-schema' but just its types, otherwise the client side code will diff --git a/x-pack/plugins/transform/common/shared_imports.ts b/x-pack/plugins/transform/common/shared_imports.ts index 3062c7ab8d23cb..a12811882412ba 100644 --- a/x-pack/plugins/transform/common/shared_imports.ts +++ b/x-pack/plugins/transform/common/shared_imports.ts @@ -11,4 +11,8 @@ export { patternValidator, ChartData, HITS_TOTAL_RELATION, + isPopulatedObject, + isRuntimeMappings, + isRuntimeField, + RuntimeMappings, } from '../../ml/common'; diff --git a/x-pack/plugins/transform/common/types/index_pattern.ts b/x-pack/plugins/transform/common/types/index_pattern.ts index bab31b67b2b61d..ee75ac9dca005c 100644 --- a/x-pack/plugins/transform/common/types/index_pattern.ts +++ b/x-pack/plugins/transform/common/types/index_pattern.ts @@ -6,8 +6,7 @@ */ import type { IndexPattern } from '../../../../../src/plugins/data/common'; - -import { isPopulatedObject } from '../utils/object_utils'; +import { isPopulatedObject } from '../shared_imports'; // Custom minimal type guard for IndexPattern to check against the attributes used in transforms code. export function isIndexPattern(arg: any): arg is IndexPattern { diff --git a/x-pack/plugins/transform/common/types/transform.ts b/x-pack/plugins/transform/common/types/transform.ts index 600808c475fd10..b987f099879d27 100644 --- a/x-pack/plugins/transform/common/types/transform.ts +++ b/x-pack/plugins/transform/common/types/transform.ts @@ -7,9 +7,9 @@ import { EuiComboBoxOptionOption } from '@elastic/eui/src/components/combo_box/types'; import type { LatestFunctionConfig, PutTransformsRequestSchema } from '../api_schemas/transforms'; -import { isPopulatedObject } from '../utils/object_utils'; import { PivotGroupByDict } from './pivot_group_by'; import { PivotAggDict } from './pivot_aggs'; +import { isPopulatedObject } from '../shared_imports'; export type IndexName = string; export type IndexPattern = string; diff --git a/x-pack/plugins/transform/common/types/transform_stats.ts b/x-pack/plugins/transform/common/types/transform_stats.ts index 03e6b2e403b69c..cefa5392de7aac 100644 --- a/x-pack/plugins/transform/common/types/transform_stats.ts +++ b/x-pack/plugins/transform/common/types/transform_stats.ts @@ -6,8 +6,8 @@ */ import { TransformState, TRANSFORM_STATE } from '../constants'; -import { isPopulatedObject } from '../utils/object_utils'; import { TransformId } from './transform'; +import { isPopulatedObject } from '../shared_imports'; export interface TransformStats { id: TransformId; diff --git a/x-pack/plugins/transform/common/utils/errors.ts b/x-pack/plugins/transform/common/utils/errors.ts index 46ff3f9165c00b..11fa8c236a6577 100644 --- a/x-pack/plugins/transform/common/utils/errors.ts +++ b/x-pack/plugins/transform/common/utils/errors.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { isPopulatedObject } from './object_utils'; +import { isPopulatedObject } from '../shared_imports'; export interface ErrorResponse { body: { diff --git a/x-pack/plugins/transform/common/utils/object_utils.test.ts b/x-pack/plugins/transform/common/utils/object_utils.test.ts index 5b354b9b275894..c99adf6b6d1894 100644 --- a/x-pack/plugins/transform/common/utils/object_utils.test.ts +++ b/x-pack/plugins/transform/common/utils/object_utils.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { getNestedProperty, isPopulatedObject } from './object_utils'; +import { getNestedProperty } from './object_utils'; describe('object_utils', () => { test('getNestedProperty()', () => { @@ -68,12 +68,4 @@ describe('object_utils', () => { expect(typeof test11).toBe('number'); expect(test11).toBe(0); }); - - test('isPopulatedObject()', () => { - expect(isPopulatedObject(0)).toBe(false); - expect(isPopulatedObject('')).toBe(false); - expect(isPopulatedObject(null)).toBe(false); - expect(isPopulatedObject({})).toBe(false); - expect(isPopulatedObject({ attribute: 'value' })).toBe(true); - }); }); diff --git a/x-pack/plugins/transform/common/utils/object_utils.ts b/x-pack/plugins/transform/common/utils/object_utils.ts index a573535da6b4fb..605af489143607 100644 --- a/x-pack/plugins/transform/common/utils/object_utils.ts +++ b/x-pack/plugins/transform/common/utils/object_utils.ts @@ -51,7 +51,3 @@ export const setNestedProperty = (obj: Record, accessor: string, va return obj; }; - -export const isPopulatedObject = >(arg: unknown): arg is T => { - return typeof arg === 'object' && arg !== null && Object.keys(arg).length > 0; -}; diff --git a/x-pack/plugins/transform/public/app/common/pivot_aggs.ts b/x-pack/plugins/transform/public/app/common/pivot_aggs.ts index 905b40f16f7fbe..b306c82e25cff2 100644 --- a/x-pack/plugins/transform/public/app/common/pivot_aggs.ts +++ b/x-pack/plugins/transform/public/app/common/pivot_aggs.ts @@ -14,10 +14,10 @@ import type { Dictionary } from '../../../common/types/common'; import type { EsFieldName } from '../../../common/types/fields'; import type { PivotAgg, PivotSupportedAggs } from '../../../common/types/pivot_aggs'; import { PIVOT_SUPPORTED_AGGS } from '../../../common/types/pivot_aggs'; -import { isPopulatedObject } from '../../../common/utils/object_utils'; import { getAggFormConfig } from '../sections/create_transform/components/step_define/common/get_agg_form_config'; import { PivotAggsConfigFilter } from '../sections/create_transform/components/step_define/common/filter_agg/types'; +import { isPopulatedObject } from '../../../common/shared_imports'; export function isPivotSupportedAggs(arg: unknown): arg is PivotSupportedAggs { return ( diff --git a/x-pack/plugins/transform/public/app/common/pivot_group_by.ts b/x-pack/plugins/transform/public/app/common/pivot_group_by.ts index fac0d88a84df74..ae330b3f2f1c86 100644 --- a/x-pack/plugins/transform/public/app/common/pivot_group_by.ts +++ b/x-pack/plugins/transform/public/app/common/pivot_group_by.ts @@ -9,9 +9,9 @@ import { AggName } from '../../../common/types/aggregations'; import { Dictionary } from '../../../common/types/common'; import { EsFieldName } from '../../../common/types/fields'; import { GenericAgg } from '../../../common/types/pivot_group_by'; -import { isPopulatedObject } from '../../../common/utils/object_utils'; import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/common'; import { PivotAggsConfigWithUiSupport } from './pivot_aggs'; +import { isPopulatedObject } from '../../../common/shared_imports'; export enum PIVOT_SUPPORTED_GROUP_BY_AGGS { DATE_HISTOGRAM = 'date_histogram', diff --git a/x-pack/plugins/transform/public/app/common/request.test.ts b/x-pack/plugins/transform/public/app/common/request.test.ts index f25fedb7aaba33..b02ae06b22e9db 100644 --- a/x-pack/plugins/transform/public/app/common/request.test.ts +++ b/x-pack/plugins/transform/public/app/common/request.test.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { estypes } from '@elastic/elasticsearch'; import { PIVOT_SUPPORTED_AGGS } from '../../../common/types/pivot_aggs'; import { PivotGroupByConfig } from '../common'; @@ -29,7 +29,6 @@ import { PivotQuery, } from './request'; import { LatestFunctionConfigUI } from '../../../common/types/transform'; -import { RuntimeField } from '../../../../../../src/plugins/data/common/index_patterns'; const simpleQuery: PivotQuery = { query_string: { query: 'airline:AAL' } }; @@ -273,7 +272,7 @@ describe('Transform: Common', () => { script: { source: "emit(doc['bytes'].value * 2.0)", }, - } as RuntimeField, + } as estypes.RuntimeField, }; const pivotState: StepDefineExposedState = { diff --git a/x-pack/plugins/transform/public/app/common/request.ts b/x-pack/plugins/transform/public/app/common/request.ts index e25548668ac5ff..4be398f0070ccf 100644 --- a/x-pack/plugins/transform/public/app/common/request.ts +++ b/x-pack/plugins/transform/public/app/common/request.ts @@ -17,7 +17,6 @@ import type { PutTransformsPivotRequestSchema, PutTransformsRequestSchema, } from '../../../common/api_schemas/transforms'; -import { isPopulatedObject } from '../../../common/utils/object_utils'; import { DateHistogramAgg, HistogramAgg, TermsAgg } from '../../../common/types/pivot_group_by'; import { isIndexPattern } from '../../../common/types/index_pattern'; @@ -35,6 +34,7 @@ import { PivotAggsConfig, PivotGroupByConfig, } from './'; +import { isPopulatedObject } from '../../../common/shared_imports'; export interface SimpleQuery { query_string: { diff --git a/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx b/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx index bd361afac2d8d7..70848b984ee720 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx +++ b/x-pack/plugins/transform/public/app/hooks/use_index_data.test.tsx @@ -18,6 +18,7 @@ import { SimpleQuery } from '../common'; import { SearchItems } from './use_search_items'; import { useIndexData } from './use_index_data'; +import { estypes } from '@elastic/elasticsearch'; jest.mock('../../shared_imports'); jest.mock('../app_dependencies'); @@ -25,7 +26,6 @@ jest.mock('./use_api'); import { useAppDependencies } from '../__mocks__/app_dependencies'; import { MlSharedContext } from '../__mocks__/shared_context'; -import { RuntimeField } from '../../../../../../src/plugins/data/common/index_patterns'; const query: SimpleQuery = { query_string: { @@ -40,7 +40,7 @@ const runtimeMappings = { script: { source: "emit(doc['bytes'].value * 2.0)", }, - } as RuntimeField, + } as estypes.RuntimeField, }; describe('Transform: useIndexData()', () => { diff --git a/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts b/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts index 28e9f190a91080..3a9ec3b5fd9980 100644 --- a/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts +++ b/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import { Privileges } from '../../../../../common/types/privileges'; -import { isPopulatedObject } from '../../../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../../../common/shared_imports'; export interface Capabilities { canGetTransform: boolean; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx index 1e8397a4d9cc34..d283b09c2e48f7 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx @@ -13,7 +13,7 @@ import { EuiCodeEditor } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { StepDefineFormHook } from '../step_define'; -import { isRuntimeMappings } from '../step_define/common/types'; +import { isRuntimeMappings } from '../../../../../../common/shared_imports'; export const AdvancedRuntimeMappingsEditor: FC = memo( ({ diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx index a7f2a3cd7178d5..79b0b0e5a32f9b 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx @@ -47,8 +47,8 @@ import { PutTransformsPivotRequestSchema, } from '../../../../../../common/api_schemas/transforms'; import type { RuntimeField } from '../../../../../../../../../src/plugins/data/common/index_patterns'; -import { isPopulatedObject } from '../../../../../../common/utils/object_utils'; import { isLatestTransform } from '../../../../../../common/types/transform'; +import { isPopulatedObject } from '../../../../../../common/shared_imports'; export interface StepDetailsExposedState { created: boolean; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/common.test.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/common.test.ts index fcdbac8c7ff39c..5519e9868d5041 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/common.test.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/common.test.ts @@ -8,7 +8,7 @@ import { getPivotDropdownOptions } from '../common'; import { IndexPattern } from '../../../../../../../../../../src/plugins/data/public'; import { FilterAggForm } from './filter_agg/components'; -import type { RuntimeField } from '../../../../../../../../../../src/plugins/data/common/index_patterns'; +import { estypes } from '@elastic/elasticsearch'; describe('Transform: Define Pivot Common', () => { test('getPivotDropdownOptions()', () => { @@ -117,7 +117,7 @@ describe('Transform: Define Pivot Common', () => { script: { source: "emit(doc['bytes'].value * 2.0)", }, - } as RuntimeField, + } as estypes.RuntimeField, }; const optionsWithRuntimeFields = getPivotDropdownOptions(indexPattern, runtimeMappings); expect(optionsWithRuntimeFields).toMatchObject({ diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.test.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.test.tsx index 7f9c4256f77557..734c37802c3889 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.test.tsx @@ -10,12 +10,10 @@ import React from 'react'; import { I18nProvider } from '@kbn/i18n/react'; import { FilterAggForm } from './filter_agg_form'; import { CreateTransformWizardContext } from '../../../../wizard/wizard'; -import { - KBN_FIELD_TYPES, - RuntimeField, -} from '../../../../../../../../../../../../src/plugins/data/common'; +import { KBN_FIELD_TYPES } from '../../../../../../../../../../../../src/plugins/data/common'; import { IndexPattern } from '../../../../../../../../../../../../src/plugins/data/public'; import { FilterTermForm } from './filter_term_form'; +import { estypes } from '@elastic/elasticsearch'; describe('FilterAggForm', () => { const runtimeMappings = { @@ -24,7 +22,7 @@ describe('FilterAggForm', () => { script: { source: "emit(doc['bytes'].value * 2.0)", }, - } as RuntimeField, + } as estypes.RuntimeField, }; const indexPattern = ({ diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx index 3b5d6e0e504973..769666149935a9 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx @@ -14,9 +14,11 @@ import { commonFilterAggs, filterAggsFieldSupport } from '../constants'; import { IndexPattern } from '../../../../../../../../../../../../src/plugins/data/public'; import { getFilterAggTypeConfig } from '../config'; import type { FilterAggType, PivotAggsConfigFilter } from '../types'; -import type { RuntimeMappings } from '../../types'; import { getKibanaFieldTypeFromEsType } from '../../get_pivot_dropdown_options'; -import { isPopulatedObject } from '../../../../../../../../../common/utils/object_utils'; +import { + isPopulatedObject, + RuntimeMappings, +} from '../../../../../../../../../common/shared_imports'; /** * Resolves supported filters for provided field. diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts index 8d85988424e270..bf1a4dab4137f9 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts @@ -26,7 +26,7 @@ import { import { getDefaultAggregationConfig } from './get_default_aggregation_config'; import { getDefaultGroupByConfig } from './get_default_group_by_config'; import type { Field, StepDefineExposedState } from './types'; -import { isRuntimeMappings } from './types'; +import { isRuntimeMappings } from '../../../../../../../common/shared_imports'; const illegalEsAggNameChars = /[[\]>]/g; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts index 6b4ff0090a497a..30f5d893c3da52 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts @@ -23,8 +23,7 @@ import { PivotConfigDefinition, } from '../../../../../../../common/types/transform'; import { LatestFunctionConfig } from '../../../../../../../common/api_schemas/transforms'; - -import { isPopulatedObject } from '../../../../../../../common/utils/object_utils'; +import { isPopulatedObject, RuntimeMappings } from '../../../../../../../common/shared_imports'; export interface ErrorMessage { query: string; @@ -36,20 +35,6 @@ export interface Field { type: KBN_FIELD_TYPES; } -// Replace this with import once #88995 is merged -const RUNTIME_FIELD_TYPES = ['keyword', 'long', 'double', 'date', 'ip', 'boolean'] as const; -type RuntimeType = typeof RUNTIME_FIELD_TYPES[number]; - -export interface RuntimeField { - type: RuntimeType; - script?: - | string - | { - source: string; - }; -} - -export type RuntimeMappings = Record; export interface StepDefineExposedState { transformFunction: TransformFunction; aggList: PivotAggsConfigDict; @@ -72,26 +57,6 @@ export interface StepDefineExposedState { isRuntimeMappingsEditorEnabled: boolean; } -export function isRuntimeField(arg: unknown): arg is RuntimeField { - return ( - isPopulatedObject(arg) && - ((Object.keys(arg).length === 1 && arg.hasOwnProperty('type')) || - (Object.keys(arg).length === 2 && - arg.hasOwnProperty('type') && - arg.hasOwnProperty('script') && - (typeof arg.script === 'string' || - (isPopulatedObject(arg.script) && - Object.keys(arg.script).length === 1 && - arg.script.hasOwnProperty('source') && - typeof arg.script.source === 'string')))) && - RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType) - ); -} - -export function isRuntimeMappings(arg: unknown): arg is RuntimeMappings { - return isPopulatedObject(arg) && Object.values(arg).every((d) => isRuntimeField(d)); -} - export function isPivotPartialRequest(arg: unknown): arg is { pivot: PivotConfigDefinition } { return isPopulatedObject(arg) && arg.hasOwnProperty('pivot'); } diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/wizard/wizard.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/wizard/wizard.tsx index 5ae464affa0164..42e7646a1bcb9f 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/wizard/wizard.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/wizard/wizard.tsx @@ -32,7 +32,7 @@ import { } from '../step_details'; import { WizardNav } from '../wizard_nav'; import { IndexPattern } from '../../../../../../../../../src/plugins/data/public'; -import type { RuntimeMappings } from '../step_define/common/types'; +import { RuntimeMappings } from '../../../../../../common/shared_imports'; enum KBN_MANAGEMENT_PAGE_CLASSNAME { DEFAULT_BODY = 'mgtPage__body', diff --git a/x-pack/plugins/transform/server/routes/api/field_histograms.ts b/x-pack/plugins/transform/server/routes/api/field_histograms.ts index bb2a1b278a5c20..bfe2f470785690 100644 --- a/x-pack/plugins/transform/server/routes/api/field_histograms.ts +++ b/x-pack/plugins/transform/server/routes/api/field_histograms.ts @@ -41,7 +41,6 @@ export function registerFieldHistogramsRoutes({ router, license }: RouteDependen query, fields, samplerShardSize, - // @ts-expect-error script is not compatible with StoredScript from @elastic/elasticsearch: string is not supported runtimeMappings ); diff --git a/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts b/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts index afdcc939983039..22ede604031d5f 100644 --- a/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts +++ b/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts @@ -5,13 +5,12 @@ * 2.0. */ -import { isPopulatedObject } from '../../../common/utils/object_utils'; - import { RouteDependencies } from '../../types'; import { addBasePath } from '../index'; import { wrapError, wrapEsError } from './error_utils'; +import { isPopulatedObject } from '../../../common/shared_imports'; const NODE_ROLES = 'roles';