Skip to content

Commit

Permalink
[ML] Transforms: Migrate IndexPattern service usage to DataView servi…
Browse files Browse the repository at this point in the history
…ce (#128247)

* [ML] Transforms: Migrate IndexPattern service usage to DataView service

* [ML] Fix tests

* [ML] Fix API delete_transforms tests

* [ML] Edits from review
  • Loading branch information
peteharverson authored Mar 24, 2022
1 parent 6dbad1d commit 337fadf
Show file tree
Hide file tree
Showing 61 changed files with 451 additions and 494 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/transform/common/api_schemas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export const transformStateSchema = schema.oneOf([
schema.literal(TRANSFORM_STATE.WAITING),
]);

export const indexPatternTitleSchema = schema.object({
export const dataViewTitleSchema = schema.object({
/** Title of the data view for which to return stats. */
indexPatternTitle: schema.string(),
dataViewTitle: schema.string(),
});

export type IndexPatternTitleSchema = TypeOf<typeof indexPatternTitleSchema>;
export type DataViewTitleSchema = TypeOf<typeof dataViewTitleSchema>;

export const transformIdParamSchema = schema.object({
transformId: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const deleteTransformsRequestSchema = schema.object({
})
),
deleteDestIndex: schema.maybe(schema.boolean()),
deleteDestIndexPattern: schema.maybe(schema.boolean()),
deleteDestDataView: schema.maybe(schema.boolean()),
forceDelete: schema.maybe(schema.boolean()),
});

Expand All @@ -29,7 +29,7 @@ export type DeleteTransformsRequestSchema = TypeOf<typeof deleteTransformsReques
export interface DeleteTransformStatus {
transformDeleted: ResponseStatus;
destIndexDeleted?: ResponseStatus;
destIndexPatternDeleted?: ResponseStatus;
destDataViewDeleted?: ResponseStatus;
destinationIndex?: string | undefined;
}

Expand Down
21 changes: 21 additions & 0 deletions x-pack/plugins/transform/common/types/data_view.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { isDataView } from './data_view';

describe('data_view', () => {
test('isDataView()', () => {
expect(isDataView(0)).toBe(false);
expect(isDataView('')).toBe(false);
expect(isDataView(null)).toBe(false);
expect(isDataView({})).toBe(false);
expect(isDataView({ attribute: 'value' })).toBe(false);
expect(isDataView({ fields: [], title: 'Data View Title', getComputedFields: () => {} })).toBe(
true
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* 2.0.
*/

import type { IndexPattern } from '../../../../../src/plugins/data/common';
import type { DataView } from '../../../../../src/plugins/data_views/common';

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 {
// Custom minimal type guard for DataView to check against the attributes used in transforms code.
export function isDataView(arg: any): arg is DataView {
return (
isPopulatedObject(arg, ['title', 'fields']) &&
// `getComputedFields` is inherited, so it's not possible to
// check with `hasOwnProperty` which is used by isPopulatedObject()
'getComputedFields' in (arg as IndexPattern) &&
typeof (arg as IndexPattern).getComputedFields === 'function' &&
'getComputedFields' in (arg as DataView) &&
typeof (arg as DataView).getComputedFields === 'function' &&
typeof arg.title === 'string' &&
Array.isArray(arg.fields)
);
Expand Down
21 changes: 0 additions & 21 deletions x-pack/plugins/transform/common/types/index_pattern.test.ts

This file was deleted.

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 @@ -13,7 +13,7 @@ import type { PivotAggDict } from './pivot_aggs';
import type { TransformHealthAlertRule } from './alerting';

export type IndexName = string;
export type IndexPattern = string;
export type DataView = string;
export type TransformId = string;

/**
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/transform/public/app/common/data_grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const getPivotPreviewDevConsoleStatement = (request: PostTransformsPrevie
return `POST _transform/_preview\n${JSON.stringify(request, null, 2)}\n`;
};

export const getIndexDevConsoleStatement = (query: PivotQuery, indexPatternTitle: string) => {
return `GET ${indexPatternTitle}/_search\n${JSON.stringify(
export const getIndexDevConsoleStatement = (query: PivotQuery, dataViewTitle: string) => {
return `GET ${dataViewTitle}/_search\n${JSON.stringify(
{
query,
},
Expand Down
38 changes: 17 additions & 21 deletions x-pack/plugins/transform/public/app/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('Transform: Common', () => {
test('getPreviewTransformRequestBody()', () => {
const query = getPivotQuery('the-query');

const request = getPreviewTransformRequestBody('the-index-pattern-title', query, {
const request = getPreviewTransformRequestBody('the-data-view-title', query, {
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
Expand All @@ -93,32 +93,28 @@ describe('Transform: Common', () => {
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
source: {
index: ['the-index-pattern-title'],
index: ['the-data-view-title'],
query: { query_string: { default_operator: 'AND', query: 'the-query' } },
},
});
});

test('getPreviewTransformRequestBody() with comma-separated index pattern', () => {
const query = getPivotQuery('the-query');
const request = getPreviewTransformRequestBody(
'the-index-pattern-title,the-other-title',
query,
{
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
}
);
const request = getPreviewTransformRequestBody('the-data-view-title,the-other-title', query, {
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
});

expect(request).toEqual({
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
source: {
index: ['the-index-pattern-title', 'the-other-title'],
index: ['the-data-view-title', 'the-other-title'],
query: { query_string: { default_operator: 'AND', query: 'the-query' } },
},
});
Expand Down Expand Up @@ -178,7 +174,7 @@ describe('Transform: Common', () => {
test('getPreviewTransformRequestBody() with missing_buckets config', () => {
const query = getPivotQuery('the-query');
const request = getPreviewTransformRequestBody(
'the-index-pattern-title',
'the-data-view-title',
query,
getRequestPayload([aggsAvg], [{ ...groupByTerms, ...{ missing_bucket: true } }])
);
Expand All @@ -191,7 +187,7 @@ describe('Transform: Common', () => {
},
},
source: {
index: ['the-index-pattern-title'],
index: ['the-data-view-title'],
query: { query_string: { default_operator: 'AND', query: 'the-query' } },
},
});
Expand Down Expand Up @@ -226,7 +222,7 @@ describe('Transform: Common', () => {
const transformDetailsState: StepDetailsExposedState = {
continuousModeDateField: 'the-continuous-mode-date-field',
continuousModeDelay: 'the-continuous-mode-delay',
createIndexPattern: false,
createDataView: false,
isContinuousModeEnabled: false,
isRetentionPolicyEnabled: false,
retentionPolicyDateField: '',
Expand All @@ -243,7 +239,7 @@ describe('Transform: Common', () => {
};

const request = getCreateTransformRequestBody(
'the-index-pattern-title',
'the-data-view-title',
pivotState,
transformDetailsState
);
Expand All @@ -261,7 +257,7 @@ describe('Transform: Common', () => {
docs_per_second: 400,
},
source: {
index: ['the-index-pattern-title'],
index: ['the-data-view-title'],
query: { query_string: { default_operator: 'AND', query: 'the-search-query' } },
},
});
Expand Down Expand Up @@ -305,7 +301,7 @@ describe('Transform: Common', () => {
const transformDetailsState: StepDetailsExposedState = {
continuousModeDateField: 'the-continuous-mode-date-field',
continuousModeDelay: 'the-continuous-mode-delay',
createIndexPattern: false,
createDataView: false,
isContinuousModeEnabled: false,
isRetentionPolicyEnabled: false,
retentionPolicyDateField: '',
Expand All @@ -322,7 +318,7 @@ describe('Transform: Common', () => {
};

const request = getCreateTransformRequestBody(
'the-index-pattern-title',
'the-data-view-title',
pivotState,
transformDetailsState
);
Expand All @@ -340,7 +336,7 @@ describe('Transform: Common', () => {
docs_per_second: 400,
},
source: {
index: ['the-index-pattern-title'],
index: ['the-data-view-title'],
query: { query_string: { default_operator: 'AND', query: 'the-search-query' } },
runtime_mappings: runtimeMappings,
},
Expand Down
18 changes: 9 additions & 9 deletions x-pack/plugins/transform/public/app/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import { HttpFetchError } from '../../../../../../src/core/public';
import type { IndexPattern } from '../../../../../../src/plugins/data/public';
import type { DataView } from '../../../../../../src/plugins/data_views/public';

import type {
PivotTransformPreviewRequestSchema,
Expand All @@ -19,7 +19,7 @@ import type {
} from '../../../common/api_schemas/transforms';
import { isPopulatedObject } from '../../../common/shared_imports';
import { DateHistogramAgg, HistogramAgg, TermsAgg } from '../../../common/types/pivot_group_by';
import { isIndexPattern } from '../../../common/types/index_pattern';
import { isDataView } from '../../../common/types/data_view';

import type { SavedSearchQuery } from '../hooks/use_search_items';
import type { StepDefineExposedState } from '../sections/create_transform/components/step_define';
Expand Down Expand Up @@ -78,14 +78,14 @@ export function isDefaultQuery(query: PivotQuery): boolean {
}

export function getCombinedRuntimeMappings(
indexPattern: IndexPattern | undefined,
dataView: DataView | undefined,
runtimeMappings?: StepDefineExposedState['runtimeMappings']
): StepDefineExposedState['runtimeMappings'] | undefined {
let combinedRuntimeMappings = {};

// And runtime field mappings defined by index pattern
if (isIndexPattern(indexPattern)) {
const computedFields = indexPattern.getComputedFields();
if (isDataView(dataView)) {
const computedFields = dataView.getComputedFields();
if (computedFields?.runtimeFields !== undefined) {
const ipRuntimeMappings = computedFields.runtimeFields;
if (isPopulatedObject(ipRuntimeMappings)) {
Expand Down Expand Up @@ -167,12 +167,12 @@ export const getRequestPayload = (
};

export function getPreviewTransformRequestBody(
indexPatternTitle: IndexPattern['title'],
dataViewTitle: DataView['title'],
query: PivotQuery,
partialRequest?: StepDefineExposedState['previewRequest'] | undefined,
runtimeMappings?: StepDefineExposedState['runtimeMappings']
): PostTransformsPreviewRequestSchema {
const index = indexPatternTitle.split(',').map((name: string) => name.trim());
const index = dataViewTitle.split(',').map((name: string) => name.trim());

return {
source: {
Expand All @@ -199,12 +199,12 @@ export const getCreateTransformSettingsRequestBody = (
};

export const getCreateTransformRequestBody = (
indexPatternTitle: IndexPattern['title'],
dataViewTitle: DataView['title'],
pivotState: StepDefineExposedState,
transformDetailsState: StepDetailsExposedState
): PutTransformsPivotRequestSchema | PutTransformsLatestRequestSchema => ({
...getPreviewTransformRequestBody(
indexPatternTitle,
dataViewTitle,
getPivotQuery(pivotState.searchQuery),
pivotState.previewRequest,
pivotState.runtimeMappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const apiFactory = () => ({
return Promise.resolve([]);
},
async getHistogramsForFields(
indexPatternTitle: string,
dataViewTitle: string,
fields: FieldHistogramRequestConfig[],
query: string | SavedSearchQuery,
samplerShardSize = DEFAULT_SAMPLER_SHARD_SIZE
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/transform/public/app/hooks/use_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ export const useApi = () => {
}
},
async getHistogramsForFields(
indexPatternTitle: string,
dataViewTitle: string,
fields: FieldHistogramRequestConfig[],
query: string | SavedSearchQuery,
runtimeMappings?: FieldHistogramsRequestSchema['runtimeMappings'],
samplerShardSize = DEFAULT_SAMPLER_SHARD_SIZE
): Promise<FieldHistogramsResponseSchema | HttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}field_histograms/${indexPatternTitle}`, {
return await http.post(`${API_BASE_PATH}field_histograms/${dataViewTitle}`, {
body: JSON.stringify({
query,
fields,
Expand Down
Loading

0 comments on commit 337fadf

Please sign in to comment.