[ML] New Platform server shim: update indices routes (#57685) (#57778)

* [ML] NP indices routes

* [ML] fix error function

* [ML] fix createAndOpenUrl function

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Dima Arnautov 2020-02-21 11:27:36 +01:00 committed by GitHub
parent e0ccf0d3e4
commit ac0166064d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 63 deletions

View file

@ -236,26 +236,26 @@ class LinksMenuUI extends Component {
let i = 0;
findFieldType(datafeedIndices[i]);
function findFieldType(index) {
getFieldTypeFromMapping(index, categorizationFieldName)
.then(resp => {
if (resp !== '') {
createAndOpenUrl(index, resp);
} else {
i++;
if (i < datafeedIndices.length) {
findFieldType(datafeedIndices[i]);
} else {
error();
}
}
const error = () => {
console.log(
`viewExamples(): error finding type of field ${categorizationFieldName} in indices:`,
datafeedIndices
);
const { toasts } = this.props.kibana.services.notifications;
toasts.addDanger(
i18n.translate('xpack.ml.anomaliesTable.linksMenu.noMappingCouldBeFoundErrorMessage', {
defaultMessage:
'Unable to view examples of documents with mlcategory {categoryId} ' +
'as no mapping could be found for the categorization field {categorizationFieldName}',
values: {
categoryId,
categorizationFieldName,
},
})
.catch(() => {
error();
});
}
);
};
function createAndOpenUrl(index, categorizationFieldType) {
const createAndOpenUrl = (index, categorizationFieldType) => {
// Find the ID of the index pattern with a title attribute which matches the
// index configured in the datafeed. If a Kibana index pattern has not been created
// for this index, then the user will see a warning message on the Discover tab advising
@ -340,25 +340,25 @@ class LinksMenuUI extends Component {
})
);
});
}
};
function error() {
console.log(
`viewExamples(): error finding type of field ${categorizationFieldName} in indices:`,
datafeedIndices
);
const { toasts } = this.props.kibana.services.notifications;
toasts.addDanger(
i18n.translate('xpack.ml.anomaliesTable.linksMenu.noMappingCouldBeFoundErrorMessage', {
defaultMessage:
'Unable to view examples of documents with mlcategory {categoryId} ' +
'as no mapping could be found for the categorization field {categorizationFieldName}',
values: {
categoryId,
categorizationFieldName,
},
function findFieldType(index) {
getFieldTypeFromMapping(index, categorizationFieldName)
.then(resp => {
if (resp !== '') {
createAndOpenUrl(index, resp);
} else {
i++;
if (i < datafeedIndices.length) {
findFieldType(datafeedIndices[i]);
} else {
error();
}
}
})
);
.catch(() => {
error();
});
}
};

View file

@ -1,28 +0,0 @@
/*
* 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 { callWithRequestFactory } from '../client/call_with_request_factory';
import { wrapError } from '../client/errors';
export function indicesRoutes({ commonRouteConfig, elasticsearchPlugin, route }) {
route({
method: 'POST',
path: '/api/ml/indices/field_caps',
handler(request) {
const callWithRequest = callWithRequestFactory(elasticsearchPlugin, request);
const index = request.payload.index;
let fields = '*';
if (request.payload.fields !== undefined && Array.isArray(request.payload.fields)) {
fields = request.payload.fields.join(',');
}
return callWithRequest('fieldCaps', { index, fields }).catch(resp => wrapError(resp));
},
config: {
...commonRouteConfig,
},
});
}

View file

@ -0,0 +1,49 @@
/*
* 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';
import { wrapError } from '../client/error_wrapper';
import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
import { RouteInitialization } from '../new_platform/plugin';
/**
* Indices routes.
*/
export function indicesRoutes({ xpackMainPlugin, router }: RouteInitialization) {
/**
* @apiGroup Indices
*
* @api {post} /api/ml/indices/field_caps
* @apiName FieldCaps
* @apiDescription Retrieves the capabilities of fields among multiple indices.
*/
router.post(
{
path: '/api/ml/indices/field_caps',
validate: {
body: schema.object({
index: schema.maybe(schema.string()),
fields: schema.maybe(schema.arrayOf(schema.string())),
}),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const {
body: { index, fields: requestFields },
} = request;
const fields =
requestFields !== undefined && Array.isArray(requestFields)
? requestFields.join(',')
: '*';
const result = await context.ml!.mlClient.callAsCurrentUser('fieldCaps', { index, fields });
return response.ok({ body: result });
} catch (e) {
return response.customError(wrapError(e));
}
})
);
}