mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[ML] Update to use some shared common functions between ml and transform
This commit is contained in:
parent
54b62f827e
commit
ce813f012d
29 changed files with 66 additions and 107 deletions
|
@ -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';
|
||||
|
|
18
x-pack/plugins/ml/common/util/object_utils.test.ts
Normal file
18
x-pack/plugins/ml/common/util/object_utils.test.ts
Normal file
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -11,4 +11,8 @@ export {
|
|||
patternValidator,
|
||||
ChartData,
|
||||
HITS_TOTAL_RELATION,
|
||||
isPopulatedObject,
|
||||
isRuntimeMappings,
|
||||
isRuntimeField,
|
||||
RuntimeMappings,
|
||||
} from '../../ml/common';
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { isPopulatedObject } from './object_utils';
|
||||
import { isPopulatedObject } from '../shared_imports';
|
||||
|
||||
export interface ErrorResponse {
|
||||
body: {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -51,7 +51,3 @@ export const setNestedProperty = (obj: Record<string, any>, accessor: string, va
|
|||
|
||||
return obj;
|
||||
};
|
||||
|
||||
export const isPopulatedObject = <T = Record<string, unknown>>(arg: unknown): arg is T => {
|
||||
return typeof arg === 'object' && arg !== null && Object.keys(arg).length > 0;
|
||||
};
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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: {
|
||||
|
|
|
@ -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()', () => {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<StepDefineFormHook['runtimeMappingsEditor']> = memo(
|
||||
({
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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 = ({
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<string, RuntimeField>;
|
||||
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');
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue