Skip to content

Commit

Permalink
[ML] Fix types. Move shared utils to ML.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Mar 31, 2021
1 parent 83a1497 commit a68405f
Show file tree
Hide file tree
Showing 21 changed files with 37 additions and 81 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/ml/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export { ES_CLIENT_TOTAL_HITS_RELATION } from './types/es_client';
export { ChartData } from './types/field_histograms';
export { ANOMALY_SEVERITY, ANOMALY_THRESHOLD, SEVERITY_COLORS } from './constants/anomalies';
export { getSeverityColor, getSeverityType } from './util/anomaly_utils';
export { isPopulatedObject } from './util/object_utils';
export { isRuntimeMappings } from './util/runtime_field_utils';
export { composeValidators, patternValidator } from './util/validators';
export { extractErrorMessage } from './util/errors';
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* 2.0.
*/

import { isRuntimeField, isRuntimeMappings } from './types';
import { isRuntimeField, isRuntimeMappings } from './runtime_field_utils';

describe('Transform: step_define type guards', () => {
it('isRuntimeField()', () => {
describe('ML runtime field utils', () => {
describe('isRuntimeField()', () => {
expect(isRuntimeField(1)).toBe(false);
expect(isRuntimeField(null)).toBe(false);
expect(isRuntimeField([])).toBe(false);
Expand All @@ -21,7 +21,7 @@ describe('Transform: step_define type guards', () => {
expect(isRuntimeField({ type: 'keyword', script: 'some script' })).toBe(true);
});

it('isRuntimeMappings()', () => {
describe('isRuntimeMappings()', () => {
expect(isRuntimeMappings(1)).toBe(false);
expect(isRuntimeMappings(null)).toBe(false);
expect(isRuntimeMappings([])).toBe(false);
Expand Down
15 changes: 7 additions & 8 deletions x-pack/plugins/ml/common/util/runtime_field_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import { isPopulatedObject } from './object_utils';
import { RUNTIME_FIELD_TYPES } from '../../../../../src/plugins/data/common';
import type { RuntimeField, RuntimeMappings } from '../types/fields';

type RuntimeType = typeof RUNTIME_FIELD_TYPES[number];

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') &&
((isPopulatedObject(arg, ['type']) && Object.keys(arg).length === 1) ||
(isPopulatedObject(arg, ['type', 'script']) &&
Object.keys(arg).length === 2 &&
(typeof arg.script === 'string' ||
(isPopulatedObject(arg.script) &&
(isPopulatedObject(arg.script, ['source']) &&
Object.keys(arg.script).length === 1 &&
arg.script.hasOwnProperty('source') &&
typeof arg.script.source === 'string')))) &&
RUNTIME_FIELD_TYPES.includes(arg.type)
RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType)
);
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/common/api_schemas/type_guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { estypes } from '@elastic/elasticsearch';

import type { EsIndex } from '../types/es_index';
import { isPopulatedObject } from '../utils/object_utils';
import { isPopulatedObject } from '../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
8 changes: 7 additions & 1 deletion x-pack/plugins/transform/common/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
* 2.0.
*/

export { composeValidators, patternValidator, ChartData } from '../../ml/common';
export {
composeValidators,
isPopulatedObject,
isRuntimeMappings,
patternValidator,
ChartData,
} from '../../ml/common';
2 changes: 1 addition & 1 deletion x-pack/plugins/transform/common/types/index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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,7 +7,7 @@

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 { isPopulatedObject } from '../shared_imports';
import { PivotGroupByDict } from './pivot_group_by';
import { PivotAggDict } from './pivot_aggs';

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,7 +6,7 @@
*/

import { TransformState, TRANSFORM_STATE } from '../constants';
import { isPopulatedObject } from '../utils/object_utils';
import { isPopulatedObject } from '../shared_imports';
import { TransformId } from './transform';

export interface TransformStats {
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
24 changes: 1 addition & 23 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,26 +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);
expect(isPopulatedObject({ attribute: 'value' }, ['otherAttribute'])).toBe(false);
expect(isPopulatedObject({ attribute: 'value' }, ['attribute'])).toBe(true);
expect(
isPopulatedObject({ attribute1: 'value1', attribute2: 'value2' }, [
'attribute1',
'attribute2',
])
).toBe(true);
expect(
isPopulatedObject({ attribute1: 'value1', attribute2: 'value2' }, [
'attribute1',
'otherAttribute',
])
).toBe(false);
});
});
13 changes: 0 additions & 13 deletions x-pack/plugins/transform/common/utils/object_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,3 @@ export const setNestedProperty = (obj: Record<string, any>, accessor: string, va

return obj;
};

export const isPopulatedObject = <T = Record<string, unknown>>(
arg: unknown,
requiredAttributes: string[] = []
): arg is T => {
return (
typeof arg === 'object' &&
arg !== null &&
Object.keys(arg).length > 0 &&
(requiredAttributes.length === 0 ||
requiredAttributes.every((d) => ({}.hasOwnProperty.call(arg, d))))
);
};
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,7 +14,7 @@ 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 { isPopulatedObject } from '../../../common/shared_imports';

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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 { isPopulatedObject } from '../../../common/shared_imports';
import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/common';
import { PivotAggsConfigWithUiSupport } from './pivot_aggs';

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,7 @@ import type {
PutTransformsPivotRequestSchema,
PutTransformsRequestSchema,
} from '../../../common/api_schemas/transforms';
import { isPopulatedObject } from '../../../common/utils/object_utils';
import { isPopulatedObject } from '../../../common/shared_imports';
import { DateHistogramAgg, HistogramAgg, TermsAgg } from '../../../common/types/pivot_group_by';
import { isIndexPattern } from '../../../common/types/index_pattern';

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 @@ -12,8 +12,9 @@ import { EuiCodeEditor } from '@elastic/eui';

import { i18n } from '@kbn/i18n';

import { isRuntimeMappings } from '../../../../../../common/shared_imports';

import { StepDefineFormHook } from '../step_define';
import { isRuntimeMappings } from '../step_define/common/types';

export const AdvancedRuntimeMappingsEditor: FC<StepDefineFormHook['runtimeMappingsEditor']> = memo(
({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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 { isPopulatedObject } from '../../../../../../common/shared_imports';
import { isLatestTransform } from '../../../../../../common/types/transform';

export interface StepDetailsExposedState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 } from '../../../../../../../../../common/shared_imports';

/**
* Resolves supported filters for provided field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../../../../../../../../../src/plugins/data/public';

import { getNestedProperty } from '../../../../../../../common/utils/object_utils';
import { isRuntimeMappings } from '../../../../../../../common/shared_imports';

import {
DropDownLabel,
Expand All @@ -26,7 +27,6 @@ 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';

const illegalEsAggNameChars = /[[\]>]/g;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '../../../../../../../common/types/transform';
import { LatestFunctionConfig } from '../../../../../../../common/api_schemas/transforms';

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

export interface ErrorMessage {
query: string;
Expand Down Expand Up @@ -72,23 +72,6 @@ export interface StepDefineExposedState {
isRuntimeMappingsEditorEnabled: boolean;
}

export function isRuntimeField(arg: unknown): arg is RuntimeField {
return (
((isPopulatedObject(arg, ['type']) && Object.keys(arg).length === 1) ||
(isPopulatedObject(arg, ['type', 'script']) &&
Object.keys(arg).length === 2 &&
(typeof arg.script === 'string' ||
(isPopulatedObject(arg.script, ['source']) &&
Object.keys(arg.script).length === 1 &&
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, ['pivot']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

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

import { RouteDependencies } from '../../types';

Expand Down

0 comments on commit a68405f

Please sign in to comment.