Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions x-pack/platform/plugins/shared/aiops/public/capabilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { CoreStart } from '@kbn/core/public';

export const canUseAiops = (coreStart: CoreStart, throwError = false) => {
const canUse =
coreStart.application.capabilities.ml.canUseAiops === true &&
coreStart.application.capabilities.aiops.enabled === true;
if (throwError && !canUse) {
throw new Error('AIOps is not enabled');
}
return canUse;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

import { i18n } from '@kbn/i18n';
import { createAction } from '@kbn/ui-actions-plugin/public';
import type { CoreStart } from '@kbn/core/public';
import { ACTION_CATEGORIZE_FIELD, type CategorizeFieldContext } from '@kbn/ml-ui-actions';
import type { AiopsPluginStartDeps } from '../../types';
import type { AiopsCoreSetup } from '../../types';

export const createCategorizeFieldAction = (coreStart: CoreStart, plugins: AiopsPluginStartDeps) =>
export const createCategorizeFieldAction = (getStartServices: AiopsCoreSetup['getStartServices']) =>
createAction<CategorizeFieldContext>({
type: ACTION_CATEGORIZE_FIELD,
id: ACTION_CATEGORIZE_FIELD,
Expand All @@ -24,12 +23,15 @@ export const createCategorizeFieldAction = (coreStart: CoreStart, plugins: Aiops
},
execute: async (context: CategorizeFieldContext) => {
const { field, dataView, originatingApp, additionalFilter, focusTrapProps } = context;
const { showCategorizeFlyout } = await import('./show_flyout');
const [[coreStart, pluginStart], { showCategorizeFlyout }] = await Promise.all([
getStartServices(),
import('./show_flyout'),
]);
showCategorizeFlyout(
field,
dataView,
coreStart,
plugins,
pluginStart,
originatingApp,
additionalFilter,
focusTrapProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from './initialize_change_point_controls';
import type { ChangePointEmbeddableApi } from './types';
import type { ChangePointEmbeddableState } from '../../../common/embeddables/change_point_chart/types';
import { canUseAiops } from '../../capabilities';

export type EmbeddableChangePointChartType = typeof EMBEDDABLE_CHANGE_POINT_CHART_TYPE;

Expand All @@ -45,6 +46,7 @@ export const getChangePointChartEmbeddableFactory = (
type: EMBEDDABLE_CHANGE_POINT_CHART_TYPE,
buildEmbeddable: async ({ initialState, finalizeApi, uuid, parentApi }) => {
const [coreStart, pluginStart] = await getStartServices();
canUseAiops(coreStart, true);

const timeRangeManager = initializeTimeRangeManager(initialState);
const titleManager = initializeTitleManager(initialState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,36 @@
* 2.0.
*/

import type { CoreSetup } from '@kbn/core-lifecycle-browser';
import type { EmbeddableSetup } from '@kbn/embeddable-plugin/public';
import { EMBEDDABLE_CHANGE_POINT_CHART_TYPE } from '@kbn/aiops-change-point-detection/constants';
import { EMBEDDABLE_PATTERN_ANALYSIS_TYPE } from '@kbn/aiops-log-pattern-analysis/constants';
import { EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE } from '@kbn/aiops-log-rate-analysis/constants';
import type { Reference } from '@kbn/content-management-utils';
import type { AiopsPluginStart, AiopsPluginStartDeps } from '../types';
import type { AiopsCoreSetup } from '../types';

export const registerEmbeddables = (
embeddable: EmbeddableSetup,
core: CoreSetup<AiopsPluginStartDeps, AiopsPluginStart>
getStartServices: AiopsCoreSetup['getStartServices']
) => {
embeddable.registerEmbeddablePublicDefinition(EMBEDDABLE_CHANGE_POINT_CHART_TYPE, async () => {
const { getChangePointChartEmbeddableFactory } = await import('./change_point_chart');
return getChangePointChartEmbeddableFactory(core.getStartServices);
return getChangePointChartEmbeddableFactory(getStartServices);
});
embeddable.registerLegacyURLTransform(EMBEDDABLE_CHANGE_POINT_CHART_TYPE, async () => {
const { transformOut } = await import('./change_point_chart');
return transformOut as (storedState: object, references?: Reference[]) => object;
});
embeddable.registerEmbeddablePublicDefinition(EMBEDDABLE_PATTERN_ANALYSIS_TYPE, async () => {
const { getPatternAnalysisEmbeddableFactory } = await import('./pattern_analysis');
return getPatternAnalysisEmbeddableFactory(core.getStartServices);
return getPatternAnalysisEmbeddableFactory(getStartServices);
});
embeddable.registerLegacyURLTransform(EMBEDDABLE_PATTERN_ANALYSIS_TYPE, async () => {
const { transformOut } = await import('./pattern_analysis');
return transformOut as (storedState: object, references?: Reference[]) => object;
});
embeddable.registerEmbeddablePublicDefinition(EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE, async () => {
const { getLogRateAnalysisEmbeddableFactory } = await import('./log_rate_analysis');
return getLogRateAnalysisEmbeddableFactory(core.getStartServices);
return getLogRateAnalysisEmbeddableFactory(getStartServices);
});
embeddable.registerLegacyURLTransform(EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE, async () => {
const { transformOut } = await import('./log_rate_analysis');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { initializeLogRateAnalysisControls } from './initialize_log_rate_analysi
import type { LogRateAnalysisEmbeddableApi } from './types';
import { EmbeddableLogRateAnalysisUserInput } from './log_rate_analysis_config_input';
import type { LogRateAnalysisEmbeddableState } from '../../../common/embeddables/log_rate_analysis/types';
import { canUseAiops } from '../../capabilities';

export type EmbeddableLogRateAnalysisType = typeof EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE;

Expand All @@ -44,6 +45,7 @@ export const getLogRateAnalysisEmbeddableFactory = (
type: EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE,
buildEmbeddable: async ({ initialState, finalizeApi, uuid, parentApi }) => {
const [coreStart, pluginStart] = await getStartServices();
canUseAiops(coreStart, true);
const runtimeState = initialState;
const timeRangeManager = initializeTimeRangeManager(initialState);
const titleManager = initializeTitleManager(initialState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type { AiopsPluginStart, AiopsPluginStartDeps } from '../../types';
import { initializePatternAnalysisControls } from './initialize_pattern_analysis_controls';
import type { PatternAnalysisEmbeddableApi } from './types';
import type { PatternAnalysisEmbeddableState } from '../../../common/embeddables/pattern_analysis/types';
import { canUseAiops } from '../../capabilities';

export type EmbeddablePatternAnalysisType = typeof EMBEDDABLE_PATTERN_ANALYSIS_TYPE;

Expand All @@ -40,6 +41,7 @@ export const getPatternAnalysisEmbeddableFactory = (
type: EMBEDDABLE_PATTERN_ANALYSIS_TYPE,
buildEmbeddable: async ({ initialState, finalizeApi, uuid, parentApi }) => {
const [coreStart, pluginStart] = await getStartServices();
canUseAiops(coreStart, true);
const runtimeState = initialState;
const timeRangeManager = initializeTimeRangeManager(initialState);
const titleManager = initializeTitleManager(initialState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import type { CoreStart, Plugin } from '@kbn/core/public';
import { type CoreSetup } from '@kbn/core/public';

import { getChangePointDetectionComponent } from './shared_components';
import { LogCategorizationForDiscover as PatternAnalysisComponent } from './shared_lazy_components';
Expand All @@ -15,35 +14,32 @@ import type {
AiopsPluginSetupDeps,
AiopsPluginStart,
AiopsPluginStartDeps,
AiopsCoreSetup,
} from './types';
import { registerEmbeddables } from './embeddables';
import { registerAiopsUiActions } from './ui_actions';
import { registerCases } from './cases/register_cases';

export type AiopsCoreSetup = CoreSetup<AiopsPluginStartDeps, AiopsPluginStart>;
import { canUseAiops } from './capabilities';

export class AiopsPlugin
implements Plugin<AiopsPluginSetup, AiopsPluginStart, AiopsPluginSetupDeps, AiopsPluginStartDeps>
{
public setup(core: AiopsCoreSetup, { embeddable, cases, uiActions }: AiopsPluginSetupDeps) {
core.getStartServices().then(([coreStart, pluginStart]) => {
const { canUseAiops } = coreStart.application.capabilities.ml;
const aiopsEnabled = coreStart.application.capabilities.aiops.enabled;

if (canUseAiops && aiopsEnabled) {
if (embeddable) {
registerEmbeddables(embeddable, core);
}
if (embeddable) {
registerEmbeddables(embeddable, core.getStartServices);
}

if (uiActions) {
registerAiopsUiActions(uiActions, coreStart, pluginStart);
}
if (uiActions) {
registerAiopsUiActions(uiActions, core.getStartServices);
}

if (cases) {
if (cases) {
core.getStartServices().then(([coreStart, pluginStart]) => {
if (canUseAiops(coreStart)) {
registerCases(cases, coreStart, pluginStart);
}
}
});
});
}
}

public start(core: CoreStart, plugins: AiopsPluginStartDeps): AiopsPluginStart {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/platform/plugins/shared/aiops/public/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { CasesPublicSetup } from '@kbn/cases-plugin/public';
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { CPSPluginStart } from '@kbn/cps/public/types';
import type { CoreSetup } from '@kbn/core/public';
import type { ChangePointDetectionSharedComponent } from '../shared_components';

import type { LogCategorizationEmbeddableWrapperProps } from '../components/log_categorization/log_categorization_for_embeddable/log_categorization_for_discover_wrapper';
Expand Down Expand Up @@ -55,3 +56,5 @@ export interface AiopsPluginStart {
PatternAnalysisComponent: React.ComponentType<LogCategorizationEmbeddableWrapperProps>;
ChangePointDetectionComponent: ChangePointDetectionSharedComponent;
}

export type AiopsCoreSetup = CoreSetup<AiopsPluginStartDeps, AiopsPluginStart>;
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import type { EmbeddableApiContext } from '@kbn/presentation-publishing';
import type { UiActionsActionDefinition } from '@kbn/ui-actions-plugin/public';
import { IncompatibleActionError } from '@kbn/ui-actions-plugin/public';
import { EMBEDDABLE_CHANGE_POINT_CHART_TYPE } from '@kbn/aiops-change-point-detection/constants';
import type { CoreStart } from '@kbn/core-lifecycle-browser';

import type { AiopsPluginStartDeps } from '../types';

import type { ChangePointChartActionContext } from './change_point_action_context';
import type { ChangePointEmbeddableState } from '../../common/embeddables/change_point_chart/types';
import type { AiopsCoreSetup } from '../types';
import { canUseAiops } from '../capabilities';

const parentApiIsCompatible = async (
parentApi: unknown
Expand All @@ -29,8 +28,7 @@ const parentApiIsCompatible = async (
};

export function createAddChangePointChartAction(
coreStart: CoreStart,
pluginStart: AiopsPluginStartDeps
getStartServices: AiopsCoreSetup['getStartServices']
): UiActionsActionDefinition<ChangePointChartActionContext> {
return {
id: 'create-change-point-chart',
Expand All @@ -51,10 +49,14 @@ export function createAddChangePointChartAction(
defaultMessage: 'Change point detection',
}),
async isCompatible(context: EmbeddableApiContext) {
return Boolean(await parentApiIsCompatible(context.embeddable));
const [coreStart] = await getStartServices();
return Boolean(await parentApiIsCompatible(context.embeddable)) && canUseAiops(coreStart);
},
async execute(context) {
const presentationContainerParent = await parentApiIsCompatible(context.embeddable);
const [[coreStart, pluginStart], presentationContainerParent] = await Promise.all([
getStartServices(),
parentApiIsCompatible(context.embeddable),
]);
if (!presentationContainerParent) throw new IncompatibleActionError();

openLazyFlyout({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import type { PresentationContainer } from '@kbn/presentation-publishing';
import type { EmbeddableApiContext } from '@kbn/presentation-publishing';
import type { UiActionsActionDefinition } from '@kbn/ui-actions-plugin/public';
import { IncompatibleActionError } from '@kbn/ui-actions-plugin/public';
import type { CoreStart } from '@kbn/core-lifecycle-browser';
import { EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE } from '@kbn/aiops-log-rate-analysis/constants';
import { AIOPS_EMBEDDABLE_GROUPING } from '@kbn/aiops-common/constants';

import { v4 } from 'uuid';
import type { LogRateAnalysisEmbeddableApi } from '../embeddables/log_rate_analysis/types';
import type { AiopsPluginStartDeps } from '../types';

import type { LogRateAnalysisActionContext } from './log_rate_analysis_action_context';
import { EmbeddableLogRateAnalysisUserInput } from '../embeddables/log_rate_analysis/log_rate_analysis_config_input';
import type { AiopsCoreSetup } from '../types';
import { canUseAiops } from '../capabilities';

const parentApiIsCompatible = async (
parentApi: unknown
Expand All @@ -32,8 +32,7 @@ const parentApiIsCompatible = async (
};

export function createAddLogRateAnalysisEmbeddableAction(
coreStart: CoreStart,
pluginStart: AiopsPluginStartDeps
getStartServices: AiopsCoreSetup['getStartServices']
): UiActionsActionDefinition<LogRateAnalysisActionContext> {
return {
id: 'create-log-rate-analysis-embeddable',
Expand All @@ -44,10 +43,14 @@ export function createAddLogRateAnalysisEmbeddableAction(
defaultMessage: 'Log rate analysis',
}),
async isCompatible(context: EmbeddableApiContext) {
return Boolean(await parentApiIsCompatible(context.embeddable));
const [coreStart] = await getStartServices();
return Boolean(await parentApiIsCompatible(context.embeddable)) && canUseAiops(coreStart);
},
async execute(context) {
const presentationContainerParent = await parentApiIsCompatible(context.embeddable);
const [[coreStart, pluginStart], presentationContainerParent] = await Promise.all([
getStartServices(),
parentApiIsCompatible(context.embeddable),
]);
if (!presentationContainerParent) throw new IncompatibleActionError();

const uuid = v4();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import type { PresentationContainer } from '@kbn/presentation-publishing';
import type { EmbeddableApiContext } from '@kbn/presentation-publishing';
import type { UiActionsActionDefinition } from '@kbn/ui-actions-plugin/public';
import { IncompatibleActionError } from '@kbn/ui-actions-plugin/public';
import type { CoreStart } from '@kbn/core-lifecycle-browser';
import { EMBEDDABLE_PATTERN_ANALYSIS_TYPE } from '@kbn/aiops-log-pattern-analysis/constants';
import { AIOPS_EMBEDDABLE_GROUPING } from '@kbn/aiops-common/constants';

import { DEFAULT_PROBABILITY, RANDOM_SAMPLER_OPTION } from '@kbn/ml-random-sampler-utils';
import type { AiopsPluginStartDeps } from '../types';
import type { PatternAnalysisEmbeddableApi } from '../embeddables/pattern_analysis/types';

import type { PatternAnalysisActionContext } from './pattern_analysis_action_context';
import { DEFAULT_MINIMUM_TIME_RANGE_OPTION } from '../components/log_categorization/log_categorization_for_embeddable/minimum_time_range';
import type { AiopsCoreSetup } from '../types';
import { canUseAiops } from '../capabilities';

const parentApiIsCompatible = async (
parentApi: unknown
Expand All @@ -30,8 +30,7 @@ const parentApiIsCompatible = async (
};

export function createAddPatternAnalysisEmbeddableAction(
coreStart: CoreStart,
pluginStart: AiopsPluginStartDeps
getStartServices: AiopsCoreSetup['getStartServices']
): UiActionsActionDefinition<PatternAnalysisActionContext> {
return {
id: 'create-pattern-analysis-embeddable',
Expand All @@ -42,10 +41,14 @@ export function createAddPatternAnalysisEmbeddableAction(
defaultMessage: 'Pattern analysis',
}),
async isCompatible(context: EmbeddableApiContext) {
return Boolean(await parentApiIsCompatible(context.embeddable));
const [coreStart] = await getStartServices();
return Boolean(await parentApiIsCompatible(context.embeddable)) && canUseAiops(coreStart);
},
async execute(context) {
const presentationContainerParent = await parentApiIsCompatible(context.embeddable);
const [[coreStart, pluginStart], presentationContainerParent] = await Promise.all([
getStartServices(),
parentApiIsCompatible(context.embeddable),
]);
if (!presentationContainerParent) throw new IncompatibleActionError();

try {
Expand Down
Loading
Loading