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] AIOps: Refactor routes to use handlers. (elastic#170096)
This refactors the code for server side routes in the AIOps plugin. It splits up the routes into an outer `define_route.ts` and an inner `route_handler_factory.ts` file. - `define_route.ts` creates a versioned route and uses `route_handler_factory.ts` to be passed in as the route handler. - `route_handler_factory.ts` includes all logic for the route, previously this was an inline callback in the route definition. The aim of this PR is to be better prepared for future route versioning. Splitting out the handler simplifies the route definition and will allow the reuse of handlers for different versions or the usage of different handlers.
- Loading branch information
Showing
50 changed files
with
1,088 additions
and
1,037 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
37 changes: 37 additions & 0 deletions
37
x-pack/plugins/aiops/server/routes/categorization_field_validation/define_route.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,37 @@ | ||
/* | ||
* 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 { IRouter } from '@kbn/core/server'; | ||
import type { DataRequestHandlerContext } from '@kbn/data-plugin/server'; | ||
import type { UsageCounter } from '@kbn/usage-collection-plugin/server'; | ||
import { categorizationFieldValidationSchema } from '../../../common/api/log_categorization/schema'; | ||
import { AIOPS_API_ENDPOINT } from '../../../common/api'; | ||
import type { AiopsLicense } from '../../types'; | ||
import { routeHandlerFactory } from './route_handler_factory'; | ||
|
||
export const defineRoute = ( | ||
router: IRouter<DataRequestHandlerContext>, | ||
license: AiopsLicense, | ||
usageCounter?: UsageCounter | ||
) => { | ||
router.versioned | ||
.post({ | ||
path: AIOPS_API_ENDPOINT.CATEGORIZATION_FIELD_VALIDATION, | ||
access: 'internal', | ||
}) | ||
.addVersion( | ||
{ | ||
version: '1', | ||
validate: { | ||
request: { | ||
body: categorizationFieldValidationSchema, | ||
}, | ||
}, | ||
}, | ||
routeHandlerFactory(license, usageCounter) | ||
); | ||
}; |
83 changes: 83 additions & 0 deletions
83
x-pack/plugins/aiops/server/routes/categorization_field_validation/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,83 @@ | ||
/* | ||
* 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 { | ||
KibanaRequest, | ||
RequestHandlerContext, | ||
RequestHandler, | ||
KibanaResponseFactory, | ||
} from '@kbn/core/server'; | ||
import { categorizationExamplesProvider } from '@kbn/ml-category-validator'; | ||
import type { UsageCounter } from '@kbn/usage-collection-plugin/server'; | ||
import { wrapError } from '../error_wrapper'; | ||
import { trackAIOpsRouteUsage } from '../../lib/track_route_usage'; | ||
import { AIOPS_TELEMETRY_ID } from '../../../common/constants'; | ||
import { AIOPS_API_ENDPOINT } from '../../../common/api'; | ||
import type { AiopsLicense } from '../../types'; | ||
import type { CategorizationFieldValidationSchema } from '../../../common/api/log_categorization/schema'; | ||
|
||
export const routeHandlerFactory: ( | ||
license: AiopsLicense, | ||
usageCounter?: UsageCounter | ||
) => RequestHandler<unknown, unknown, CategorizationFieldValidationSchema> = | ||
(license, usageCounter) => | ||
async ( | ||
context: RequestHandlerContext, | ||
request: KibanaRequest<unknown, unknown, CategorizationFieldValidationSchema>, | ||
response: KibanaResponseFactory | ||
) => { | ||
const { headers } = request; | ||
trackAIOpsRouteUsage( | ||
`POST ${AIOPS_API_ENDPOINT.CATEGORIZATION_FIELD_VALIDATION}`, | ||
headers[AIOPS_TELEMETRY_ID.AIOPS_ANALYSIS_RUN_ORIGIN], | ||
usageCounter | ||
); | ||
|
||
if (!license.isActivePlatinumLicense) { | ||
return response.forbidden(); | ||
} | ||
try { | ||
const { | ||
elasticsearch: { client }, | ||
} = await context.core; | ||
|
||
const { | ||
indexPatternTitle, | ||
timeField, | ||
query, | ||
size, | ||
field, | ||
start, | ||
end, | ||
analyzer, | ||
runtimeMappings, | ||
indicesOptions, | ||
includeExamples, | ||
} = request.body; | ||
|
||
const { validateCategoryExamples } = categorizationExamplesProvider(client); | ||
const resp = await validateCategoryExamples( | ||
indexPatternTitle, | ||
query, | ||
size, | ||
field, | ||
timeField, | ||
start, | ||
end, | ||
analyzer ?? {}, | ||
runtimeMappings, | ||
indicesOptions, | ||
includeExamples | ||
); | ||
|
||
return response.ok({ | ||
body: resp, | ||
}); | ||
} catch (e) { | ||
return response.customError(wrapError(e)); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.