Skip to content

Commit

Permalink
[ML] New Platform server shim: update fields service routes (#58060) (#…
Browse files Browse the repository at this point in the history
…58222)

* update fieldsService routes to use NP router

* fix file name typo

* add routes names for docs
  • Loading branch information
alvarezmelissa87 authored Feb 21, 2020
1 parent d07f36a commit 1fbd460
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 54 deletions.
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));
}
})
);
}

0 comments on commit 1fbd460

Please sign in to comment.