Skip to content

Commit

Permalink
[ML] Fixes unnecessary ML services initialization during plugin setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Sep 17, 2024
1 parent 2b12950 commit 6ebe7e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
11 changes: 2 additions & 9 deletions x-pack/plugins/ml/public/cases/register_cases_attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@ import type { CoreStart } from '@kbn/core/public';
import { registerAnomalyChartsCasesAttachment } from './register_anomaly_charts_attachment';
import { registerSingleMetricViewerCasesAttachment } from './register_single_metric_viewer_attachment';
import type { MlStartDependencies } from '../plugin';
import type { SingleMetricViewerServices } from '../embeddables/types';
import { registerAnomalySwimLaneCasesAttachment } from './register_anomaly_swim_lane_attachment';

export function registerCasesAttachments(
cases: CasesPublicSetup,
coreStart: CoreStart,
pluginStart: MlStartDependencies,
singleMetricViewerServices: SingleMetricViewerServices
pluginStart: MlStartDependencies
) {
registerAnomalySwimLaneCasesAttachment(cases, pluginStart);
registerAnomalyChartsCasesAttachment(cases, coreStart, pluginStart);
registerSingleMetricViewerCasesAttachment(
cases,
coreStart,
pluginStart,
singleMetricViewerServices
);
registerSingleMetricViewerCasesAttachment(cases, coreStart, pluginStart);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ import { PLUGIN_ICON } from '../../common/constants/app';
import { CASE_ATTACHMENT_TYPE_ID_SINGLE_METRIC_VIEWER } from '../../common/constants/cases';
import type { MlStartDependencies } from '../plugin';
import { getSingleMetricViewerComponent } from '../shared_components/single_metric_viewer';
import type { SingleMetricViewerServices } from '../embeddables/types';
import type { MlDependencies } from '../application/app';
import { getMlServices } from '../embeddables/single_metric_viewer/get_services';

export function registerSingleMetricViewerCasesAttachment(
cases: CasesPublicSetup,
coreStart: CoreStart,
pluginStart: MlStartDependencies,
mlServices: SingleMetricViewerServices
pluginStart: MlStartDependencies
) {
const SingleMetricViewerComponent = getSingleMetricViewerComponent(
coreStart,
pluginStart as MlDependencies,
mlServices
);

cases.attachmentFramework.registerPersistableState({
id: CASE_ATTACHMENT_TYPE_ID_SINGLE_METRIC_VIEWER,
icon: PLUGIN_ICON,
Expand All @@ -44,7 +37,15 @@ export function registerSingleMetricViewerCasesAttachment(
),
timelineAvatar: PLUGIN_ICON,
children: React.lazy(async () => {
const { initComponent } = await import('./single_metric_viewer_attachment');
const [{ initComponent }, mlServices] = await Promise.all([
import('./single_metric_viewer_attachment'),
getMlServices(coreStart, pluginStart),
]);
const SingleMetricViewerComponent = getSingleMetricViewerComponent(
coreStart,
pluginStart as MlDependencies,
mlServices
);
return {
default: initComponent(pluginStart.fieldFormats, SingleMetricViewerComponent),
};
Expand Down
4 changes: 1 addition & 3 deletions x-pack/plugins/ml/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import type { ElasticModels } from './application/services/elastic_models_servic
import type { MlApi } from './application/services/ml_api_service';
import type { MlCapabilities } from '../common/types/capabilities';
import { AnomalySwimLane } from './shared_components';
import { getMlServices } from './embeddables/single_metric_viewer/get_services';

export interface MlStartDependencies {
cases?: CasesPublicStart;
Expand Down Expand Up @@ -275,8 +274,7 @@ export class MlPlugin implements Plugin<MlPluginSetup, MlPluginStart> {
registerEmbeddables(pluginsSetup.embeddable, core);

if (pluginsSetup.cases) {
const mlServices = await getMlServices(coreStart, pluginStart);
registerCasesAttachments(pluginsSetup.cases, coreStart, pluginStart, mlServices);
registerCasesAttachments(pluginsSetup.cases, coreStart, pluginStart);
}

if (pluginsSetup.maps) {
Expand Down

0 comments on commit 6ebe7e7

Please sign in to comment.