Skip to content

Commit

Permalink
[ML] New Platform server shim: update analytics routes to use new pla…
Browse files Browse the repository at this point in the history
…tform router (#53521) (#53897)

* update dfAnalytics routes to use np router

* add route schemas and only show error message

* convert route file to ts and set handlers inline

* update df analytics param type

* update mlClient type and assert mlClient is not null

* handle errors correctly

* ensure error status gets passed correctly to wrapper
  • Loading branch information
alvarezmelissa87 committed Jan 3, 2020
1 parent 126da30 commit 6380dfc
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 180 deletions.
3 changes: 2 additions & 1 deletion x-pack/legacy/plugins/ml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export const ml = (kibana: any) => {
injectUiAppVars: server.injectUiAppVars,
http: mlHttpService,
savedObjects: server.savedObjects,
elasticsearch: kbnServer.newPlatform.setup.core.elasticsearch, // NP
};
const { usageCollection, cloud, home } = kbnServer.newPlatform.setup.plugins;
const plugins = {
elasticsearch: server.plugins.elasticsearch,
elasticsearch: server.plugins.elasticsearch, // legacy
security: server.plugins.security,
xpackMain: server.plugins.xpack_main,
spaces: server.plugins.spaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const AnalyticsPanel: FC<Props> = ({ jobCreationDisabled }) => {
<EuiLoadingSpinner className="mlOverviewPanel__spinner" size="xl" />
)}

{isInitialized === true && analytics.length === 0 && (
{errorMessage === undefined && isInitialized === true && analytics.length === 0 && (
<EuiEmptyPrompt
iconType="createAdvancedJob"
title={
Expand Down
17 changes: 17 additions & 0 deletions x-pack/legacy/plugins/ml/server/client/error_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { boomify, isBoom } from 'boom';
import { ResponseError, CustomHttpResponseOptions } from 'src/core/server';

export function wrapError(error: any): CustomHttpResponseOptions<ResponseError> {
const boom = isBoom(error) ? error : boomify(error, { statusCode: error.status });
return {
body: boom,
headers: boom.output.headers,
statusCode: boom.output.statusCode,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 dataAnalyticsJobConfigSchema = {
description: schema.maybe(schema.string()),
dest: schema.object({
index: schema.string(),
results_field: schema.maybe(schema.string()),
}),
source: schema.object({
index: schema.string(),
}),
analysis: schema.any(),
analyzed_fields: schema.any(),
model_memory_limit: schema.string(),
};

export const dataAnalyticsEvaluateSchema = {
index: schema.string(),
query: schema.maybe(schema.any()),
evaluation: schema.maybe(
schema.object({
regression: schema.maybe(schema.any()),
classification: schema.maybe(schema.any()),
})
),
};

export const dataAnalyticsExplainSchema = {
description: schema.maybe(schema.string()),
dest: schema.maybe(schema.any()),
source: schema.object({
index: schema.string(),
}),
analysis: schema.any(),
analyzed_fields: schema.maybe(schema.any()),
model_memory_limit: schema.maybe(schema.string()),
};
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;
* you may not use this file except in compliance with the Elastic License.
*/

import {
KibanaRequest,
KibanaResponseFactory,
RequestHandler,
RequestHandlerContext,
} from 'src/core/server';
import { PLUGIN_ID, MlXpackMainPlugin } from './plugin';

export const licensePreRoutingFactory = (
xpackMainPlugin: MlXpackMainPlugin,
handler: RequestHandler<any, any, any>
): RequestHandler<any, any, any> => {
// License checking and enable/disable logic
return function licensePreRouting(
ctx: RequestHandlerContext,
request: KibanaRequest,
response: KibanaResponseFactory
) {
const licenseCheckResults = xpackMainPlugin.info.feature(PLUGIN_ID).getLicenseCheckResults();

if (!licenseCheckResults.isAvailable) {
return response.forbidden({
body: {
message: licenseCheckResults.message,
},
});
}

return handler(ctx, request, response);
};
};
41 changes: 37 additions & 4 deletions x-pack/legacy/plugins/ml/server/new_platform/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import Boom from 'boom';
import { i18n } from '@kbn/i18n';
import { ServerRoute } from 'hapi';
import { KibanaConfig, SavedObjectsLegacyService } from 'src/legacy/server/kbn_server';
import { Logger, PluginInitializerContext, CoreSetup } from 'src/core/server';
import {
Logger,
PluginInitializerContext,
CoreSetup,
IRouter,
IScopedClusterClient,
} from 'src/core/server';
import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { ElasticsearchServiceSetup } from 'src/core/server';
import { CloudSetup } from '../../../../../plugins/cloud/server';
import { XPackMainPlugin } from '../../../xpack_main/server/xpack_main';
import { addLinksToSampleDatasets } from '../lib/sample_data_sets';
Expand Down Expand Up @@ -56,6 +63,10 @@ import { jobAuditMessagesRoutes } from '../routes/job_audit_messages';
import { fileDataVisualizerRoutes } from '../routes/file_data_visualizer';
import { initMlServerLog, LogInitialization } from '../client/log';
import { HomeServerPluginSetup } from '../../../../../../src/plugins/home/server';
// @ts-ignore: could not find declaration file for module
import { elasticsearchJsPlugin } from '../client/elasticsearch_ml';

export const PLUGIN_ID = 'ml';

type CoreHttpSetup = CoreSetup['http'];
export interface MlHttpServiceSetup extends CoreHttpSetup {
Expand All @@ -70,6 +81,7 @@ export interface MlCoreSetup {
injectUiAppVars: (id: string, callback: () => {}) => any;
http: MlHttpServiceSetup;
savedObjects: SavedObjectsLegacyService;
elasticsearch: ElasticsearchServiceSetup;
}
export interface MlInitializerContext extends PluginInitializerContext {
legacyConfig: KibanaConfig;
Expand All @@ -86,12 +98,15 @@ export interface PluginsSetup {
// TODO: this is temporary for `mirrorPluginStatus`
ml: any;
}

export interface RouteInitialization {
commonRouteConfig: any;
config?: any;
elasticsearchPlugin: ElasticsearchPlugin;
elasticsearchService: ElasticsearchServiceSetup;
route(route: ServerRoute | ServerRoute[]): void;
xpackMainPlugin?: MlXpackMainPlugin;
router: IRouter;
xpackMainPlugin: MlXpackMainPlugin;
savedObjects?: SavedObjectsLegacyService;
spacesPlugin: any;
cloud?: CloudSetup;
Expand All @@ -101,8 +116,16 @@ export interface UsageInitialization {
savedObjects: SavedObjectsLegacyService;
}

declare module 'kibana/server' {
interface RequestHandlerContext {
ml?: {
mlClient: IScopedClusterClient;
};
}
}

export class Plugin {
private readonly pluginId: string = 'ml';
private readonly pluginId: string = PLUGIN_ID;
private config: any;
private log: Logger;

Expand Down Expand Up @@ -183,17 +206,27 @@ export class Plugin {
};
});

// Can access via new platform router's handler function 'context' parameter - context.ml.mlClient
const mlClient = core.elasticsearch.createClient('ml', { plugins: [elasticsearchJsPlugin] });
http.registerRouteHandlerContext('ml', (context, request) => {
return {
mlClient: mlClient.asScoped(request),
};
});

const routeInitializationDeps: RouteInitialization = {
commonRouteConfig,
route: http.route,
router: http.createRouter(),
elasticsearchPlugin: plugins.elasticsearch,
elasticsearchService: core.elasticsearch,
xpackMainPlugin: plugins.xpackMain,
spacesPlugin: plugins.spaces,
};

const extendedRouteInitializationDeps: RouteInitialization = {
...routeInitializationDeps,
config: this.config,
xpackMainPlugin: plugins.xpackMain,
savedObjects: core.savedObjects,
spacesPlugin: plugins.spaces,
cloud: plugins.cloud,
Expand Down
174 changes: 0 additions & 174 deletions x-pack/legacy/plugins/ml/server/routes/data_frame_analytics.js

This file was deleted.

Loading

0 comments on commit 6380dfc

Please sign in to comment.