Skip to content

Commit

Permalink
Reduce ML main bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Aug 3, 2020
1 parent 447db18 commit 79deeb7
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import { SwimlaneType } from '../../application/explorer/explorer_constants';
import { MlDependencies } from '../../application/app';
import { AppStateSelectedCells } from '../../application/explorer/explorer_utils';
import { SWIM_LANE_SELECTION_TRIGGER } from '../../ui_actions/triggers';

export const ANOMALY_SWIMLANE_EMBEDDABLE_TYPE = 'ml_anomaly_swimlane';
import { ANOMALY_SWIMLANE_EMBEDDABLE_TYPE } from './constants';

export const getDefaultPanelTitle = (jobIds: JobId[]) =>
i18n.translate('xpack.ml.swimlaneEmbeddable.title', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@

import { i18n } from '@kbn/i18n';

import { StartServicesAccessor } from 'kibana/public';
import type { StartServicesAccessor } from 'kibana/public';

import {
import type {
EmbeddableFactoryDefinition,
ErrorEmbeddable,
IContainer,
} from '../../../../../../src/plugins/embeddable/public';
import {
ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
AnomalySwimlaneEmbeddable,
import type {
AnomalySwimlaneEmbeddableInput,
AnomalySwimlaneEmbeddableServices,
} from './anomaly_swimlane_embeddable';
import { HttpService } from '../../application/services/http_service';
import { AnomalyDetectorService } from '../../application/services/anomaly_detector_service';
import { AnomalyTimelineService } from '../../application/services/anomaly_timeline_service';
import { mlResultsServiceProvider } from '../../application/services/results_service';
import { resolveAnomalySwimlaneUserInput } from './anomaly_swimlane_setup_flyout';
import { mlApiServicesProvider } from '../../application/services/ml_api_service';
import { MlPluginStart, MlStartDependencies } from '../../plugin';
import { MlDependencies } from '../../application/app';
import type { MlPluginStart, MlStartDependencies } from '../../plugin';
import type { MlDependencies } from '../../application/app';
import { ANOMALY_SWIMLANE_EMBEDDABLE_TYPE } from './constants';

export class AnomalySwimlaneEmbeddableFactory
implements EmbeddableFactoryDefinition<AnomalySwimlaneEmbeddableInput> {
Expand All @@ -50,6 +42,7 @@ export class AnomalySwimlaneEmbeddableFactory
const [coreStart] = await this.getServices();

try {
const { resolveAnomalySwimlaneUserInput } = await import('./anomaly_swimlane_setup_flyout');
return await resolveAnomalySwimlaneUserInput(coreStart);
} catch (e) {
return Promise.reject();
Expand All @@ -59,6 +52,14 @@ export class AnomalySwimlaneEmbeddableFactory
private async getServices(): Promise<AnomalySwimlaneEmbeddableServices> {
const [coreStart, pluginsStart] = await this.getStartServices();

const {
HttpService,
AnomalyDetectorService,
AnomalyTimelineService,
mlResultsServiceProvider,
mlApiServicesProvider,
} = await import('./services');

const httpService = new HttpService(coreStart.http);
const anomalyDetectorService = new AnomalyDetectorService(httpService);
const anomalyTimelineService = new AnomalyTimelineService(
Expand All @@ -74,11 +75,9 @@ export class AnomalySwimlaneEmbeddableFactory
];
}

public async create(
initialInput: AnomalySwimlaneEmbeddableInput,
parent?: IContainer
): Promise<AnomalySwimlaneEmbeddable | ErrorEmbeddable> {
public async create(initialInput: AnomalySwimlaneEmbeddableInput, parent?: IContainer) {
const services = await this.getServices();
const { AnomalySwimlaneEmbeddable } = await import('./anomaly_swimlane_embeddable');
return new AnomalySwimlaneEmbeddable(initialInput, services, parent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export const ANOMALY_SWIMLANE_EMBEDDABLE_TYPE = 'ml_anomaly_swimlane';
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
*/

export { AnomalySwimlaneEmbeddableFactory } from './anomaly_swimlane_embeddable_factory';
export { ANOMALY_SWIMLANE_EMBEDDABLE_TYPE } from './anomaly_swimlane_embeddable';
export { ANOMALY_SWIMLANE_EMBEDDABLE_TYPE } from './constants';
export type {
EditSwimlanePanelContext,
SwimLaneDrilldownContext,
} from './anomaly_swimlane_embeddable';
11 changes: 11 additions & 0 deletions x-pack/plugins/ml/public/embeddables/anomaly_swimlane/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

export { HttpService } from '../../application/services/http_service';
export { AnomalyDetectorService } from '../../application/services/anomaly_detector_service';
export { AnomalyTimelineService } from '../../application/services/anomaly_timeline_service';
export { mlResultsServiceProvider } from '../../application/services/results_service';
export { mlApiServicesProvider } from '../../application/services/ml_api_service';
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/public/embeddables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import { AnomalySwimlaneEmbeddableFactory } from './anomaly_swimlane';
import { MlCoreSetup } from '../plugin';
import { EmbeddableSetup } from '../../../../../src/plugins/embeddable/public';
import type { MlCoreSetup } from '../plugin';
import type { EmbeddableSetup } from '../../../../../src/plugins/embeddable/public';

export function registerEmbeddables(embeddable: EmbeddableSetup, core: MlCoreSetup) {
const anomalySwimlaneEmbeddableFactory = new AnomalySwimlaneEmbeddableFactory(
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import { PLUGIN_ID, PLUGIN_ICON } from '../common/constants/app';
import { registerFeature } from './register_feature';
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public';
import { registerEmbeddables } from './embeddables';
import { UiActionsSetup, UiActionsStart } from '../../../../src/plugins/ui_actions/public';
import type { UiActionsSetup, UiActionsStart } from '../../../../src/plugins/ui_actions/public';
import type { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public';
import { registerMlUiActions } from './ui_actions';
import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public';
import { registerUrlGenerator, MlUrlGeneratorState, ML_APP_URL_GENERATOR } from './url_generator';
import { isMlEnabled, isFullLicense } from '../common/license';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import { i18n } from '@kbn/i18n';
import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public';
import {
AnomalySwimlaneEmbeddable,
ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
SwimLaneDrilldownContext,
} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable';
} from '../embeddables/anomaly_swimlane';
import { MlCoreSetup } from '../plugin';
import { SWIMLANE_TYPE, VIEW_BY_JOB_LABEL } from '../application/explorer/explorer_constants';
import { Filter, FilterStateStore } from '../../../../../src/plugins/data/common';
Expand Down Expand Up @@ -73,7 +73,7 @@ export function createApplyInfluencerFiltersAction(
async isCompatible({ embeddable, data }: SwimLaneDrilldownContext) {
// Only compatible with view by influencer swim lanes and single selection
return (
embeddable instanceof AnomalySwimlaneEmbeddable &&
embeddable.type === ANOMALY_SWIMLANE_EMBEDDABLE_TYPE &&
data !== undefined &&
data.type === SWIMLANE_TYPE.VIEW_BY &&
data.viewByFieldName !== VIEW_BY_JOB_LABEL &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { i18n } from '@kbn/i18n';
import moment from 'moment';
import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public';
import {
AnomalySwimlaneEmbeddable,
ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
SwimLaneDrilldownContext,
} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable';
} from '../embeddables/anomaly_swimlane';
import { MlCoreSetup } from '../plugin';

export const APPLY_TIME_RANGE_SELECTION_ACTION = 'applyTimeRangeSelectionAction';
Expand Down Expand Up @@ -52,7 +52,7 @@ export function createApplyTimeRangeSelectionAction(
});
},
async isCompatible({ embeddable, data }: SwimLaneDrilldownContext) {
return embeddable instanceof AnomalySwimlaneEmbeddable && data !== undefined;
return embeddable.type === ANOMALY_SWIMLANE_EMBEDDABLE_TYPE && data !== undefined;
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import { i18n } from '@kbn/i18n';
import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public';
import {
AnomalySwimlaneEmbeddable,
ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
EditSwimlanePanelContext,
} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable';
import { resolveAnomalySwimlaneUserInput } from '../embeddables/anomaly_swimlane/anomaly_swimlane_setup_flyout';
} from '../embeddables/anomaly_swimlane';
import { ViewMode } from '../../../../../src/plugins/embeddable/public';
import { MlCoreSetup } from '../plugin';

Expand All @@ -35,6 +34,9 @@ export function createEditSwimlanePanelAction(getStartServices: MlCoreSetup['get
const [coreStart] = await getStartServices();

try {
const { resolveAnomalySwimlaneUserInput } = await import(
'../embeddables/anomaly_swimlane/anomaly_swimlane_setup_flyout'
);
const result = await resolveAnomalySwimlaneUserInput(coreStart, embeddable.getInput());
embeddable.updateInput(result);
} catch (e) {
Expand All @@ -43,7 +45,7 @@ export function createEditSwimlanePanelAction(getStartServices: MlCoreSetup['get
},
isCompatible: async ({ embeddable }: EditSwimlanePanelContext) => {
return (
embeddable instanceof AnomalySwimlaneEmbeddable &&
embeddable.type === ANOMALY_SWIMLANE_EMBEDDABLE_TYPE &&
embeddable.getInput().viewMode === ViewMode.EDIT
);
},
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/ml/public/ui_actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import {
createOpenInExplorerAction,
OPEN_IN_ANOMALY_EXPLORER_ACTION,
} from './open_in_anomaly_explorer_action';
import { EditSwimlanePanelContext } from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable';
import { UiActionsSetup } from '../../../../../src/plugins/ui_actions/public';
import { MlPluginStart, MlStartDependencies } from '../plugin';
import type { EditSwimlanePanelContext } from '../embeddables/anomaly_swimlane';
import type { UiActionsSetup } from '../../../../../src/plugins/ui_actions/public';
import type { MlPluginStart, MlStartDependencies } from '../plugin';
import { CONTEXT_MENU_TRIGGER } from '../../../../../src/plugins/embeddable/public';
import {
APPLY_INFLUENCER_FILTERS_ACTION,
createApplyInfluencerFiltersAction,
} from './apply_influencer_filters_action';
import { SWIM_LANE_SELECTION_TRIGGER, swimLaneSelectionTrigger } from './triggers';
import { SwimLaneDrilldownContext } from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable';
import { SwimLaneDrilldownContext } from '../embeddables/anomaly_swimlane';
import {
APPLY_TIME_RANGE_SELECTION_ACTION,
createApplyTimeRangeSelectionAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import { i18n } from '@kbn/i18n';
import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public';
import {
AnomalySwimlaneEmbeddable,
ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
SwimLaneDrilldownContext,
} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable';
} from '../embeddables/anomaly_swimlane';
import { MlCoreSetup } from '../plugin';
import { ML_APP_URL_GENERATOR } from '../url_generator';

Expand Down Expand Up @@ -60,7 +60,7 @@ export function createOpenInExplorerAction(getStartServices: MlCoreSetup['getSta
await application.navigateToUrl(anomalyExplorerUrl!);
},
async isCompatible({ embeddable }: SwimLaneDrilldownContext) {
return embeddable instanceof AnomalySwimlaneEmbeddable;
return embeddable.type === ANOMALY_SWIMLANE_EMBEDDABLE_TYPE;
},
});
}

0 comments on commit 79deeb7

Please sign in to comment.