Skip to content

Commit fe017b5

Browse files
move pattern analysis embeddable reference handling to server (#244421)
Part of #222615 Part of "Dashboards as code" project involves moving reference handling from client to server. References add complexity to dashboard REST API. Moving reference handling to server removes references from dashboard REST API. This PR registers transformIn and transformOut methods for pattern analysis embeddable. On write, dashboard uses `transformIn` to extract data view reference. On read, dashboard users `transformOut` to inject data view reference. In this way, the REST API and pattern analysis embeddable client code no longer need to account for references as all reference handling is done on the server. --------- Co-authored-by: Elastic Machine <[email protected]>
1 parent 62cd83d commit fe017b5

File tree

22 files changed

+170
-136
lines changed

22 files changed

+170
-136
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '@kbn/data-views-plugin/common';
9+
import type { Reference } from '@kbn/content-management-utils';
10+
import { PATTERN_ANALYSIS_DATA_VIEW_REF_NAME } from '@kbn/aiops-log-pattern-analysis/constants';
11+
import type { PatternAnalysisEmbeddableState, StoredPatternAnalysisEmbeddableState } from './types';
12+
13+
export function transformIn(state: PatternAnalysisEmbeddableState): {
14+
state: StoredPatternAnalysisEmbeddableState;
15+
references: Reference[];
16+
} {
17+
const { dataViewId, ...rest } = state;
18+
return {
19+
state: rest,
20+
references: dataViewId
21+
? [
22+
{
23+
type: DATA_VIEW_SAVED_OBJECT_TYPE,
24+
name: PATTERN_ANALYSIS_DATA_VIEW_REF_NAME,
25+
id: dataViewId,
26+
},
27+
]
28+
: [],
29+
};
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { Reference } from '@kbn/content-management-utils';
9+
import { PATTERN_ANALYSIS_DATA_VIEW_REF_NAME } from '@kbn/aiops-log-pattern-analysis/constants';
10+
import type { PatternAnalysisEmbeddableState, StoredPatternAnalysisEmbeddableState } from './types';
11+
12+
export function transformOut(
13+
state: StoredPatternAnalysisEmbeddableState,
14+
references?: Reference[]
15+
): PatternAnalysisEmbeddableState {
16+
const dataViewIdRef = references?.find((ref) => ref.name === PATTERN_ANALYSIS_DATA_VIEW_REF_NAME);
17+
return {
18+
...state,
19+
...(dataViewIdRef && { dataViewId: dataViewIdRef.id }),
20+
};
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { RandomSamplerOption, RandomSamplerProbability } from '@kbn/ml-random-sampler-utils';
9+
import type { SerializedTimeRange } from '@kbn/presentation-publishing';
10+
import type { SerializedTitles } from '@kbn/presentation-publishing-schemas';
11+
12+
export type MinimumTimeRangeOption = 'No minimum' | '1 week' | '1 month' | '3 months' | '6 months';
13+
14+
export interface PatternAnalysisEmbeddableState extends SerializedTitles, SerializedTimeRange {
15+
dataViewId?: string;
16+
fieldName?: string;
17+
minimumTimeRangeOption: MinimumTimeRangeOption;
18+
randomSamplerMode: RandomSamplerOption;
19+
randomSamplerProbability: RandomSamplerProbability;
20+
}
21+
22+
export type StoredPatternAnalysisEmbeddableState = Omit<
23+
PatternAnalysisEmbeddableState,
24+
'dataViewId'
25+
>;

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/attachments_menu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ import {
3333
PATTERN_ANALYSIS_DATA_VIEW_REF_NAME,
3434
} from '@kbn/aiops-log-pattern-analysis/constants';
3535
import { useTimeRangeUpdates } from '@kbn/ml-date-picker';
36-
import type { PatternAnalysisEmbeddableState } from '../../embeddables/pattern_analysis/types';
37-
import type { RandomSamplerOption, RandomSamplerProbability } from './sampling_menu/random_sampler';
36+
import type { RandomSamplerOption, RandomSamplerProbability } from '@kbn/ml-random-sampler-utils';
3837
import { useCasesModal } from '../../hooks/use_cases_modal';
3938
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
4039
import { CASES_TOAST_MESSAGES_TITLES } from '../../cases/constants';
4140
import { getDataviewReferences } from '../../embeddables/get_dataview_references';
41+
import type { PatternAnalysisEmbeddableState } from '../../../common/embeddables/pattern_analysis/types';
4242

4343
const SavedObjectSaveModalDashboard = withSuspense(LazySavedObjectSaveModalDashboard);
4444

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/log_categorization_for_embeddable/discover_tabs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/type
1717
import type { OpenInDiscover } from '../category_table/use_open_in_discover';
1818
import { EmbeddableMenu } from './embeddable_menu';
1919
import type { RandomSampler } from '../sampling_menu';
20-
import type { MinimumTimeRangeOption } from './minimum_time_range';
2120
import { SelectedPatterns } from './selected_patterns';
2221
import { CreateCategorizationJobButton } from '../create_categorization_job';
2322
import { SelectedField } from './field_selector';
23+
import type { MinimumTimeRangeOption } from '../../../../common/embeddables/pattern_analysis/types';
2424

2525
interface Props {
2626
renderViewModeToggle: (patternCount?: number) => React.ReactElement;

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/log_categorization_for_embeddable/embeddable_menu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import { i18n } from '@kbn/i18n';
2525
import { FormattedMessage } from '@kbn/i18n-react';
2626
import type { RandomSampler } from '../sampling_menu';
2727
import { SamplingPanel } from '../sampling_menu/sampling_panel';
28-
import type { MinimumTimeRangeOption } from './minimum_time_range';
2928
import { MINIMUM_TIME_RANGE } from './minimum_time_range';
29+
import type { MinimumTimeRangeOption } from '../../../../common/embeddables/pattern_analysis/types';
3030

3131
interface Props {
3232
randomSampler: RandomSampler;

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/log_categorization_for_embeddable/minimum_time_range.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
*/
77
import { i18n } from '@kbn/i18n';
88
import type { unitOfTime } from 'moment';
9+
import type { MinimumTimeRangeOption } from '../../../../common/embeddables/pattern_analysis/types';
910

1011
export const DEFAULT_MINIMUM_TIME_RANGE_OPTION: MinimumTimeRangeOption = 'No minimum';
1112

12-
export type MinimumTimeRangeOption = 'No minimum' | '1 week' | '1 month' | '3 months' | '6 months';
13-
1413
type MinimumTimeRange = Record<
1514
MinimumTimeRangeOption,
1615
{ label: string; factor: number; unit: unitOfTime.Base }

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/log_categorization_for_embeddable/use_minimum_time_range.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import moment from 'moment';
1414
import { useStorage } from '@kbn/ml-local-storage';
1515
import type { MappingRuntimeFields } from '@elastic/elasticsearch/lib/api/types';
1616
import { useAiopsAppContext } from '../../../hooks/use_aiops_app_context';
17-
import type { MinimumTimeRangeOption } from './minimum_time_range';
1817
import { DEFAULT_MINIMUM_TIME_RANGE_OPTION, MINIMUM_TIME_RANGE } from './minimum_time_range';
1918
import type { AiOpsKey, AiOpsStorageMapped } from '../../../types/storage';
2019
import { AIOPS_PATTERN_ANALYSIS_MINIMUM_TIME_RANGE_PREFERENCE } from '../../../types/storage';
20+
import type { MinimumTimeRangeOption } from '../../../../common/embeddables/pattern_analysis/types';
2121

2222
export function useMinimumTimeRange() {
2323
const { http } = useAiopsAppContext();

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/sampling_menu/random_sampler.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77

88
import { BehaviorSubject } from 'rxjs';
9-
import { createRandomSamplerWrapper } from '@kbn/ml-random-sampler-utils';
9+
import type { RandomSamplerOption, RandomSamplerProbability } from '@kbn/ml-random-sampler-utils';
10+
import { RANDOM_SAMPLER_OPTION, createRandomSamplerWrapper } from '@kbn/ml-random-sampler-utils';
1011
import { i18n } from '@kbn/i18n';
1112
import type { RandomSamplerStorage } from './use_random_sampler_storage';
1213

@@ -18,16 +19,6 @@ export const MIN_SAMPLER_PROBABILITY = 0.00001;
1819
export const RANDOM_SAMPLER_STEP = MIN_SAMPLER_PROBABILITY * 100;
1920
export const DEFAULT_PROBABILITY = 0.001;
2021

21-
export const RANDOM_SAMPLER_OPTION = {
22-
ON_AUTOMATIC: 'on_automatic',
23-
ON_MANUAL: 'on_manual',
24-
OFF: 'off',
25-
} as const;
26-
27-
export type RandomSamplerOption =
28-
(typeof RANDOM_SAMPLER_OPTION)[keyof typeof RANDOM_SAMPLER_OPTION];
29-
export type RandomSamplerProbability = number | null;
30-
3122
export const RANDOM_SAMPLER_SELECT_OPTIONS: Array<{
3223
value: RandomSamplerOption;
3324
inputDisplay: string;

x-pack/platform/plugins/shared/aiops/public/components/log_categorization/sampling_menu/sampling_panel.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import { i18n } from '@kbn/i18n';
1212
import { FormattedMessage } from '@kbn/i18n-react';
1313
import useObservable from 'react-use/lib/useObservable';
1414

15+
import type { RandomSamplerOption } from '@kbn/ml-random-sampler-utils';
16+
import { RANDOM_SAMPLER_OPTION } from '@kbn/ml-random-sampler-utils';
1517
import { RandomSamplerRangeSlider } from './random_sampler_range_slider';
16-
import type { RandomSampler, RandomSamplerOption } from './random_sampler';
18+
import type { RandomSampler } from './random_sampler';
1719
import { randomSamplerText } from './random_sampler';
18-
import { RANDOM_SAMPLER_OPTION, RANDOM_SAMPLER_SELECT_OPTIONS } from './random_sampler';
20+
import { RANDOM_SAMPLER_SELECT_OPTIONS } from './random_sampler';
1921

2022
interface Props {
2123
randomSampler: RandomSampler;

0 commit comments

Comments
 (0)