forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Transforms: Create optional data view on server side as part of …
…transform create API call. (elastic#160513) When creating a transform, users have the option to create a Kibana data view as part of the creation process. So far we've done that as a separate call from the client side. This PR adds query param options to the API endpoint to optionally create the data view server side. API integration tests have been extended to test the new feature.
- Loading branch information
Showing
10 changed files
with
250 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
x-pack/plugins/transform/server/routes/api/transforms_create/route_handler.ts
This file was deleted.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
x-pack/plugins/transform/server/routes/api/transforms_create/route_handler_factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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 type { RequestHandler } from '@kbn/core/server'; | ||
import type { RuntimeField } from '@kbn/data-views-plugin/common'; | ||
import { isPopulatedObject } from '@kbn/ml-is-populated-object'; | ||
|
||
import type { TransformIdParamSchema } from '../../../../common/api_schemas/common'; | ||
import type { | ||
PutTransformsRequestSchema, | ||
PutTransformsQuerySchema, | ||
PutTransformsResponseSchema, | ||
} from '../../../../common/api_schemas/transforms'; | ||
import { isLatestTransform } from '../../../../common/types/transform'; | ||
|
||
import type { RouteDependencies } from '../../../types'; | ||
import type { TransformRequestHandlerContext } from '../../../services/license'; | ||
|
||
import { wrapEsError } from '../../utils/error_utils'; | ||
|
||
export const routeHandlerFactory: ( | ||
routeDependencies: RouteDependencies | ||
) => RequestHandler< | ||
TransformIdParamSchema, | ||
PutTransformsQuerySchema, | ||
PutTransformsRequestSchema, | ||
TransformRequestHandlerContext | ||
> = (routeDependencies) => async (ctx, req, res) => { | ||
const { coreStart, dataViews } = routeDependencies; | ||
const { transformId } = req.params; | ||
const { createDataView, timeFieldName } = req.query; | ||
|
||
const response: PutTransformsResponseSchema = { | ||
dataViewsCreated: [], | ||
dataViewsErrors: [], | ||
transformsCreated: [], | ||
errors: [], | ||
}; | ||
|
||
const esClient = (await ctx.core).elasticsearch.client; | ||
|
||
try { | ||
const resp = await esClient.asCurrentUser.transform.putTransform({ | ||
// @ts-expect-error @elastic/elasticsearch group_by is expected to be optional in TransformPivot | ||
body: req.body, | ||
transform_id: transformId, | ||
}); | ||
|
||
if (resp.acknowledged) { | ||
response.transformsCreated.push({ transform: transformId }); | ||
} else { | ||
response.errors.push({ | ||
id: transformId, | ||
error: wrapEsError(resp), | ||
}); | ||
} | ||
} catch (e) { | ||
response.errors.push({ | ||
id: transformId, | ||
error: wrapEsError(e), | ||
}); | ||
} | ||
|
||
if (createDataView) { | ||
const { savedObjects, elasticsearch } = coreStart; | ||
const dataViewsService = await dataViews.dataViewsServiceFactory( | ||
savedObjects.getScopedClient(req), | ||
elasticsearch.client.asScoped(req).asCurrentUser, | ||
req | ||
); | ||
|
||
const dataViewName = req.body.dest.index; | ||
const runtimeMappings = req.body.source.runtime_mappings as Record<string, RuntimeField>; | ||
|
||
try { | ||
const dataViewsResp = await dataViewsService.createAndSave( | ||
{ | ||
title: dataViewName, | ||
timeFieldName, | ||
// Adding runtime mappings for transforms of type latest only here | ||
// since only they will want to replicate the source index mapping. | ||
// Pivot type transforms have index mappings that cannot be | ||
// inferred from the source index. | ||
...(isPopulatedObject(runtimeMappings) && isLatestTransform(req.body) | ||
? { runtimeFieldMap: runtimeMappings } | ||
: {}), | ||
allowNoIndex: true, | ||
}, | ||
false, | ||
true | ||
); | ||
|
||
if (dataViewsResp.id) { | ||
response.dataViewsCreated = [{ id: dataViewsResp.id }]; | ||
} | ||
} catch (error) { | ||
// For the error id we use the transform id | ||
// because in case of an error we don't get a data view id. | ||
response.dataViewsErrors = [{ id: transformId, error }]; | ||
} | ||
} | ||
|
||
return res.ok({ body: response }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.