Skip to content

Commit

Permalink
[ML] Update to use some shared common functions between ml and transform
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Mar 26, 2021
1 parent 54b62f8 commit ce813f0
Show file tree
Hide file tree
Showing 29 changed files with 66 additions and 107 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/ml/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 18 additions & 0 deletions x-pack/plugins/ml/common/util/object_utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/ml/common/util/runtime_field_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 2 additions & 5 deletions x-pack/plugins/ml/common/util/runtime_field_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
);
}

Expand Down
31 changes: 11 additions & 20 deletions x-pack/plugins/transform/common/api_schemas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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';
}
},
}
)
);
3 changes: 1 addition & 2 deletions x-pack/plugins/transform/common/api_schemas/type_guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/transform/common/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export {
patternValidator,
ChartData,
HITS_TOTAL_RELATION,
isPopulatedObject,
isRuntimeMappings,
isRuntimeField,
RuntimeMappings,
} from '../../ml/common';
3 changes: 1 addition & 2 deletions x-pack/plugins/transform/common/types/index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/common/types/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/common/types/transform_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/common/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { isPopulatedObject } from './object_utils';
import { isPopulatedObject } from '../shared_imports';

export interface ErrorResponse {
body: {
Expand Down
10 changes: 1 addition & 9 deletions x-pack/plugins/transform/common/utils/object_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { getNestedProperty, isPopulatedObject } from './object_utils';
import { getNestedProperty } from './object_utils';

describe('object_utils', () => {
test('getNestedProperty()', () => {
Expand Down Expand Up @@ -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);
});
});
4 changes: 0 additions & 4 deletions x-pack/plugins/transform/common/utils/object_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/public/app/common/pivot_aggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/transform/public/app/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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' } };

Expand Down Expand Up @@ -273,7 +272,7 @@ describe('Transform: Common', () => {
script: {
source: "emit(doc['bytes'].value * 2.0)",
},
} as RuntimeField,
} as estypes.RuntimeField,
};

const pivotState: StepDefineExposedState = {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/public/app/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -35,6 +34,7 @@ import {
PivotAggsConfig,
PivotGroupByConfig,
} from './';
import { isPopulatedObject } from '../../../common/shared_imports';

export interface SimpleQuery {
query_string: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ 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');
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: {
Expand All @@ -40,7 +40,7 @@ const runtimeMappings = {
script: {
source: "emit(doc['bytes'].value * 2.0)",
},
} as RuntimeField,
} as estypes.RuntimeField,
};

describe('Transform: useIndexData()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -24,7 +22,7 @@ describe('FilterAggForm', () => {
script: {
source: "emit(doc['bytes'].value * 2.0)",
},
} as RuntimeField,
} as estypes.RuntimeField,
};

const indexPattern = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit ce813f0

Please sign in to comment.