Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] New Platform server shim: update fields service routes #58060

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
* you may not use this file except in compliance with the Elastic License.
*/

import { APICaller } from 'src/core/server';

export function fieldsServiceProvider(
callAsCurrentUser: APICaller
): {
getCardinalityOfFields: (
index: string[] | string,
fieldNames: string[],
query: any,
timeFieldName: string,
earliestMs: number,
latestMs: number
) => Promise<any>;
getTimeFieldRange: (index: string[] | string, timeFieldName: string, query: any) => Promise<any>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Service for carrying out queries to obtain data
// specific to fields in Elasticsearch indices.

export function fieldsServiceProvider(callWithRequest) {
export function fieldsServiceProvider(callAsCurrentUser) {
// Obtains the cardinality of one or more fields.
// Returns an Object whose keys are the names of the fields,
// with values equal to the cardinality of the field.
Expand All @@ -17,7 +17,7 @@ export function fieldsServiceProvider(callWithRequest) {
// First check that each of the supplied fieldNames are aggregatable,
// then obtain the cardinality for each of the aggregatable fields.
return new Promise((resolve, reject) => {
callWithRequest('fieldCaps', {
callAsCurrentUser('fieldCaps', {
index,
fields: fieldNames,
})
Expand Down Expand Up @@ -72,7 +72,7 @@ export function fieldsServiceProvider(callWithRequest) {
aggs,
};

callWithRequest('search', {
callAsCurrentUser('search', {
index,
body,
})
Expand Down Expand Up @@ -106,7 +106,7 @@ export function fieldsServiceProvider(callWithRequest) {
return new Promise((resolve, reject) => {
const obj = { success: true, start: { epoch: 0, string: '' }, end: { epoch: 0, string: '' } };

callWithRequest('search', {
callAsCurrentUser('search', {
index,
size: 0,
body: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { schema } from '@kbn/config-schema';

export const getCardinalityOfFieldsSchema = schema.object({
index: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
fieldNames: schema.maybe(schema.arrayOf(schema.string())),
query: schema.maybe(schema.any()),
timeFieldName: schema.maybe(schema.string()),
earliestMs: schema.maybe(schema.oneOf([schema.number(), schema.string()])),
latestMs: schema.maybe(schema.oneOf([schema.number(), schema.string()])),
});

export const getTimeFieldRangeSchema = schema.object({
index: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
timeFieldName: schema.maybe(schema.string()),
query: schema.maybe(schema.any()),
});
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/apidoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
"DeleteDatafeed",
"StartDatafeed",
"StopDatafeed",
"PreviewDatafeed"
"PreviewDatafeed",
"FieldsService",
"GetCardinalityOfFields",
"GetTimeFieldRange"
]
}
49 changes: 0 additions & 49 deletions x-pack/legacy/plugins/ml/server/routes/fields_service.js

This file was deleted.

86 changes: 86 additions & 0 deletions x-pack/legacy/plugins/ml/server/routes/fields_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { RequestHandlerContext } from 'src/core/server';
import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../new_platform/plugin';
import {
getCardinalityOfFieldsSchema,
getTimeFieldRangeSchema,
} from '../new_platform/fields_service_schema';
import { fieldsServiceProvider } from '../models/fields_service';

function getCardinalityOfFields(context: RequestHandlerContext, payload: any) {
const fs = fieldsServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { index, fieldNames, query, timeFieldName, earliestMs, latestMs } = payload;
return fs.getCardinalityOfFields(index, fieldNames, query, timeFieldName, earliestMs, latestMs);
}

function getTimeFieldRange(context: RequestHandlerContext, payload: any) {
const fs = fieldsServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { index, timeFieldName, query } = payload;
return fs.getTimeFieldRange(index, timeFieldName, query);
}

/**
* Routes for fields service
*/
export function fieldsService({ xpackMainPlugin, router }: RouteInitialization) {
/**
* @apiGroup FieldsService
*
* @api {post} /api/ml/fields_service/field_cardinality Get cardinality of fields
* @apiName GetCardinalityOfFields
* @apiDescription Returns the cardinality of one or more fields. Returns an Object whose keys are the names of the fields, with values equal to the cardinality of the field
*/
router.post(
{
path: '/api/ml/fields_service/field_cardinality',
validate: {
body: getCardinalityOfFieldsSchema,
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const resp = await getCardinalityOfFields(context, request.body);

return response.ok({
body: resp,
});
} catch (e) {
return response.customError(wrapError(e));
}
})
);

/**
* @apiGroup FieldsService
*
* @api {post} /api/ml/fields_service/time_field_range Get time field range
* @apiName GetTimeFieldRange
* @apiDescription Returns the timefield range for the given index
*/
router.post(
{
path: '/api/ml/fields_service/time_field_range',
validate: {
body: getTimeFieldRangeSchema,
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const resp = await getTimeFieldRange(context, request.body);

return response.ok({
body: resp,
});
} catch (e) {
return response.customError(wrapError(e));
}
})
);
}