Skip to content

Commit 1fbd460

Browse files
[ML] New Platform server shim: update fields service routes (#58060) (#58222)
* update fieldsService routes to use NP router * fix file name typo * add routes names for docs
1 parent d07f36a commit 1fbd460

File tree

7 files changed

+137
-54
lines changed

7 files changed

+137
-54
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { APICaller } from 'src/core/server';
8+
9+
export function fieldsServiceProvider(
10+
callAsCurrentUser: APICaller
11+
): {
12+
getCardinalityOfFields: (
13+
index: string[] | string,
14+
fieldNames: string[],
15+
query: any,
16+
timeFieldName: string,
17+
earliestMs: number,
18+
latestMs: number
19+
) => Promise<any>;
20+
getTimeFieldRange: (index: string[] | string, timeFieldName: string, query: any) => Promise<any>;
21+
};

x-pack/legacy/plugins/ml/server/models/fields_service/fields_service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Service for carrying out queries to obtain data
88
// specific to fields in Elasticsearch indices.
99

10-
export function fieldsServiceProvider(callWithRequest) {
10+
export function fieldsServiceProvider(callAsCurrentUser) {
1111
// Obtains the cardinality of one or more fields.
1212
// Returns an Object whose keys are the names of the fields,
1313
// with values equal to the cardinality of the field.
@@ -17,7 +17,7 @@ export function fieldsServiceProvider(callWithRequest) {
1717
// First check that each of the supplied fieldNames are aggregatable,
1818
// then obtain the cardinality for each of the aggregatable fields.
1919
return new Promise((resolve, reject) => {
20-
callWithRequest('fieldCaps', {
20+
callAsCurrentUser('fieldCaps', {
2121
index,
2222
fields: fieldNames,
2323
})
@@ -72,7 +72,7 @@ export function fieldsServiceProvider(callWithRequest) {
7272
aggs,
7373
};
7474

75-
callWithRequest('search', {
75+
callAsCurrentUser('search', {
7676
index,
7777
body,
7878
})
@@ -106,7 +106,7 @@ export function fieldsServiceProvider(callWithRequest) {
106106
return new Promise((resolve, reject) => {
107107
const obj = { success: true, start: { epoch: 0, string: '' }, end: { epoch: 0, string: '' } };
108108

109-
callWithRequest('search', {
109+
callAsCurrentUser('search', {
110110
index,
111111
size: 0,
112112
body: {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { schema } from '@kbn/config-schema';
8+
9+
export const getCardinalityOfFieldsSchema = schema.object({
10+
index: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
11+
fieldNames: schema.maybe(schema.arrayOf(schema.string())),
12+
query: schema.maybe(schema.any()),
13+
timeFieldName: schema.maybe(schema.string()),
14+
earliestMs: schema.maybe(schema.oneOf([schema.number(), schema.string()])),
15+
latestMs: schema.maybe(schema.oneOf([schema.number(), schema.string()])),
16+
});
17+
18+
export const getTimeFieldRangeSchema = schema.object({
19+
index: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]),
20+
timeFieldName: schema.maybe(schema.string()),
21+
query: schema.maybe(schema.any()),
22+
});

x-pack/legacy/plugins/ml/server/routes/apidoc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
"DeleteDatafeed",
106106
"StartDatafeed",
107107
"StopDatafeed",
108-
"PreviewDatafeed"
108+
"PreviewDatafeed",
109+
"FieldsService",
110+
"GetCardinalityOfFields",
111+
"GetTimeFieldRange"
109112
]
110113
}

x-pack/legacy/plugins/ml/server/routes/fields_service.js

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { RequestHandlerContext } from 'src/core/server';
8+
import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
9+
import { wrapError } from '../client/error_wrapper';
10+
import { RouteInitialization } from '../new_platform/plugin';
11+
import {
12+
getCardinalityOfFieldsSchema,
13+
getTimeFieldRangeSchema,
14+
} from '../new_platform/fields_service_schema';
15+
import { fieldsServiceProvider } from '../models/fields_service';
16+
17+
function getCardinalityOfFields(context: RequestHandlerContext, payload: any) {
18+
const fs = fieldsServiceProvider(context.ml!.mlClient.callAsCurrentUser);
19+
const { index, fieldNames, query, timeFieldName, earliestMs, latestMs } = payload;
20+
return fs.getCardinalityOfFields(index, fieldNames, query, timeFieldName, earliestMs, latestMs);
21+
}
22+
23+
function getTimeFieldRange(context: RequestHandlerContext, payload: any) {
24+
const fs = fieldsServiceProvider(context.ml!.mlClient.callAsCurrentUser);
25+
const { index, timeFieldName, query } = payload;
26+
return fs.getTimeFieldRange(index, timeFieldName, query);
27+
}
28+
29+
/**
30+
* Routes for fields service
31+
*/
32+
export function fieldsService({ xpackMainPlugin, router }: RouteInitialization) {
33+
/**
34+
* @apiGroup FieldsService
35+
*
36+
* @api {post} /api/ml/fields_service/field_cardinality Get cardinality of fields
37+
* @apiName GetCardinalityOfFields
38+
* @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
39+
*/
40+
router.post(
41+
{
42+
path: '/api/ml/fields_service/field_cardinality',
43+
validate: {
44+
body: getCardinalityOfFieldsSchema,
45+
},
46+
},
47+
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
48+
try {
49+
const resp = await getCardinalityOfFields(context, request.body);
50+
51+
return response.ok({
52+
body: resp,
53+
});
54+
} catch (e) {
55+
return response.customError(wrapError(e));
56+
}
57+
})
58+
);
59+
60+
/**
61+
* @apiGroup FieldsService
62+
*
63+
* @api {post} /api/ml/fields_service/time_field_range Get time field range
64+
* @apiName GetTimeFieldRange
65+
* @apiDescription Returns the timefield range for the given index
66+
*/
67+
router.post(
68+
{
69+
path: '/api/ml/fields_service/time_field_range',
70+
validate: {
71+
body: getTimeFieldRangeSchema,
72+
},
73+
},
74+
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
75+
try {
76+
const resp = await getTimeFieldRange(context, request.body);
77+
78+
return response.ok({
79+
body: resp,
80+
});
81+
} catch (e) {
82+
return response.customError(wrapError(e));
83+
}
84+
})
85+
);
86+
}

0 commit comments

Comments
 (0)