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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
*/
export const CHANGE_POINT_DETECTION_ENABLED = true;

export const CASES_ATTACHMENT_CHANGE_POINT_CHART = 'aiopsChangePointChart';

//
// Do not change constant value - part of public REST APIs
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

export const CASES_ATTACHMENT_LOG_PATTERN = 'aiopsPatternAnalysisEmbeddable';

//
// Do not change constant value - part of public REST APIs
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ export const EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE = 'aiops_log_rate_analysis' as co

/** */
export const LOG_RATE_ANALYSIS_DATA_VIEW_REF_NAME = 'aiopsLogRateAnalysisEmbeddableDataViewId';

export const CASES_ATTACHMENT_LOG_RATE_ANALYSIS = 'aiopsLogRateAnalysisEmbeddable';
19 changes: 19 additions & 0 deletions x-pack/platform/plugins/shared/aiops/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { isPlainObject } from 'lodash';

export const casesSchemaValidator = (data: unknown) => {
if (!isPlainObject(data) || data === null) {
throw new Error('Persistable attachment data must be an object');
}

const state = (data as Record<string, unknown>).state;
if (!isPlainObject(state) || state === null) {
throw new Error('Persistable attachment data must include object "state"');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { memoize } from 'lodash';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import React from 'react';
import type { PersistableStateAttachmentViewProps } from '@kbn/cases-plugin/public/client/attachment_framework/types';
import type { UnifiedValueAttachmentViewProps } from '@kbn/cases-plugin/public/client/attachment_framework/types';
import { FIELD_FORMAT_IDS } from '@kbn/field-formats-plugin/common';
import type { TimeRange } from '@kbn/es-query';
import { FormattedMessage } from '@kbn/i18n-react';
Expand All @@ -25,17 +25,15 @@ export const initComponent = memoize(
ChangePointDetectionComponent: ChangePointDetectionSharedComponent
) => {
return React.memo(
(props: PersistableStateAttachmentViewProps) => {
const { persistableStateAttachmentState } = props;

(props: UnifiedValueAttachmentViewProps) => {
const dataFormatter = fieldFormats.deserialize({
id: FIELD_FORMAT_IDS.DATE,
});

const rawState = persistableStateAttachmentState as unknown as Record<string, unknown>;
const rawState = props.data.state as Record<string, unknown>;
const timeRange = (rawState.time_range ?? rawState.timeRange) as TimeRange;
const inputProps = {
...(persistableStateAttachmentState as unknown as ChangePointDetectionProps),
...(rawState as unknown as ChangePointDetectionProps),
timeRange,
};

Expand All @@ -60,11 +58,7 @@ export const initComponent = memoize(
</>
);
},
(prevProps, nextProps) =>
deepEqual(
prevProps.persistableStateAttachmentState,
nextProps.persistableStateAttachmentState
)
(prevProps, nextProps) => deepEqual(prevProps.data.state, nextProps.data.state)
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { memoize } from 'lodash';
import React from 'react';
import type { PersistableStateAttachmentViewProps } from '@kbn/cases-plugin/public/client/attachment_framework/types';
import type { UnifiedValueAttachmentViewProps } from '@kbn/cases-plugin/public/client/attachment_framework/types';
import { FIELD_FORMAT_IDS } from '@kbn/field-formats-plugin/common';
import type { TimeRange } from '@kbn/es-query';
import { FormattedMessage } from '@kbn/i18n-react';
Expand All @@ -22,17 +22,16 @@ import type {
export const initComponent = memoize(
(fieldFormats: FieldFormatsStart, PatternAnalysisComponent: PatternAnalysisSharedComponent) => {
return React.memo(
(props: PersistableStateAttachmentViewProps) => {
const { persistableStateAttachmentState } = props;
(props: UnifiedValueAttachmentViewProps) => {
const rawState = props.data.state as Record<string, unknown>;

const dataFormatter = fieldFormats.deserialize({
id: FIELD_FORMAT_IDS.DATE,
});

const rawState = persistableStateAttachmentState as unknown as Record<string, unknown>;
const timeRange = (rawState.time_range ?? rawState.timeRange) as TimeRange;
const inputProps = {
...(persistableStateAttachmentState as unknown as PatternAnalysisProps),
...(rawState as unknown as PatternAnalysisProps),
timeRange,
};

Expand All @@ -57,11 +56,7 @@ export const initComponent = memoize(
</>
);
},
(prevProps, nextProps) =>
deepEqual(
prevProps.persistableStateAttachmentState,
nextProps.persistableStateAttachmentState
)
(prevProps, nextProps) => deepEqual(prevProps.data.state, nextProps.data.state)
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import { memoize } from 'lodash';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { PersistableStateAttachmentViewProps } from '@kbn/cases-plugin/public/client/attachment_framework/types';
import type { UnifiedValueAttachmentViewProps } from '@kbn/cases-plugin/public/client/attachment_framework/types';
import { FIELD_FORMAT_IDS } from '@kbn/field-formats-plugin/common';
import type { TimeRange } from '@kbn/es-query';
import { FormattedMessage } from '@kbn/i18n-react';
Expand All @@ -20,15 +20,14 @@ import type {

export const initComponent = memoize(
(fieldFormats: FieldFormatsStart, LogRateAnalysisComponent: LogRateAnalysisEmbeddableWrapper) => {
return React.memo((props: PersistableStateAttachmentViewProps) => {
const { persistableStateAttachmentState } = props;
return React.memo((props: UnifiedValueAttachmentViewProps) => {
const rawState = props.data.state as Record<string, unknown>;
const dataFormatter = fieldFormats.deserialize({
id: FIELD_FORMAT_IDS.DATE,
});
const rawState = persistableStateAttachmentState as unknown as Record<string, unknown>;
const timeRange = (rawState.time_range ?? rawState.timeRange) as TimeRange;
const inputProps = {
...(persistableStateAttachmentState as unknown as LogRateAnalysisEmbeddableWrapperProps),
...(rawState as unknown as LogRateAnalysisEmbeddableWrapperProps),
timeRange,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import React from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import type { CasesPublicSetup } from '@kbn/cases-plugin/public';
import {
AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
} from '@kbn/cases-plugin/common';
import type { CoreStart } from '@kbn/core/public';
import { CASES_ATTACHMENT_CHANGE_POINT_CHART } from '@kbn/aiops-change-point-detection/constants';
import { CASES_ATTACHMENT_LOG_PATTERN } from '@kbn/aiops-log-pattern-analysis/constants';
import { CASES_ATTACHMENT_LOG_RATE_ANALYSIS } from '@kbn/aiops-log-rate-analysis/constants';
import {
getChangePointDetectionComponent,
getLogRateAnalysisEmbeddableWrapperComponent,
getPatternAnalysisComponent,
} from '../shared_components';
import type { AiopsPluginStartDeps } from '../types';
import { casesSchemaValidator } from '../../common/utils';

export function registerCases(
cases: CasesPublicSetup,
Expand All @@ -27,8 +30,8 @@ export function registerCases(
) {
const ChangePointDetectionComponent = getChangePointDetectionComponent(coreStart, pluginStart);

cases.attachmentFramework.registerPersistableState({
id: CASES_ATTACHMENT_CHANGE_POINT_CHART,
cases.attachmentFramework.registerUnified({
id: AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
icon: 'machineLearningApp',
displayName: i18n.translate('xpack.aiops.changePointDetection.cases.attachmentName', {
defaultMessage: 'Change point chart',
Expand Down Expand Up @@ -57,12 +60,13 @@ export function registerCases(
/>
),
}),
schemaValidator: casesSchemaValidator,
});

const LogPatternAttachmentComponent = getPatternAnalysisComponent(coreStart, pluginStart);

cases.attachmentFramework.registerPersistableState({
id: CASES_ATTACHMENT_LOG_PATTERN,
cases.attachmentFramework.registerUnified({
id: AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
icon: 'machineLearningApp',
displayName: i18n.translate('xpack.aiops.logPatternAnalysis.cases.attachmentName', {
defaultMessage: 'Log pattern analysis',
Expand All @@ -89,15 +93,16 @@ export function registerCases(
/>
),
}),
schemaValidator: casesSchemaValidator,
});

const LogRateAnalysisEmbeddableWrapperComponent = getLogRateAnalysisEmbeddableWrapperComponent(
coreStart,
pluginStart
);

cases.attachmentFramework.registerPersistableState({
id: CASES_ATTACHMENT_LOG_RATE_ANALYSIS,
cases.attachmentFramework.registerUnified({
id: AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
icon: 'machineLearningApp',
displayName: i18n.translate('xpack.aiops.logRateAnalysis.cases.attachmentName', {
defaultMessage: 'Log rate analysis',
Expand Down Expand Up @@ -129,5 +134,6 @@ export function registerCases(
/>
),
}),
schemaValidator: casesSchemaValidator,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@

import { useCallback, useMemo } from 'react';
import { stringHash } from '@kbn/ml-string-hash';
import { AttachmentType } from '@kbn/cases-plugin/common';
import {
AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
} from '@kbn/cases-plugin/common';
import { i18n } from '@kbn/i18n';
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 { ChangePointEmbeddableState } from '../../common/embeddables/change_point_chart/types';
import type { EmbeddableChangePointChartType } from '../embeddables/change_point_chart/embeddable_change_point_chart_factory';
import { useAiopsAppContext } from './use_aiops_app_context';
Expand All @@ -31,6 +38,12 @@ type EmbeddableRuntimeState<T extends SupportedEmbeddableTypes> =
? LogRateAnalysisEmbeddableState
: never;

const attachmentTypeByEmbeddableType: Record<SupportedEmbeddableTypes, string> = {
[EMBEDDABLE_CHANGE_POINT_CHART_TYPE]: AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
[EMBEDDABLE_PATTERN_ANALYSIS_TYPE]: AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
[EMBEDDABLE_LOG_RATE_ANALYSIS_TYPE]: AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
};

/**
* Returns a callback for opening the cases modal with provided attachment state.
*/
Expand Down Expand Up @@ -68,11 +81,10 @@ export const useCasesModal = <EmbeddableType extends SupportedEmbeddableTypes>(
selectCaseModal.open({
getAttachments: () => [
{
type: AttachmentType.persistableState,
persistableStateAttachmentTypeId: embeddableType,
persistableStateAttachmentState: JSON.parse(
JSON.stringify(persistableStateAttachmentState)
),
type: attachmentTypeByEmbeddableType[embeddableType],
data: {
state: JSON.parse(JSON.stringify(persistableStateAttachmentState)),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we consider structuredClone here?

},
},
],
});
Expand Down
4 changes: 2 additions & 2 deletions x-pack/platform/plugins/shared/aiops/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
import { defineRoute as defineLogRateAnalysisFieldCandidatesRoute } from './routes/log_rate_analysis_field_candidates/define_route';
import { defineRoute as defineLogRateAnalysisRoute } from './routes/log_rate_analysis/define_route';
import { defineRoute as defineCategorizationFieldValidationRoute } from './routes/categorization_field_validation/define_route';
import { registerCasesPersistableState } from './register_cases';
import { registerCaseAttachments } from './register_cases';
import type { ConfigSchema } from './config_schema';
import { setupCapabilities } from './lib/capabilities';
import { transformIn as changePointTransformIn } from '../common/embeddables/change_point_chart/transform_in';
Expand Down Expand Up @@ -70,7 +70,7 @@ export class AiopsPlugin
aiopsLicense.isActivePlatinumLicense = isActiveLicense('platinum', license);

if (aiopsLicense.isActivePlatinumLicense) {
registerCasesPersistableState(plugins.cases, this.logger);
registerCaseAttachments(plugins.cases, this.logger);
}
});

Expand Down
26 changes: 16 additions & 10 deletions x-pack/platform/plugins/shared/aiops/server/register_cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@

import type { Logger } from '@kbn/core/server';
import type { CasesServerSetup } from '@kbn/cases-plugin/server';
import { CASES_ATTACHMENT_CHANGE_POINT_CHART } from '@kbn/aiops-change-point-detection/constants';
import { CASES_ATTACHMENT_LOG_PATTERN } from '@kbn/aiops-log-pattern-analysis/constants';
import { CASES_ATTACHMENT_LOG_RATE_ANALYSIS } from '@kbn/aiops-log-rate-analysis/constants';
import {
AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
} from '@kbn/cases-plugin/common';
import { casesSchemaValidator } from '../common/utils';

export function registerCasesPersistableState(cases: CasesServerSetup | undefined, logger: Logger) {
export function registerCaseAttachments(cases: CasesServerSetup | undefined, logger: Logger) {
if (cases) {
try {
cases.attachmentFramework.registerPersistableState({
id: CASES_ATTACHMENT_CHANGE_POINT_CHART,
cases.attachmentFramework.registerUnified({
id: AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
schemaValidator: casesSchemaValidator,
});
cases.attachmentFramework.registerPersistableState({
id: CASES_ATTACHMENT_LOG_PATTERN,
cases.attachmentFramework.registerUnified({
id: AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
schemaValidator: casesSchemaValidator,
});
cases.attachmentFramework.registerPersistableState({
id: CASES_ATTACHMENT_LOG_RATE_ANALYSIS,
cases.attachmentFramework.registerUnified({
id: AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
schemaValidator: casesSchemaValidator,
});
} catch (error) {
logger.warn(`AIOPs failed to register cases persistable state`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export const COMMENT_ATTACHMENT_TYPE = 'comment';
export const SECURITY_EVENT_ATTACHMENT_TYPE = 'security.event';
export const LENS_ATTACHMENT_TYPE = 'lens';

export const ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE = 'ml.anomaly_swimlane';
export const ML_ANOMALY_CHARTS_ATTACHMENT_TYPE = 'ml.anomaly_charts';
export const ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE = 'ml.single_metric_viewer';
export const AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE = 'aiops.change_point_chart';
export const AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE = 'aiops.pattern_analysis';
export const AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE = 'aiops.log_rate_analysis';

// ----------------Legacy attachment types-------------------------
export const LEGACY_ACTIONS_TYPE = 'actions';
export const LEGACY_ALERT_TYPE = 'alert';
Expand All @@ -20,6 +27,12 @@ export const LEGACY_PERSISTABLE_STATE_TYPE = 'persistableState';
export const LEGACY_USER_TYPE = 'user';

export const LEGACY_LENS_ATTACHMENT_TYPE = '.lens';
export const LEGACY_ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE = 'ml_anomaly_swimlane';
export const LEGACY_ML_ANOMALY_CHARTS_ATTACHMENT_TYPE = 'ml_anomaly_charts';
export const LEGACY_ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE = 'ml_single_metric_viewer';
export const LEGACY_AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE = 'aiopsChangePointChart';
export const LEGACY_AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE = 'aiopsPatternAnalysisEmbeddable';
export const LEGACY_AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE = 'aiopsLogRateAnalysisEmbeddable';

export const LEGACY_ATTACHMENT_TYPES = new Set([
LEGACY_ACTIONS_TYPE,
Expand All @@ -37,10 +50,22 @@ export const UNIFIED_ATTACHMENT_TYPES = new Set([

export const PERSISTABLE_STATE_LEGACY_TO_UNIFIED_MAP: Record<string, string> = {
[LEGACY_LENS_ATTACHMENT_TYPE]: LENS_ATTACHMENT_TYPE,
[LEGACY_ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE]: ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE,
[LEGACY_ML_ANOMALY_CHARTS_ATTACHMENT_TYPE]: ML_ANOMALY_CHARTS_ATTACHMENT_TYPE,
[LEGACY_ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE]: ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE,
[LEGACY_AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE]: AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
[LEGACY_AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE]: AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
[LEGACY_AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE]: AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
} as const;

export const PERSISTABLE_STATE_UNIFIED_TO_LEGACY_MAP: Record<string, string> = {
[LENS_ATTACHMENT_TYPE]: LEGACY_LENS_ATTACHMENT_TYPE,
[ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE]: LEGACY_ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE,
[ML_ANOMALY_CHARTS_ATTACHMENT_TYPE]: LEGACY_ML_ANOMALY_CHARTS_ATTACHMENT_TYPE,
[ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE]: LEGACY_ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE,
[AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE]: LEGACY_AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
[AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE]: LEGACY_AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
[AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE]: LEGACY_AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
} as const;

export const PERSISTABLE_ATTACHMENT_TYPES = new Set<string>(
Expand Down
6 changes: 6 additions & 0 deletions x-pack/platform/plugins/shared/cases/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export {
ASSIGN_CASE_CAPABILITY,
SECURITY_EVENT_ATTACHMENT_TYPE,
MANAGE_TEMPLATES_CAPABILITY,
ML_ANOMALY_SWIMLANE_ATTACHMENT_TYPE,
ML_ANOMALY_CHARTS_ATTACHMENT_TYPE,
ML_SINGLE_METRIC_VIEWER_ATTACHMENT_TYPE,
AIOPS_CHANGE_POINT_CHART_ATTACHMENT_TYPE,
AIOPS_PATTERN_ANALYSIS_ATTACHMENT_TYPE,
AIOPS_LOG_RATE_ANALYSIS_ATTACHMENT_TYPE,
} from './constants';

export type { AttachmentAttributes } from './types/domain';
Expand Down
Loading
Loading