Skip to content

Commit c8754cf

Browse files
[Dashboards as code] Convert anomaly single metric view Embeddable types to API schemas (elastic#239161)
## Summary Convert SingleMetricViewerEmbeddableState to API schemas --------- Co-authored-by: kibanamachine <[email protected]>
1 parent 40d53ff commit c8754cf

File tree

9 files changed

+100
-36
lines changed

9 files changed

+100
-36
lines changed

x-pack/platform/packages/shared/ml/anomaly_utils/schemas.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ export const criteriaFieldSchema = schema.object({
3030
fieldType: schema.maybe(mlEntityFieldTypeSchema),
3131
});
3232

33+
export const mlEntityFieldValueSchema = schema.oneOf([schema.string(), schema.number()]);
34+
3335
export const mlEntityFieldSchema = schema.object({
3436
fieldName: schema.string(),
35-
fieldValue: schema.maybe(schema.oneOf([schema.string(), schema.number()])),
37+
fieldValue: schema.maybe(mlEntityFieldValueSchema),
3638
fieldType: schema.maybe(mlEntityFieldTypeSchema),
3739
operation: schema.maybe(mlEntityFieldOperationSchema),
3840
cardinality: schema.maybe(schema.number()),

x-pack/platform/plugins/shared/ml/public/embeddables/single_metric_viewer/single_metric_viewer_controls_initializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { JobId } from '../../../common/types/anomaly_detection_jobs';
1111
import type {
1212
SingleMetricViewerEmbeddableState,
1313
SingleMetricViewerEmbeddableUserInput,
14-
} from '../types';
14+
} from './types';
1515

1616
export type AnomalySwimLaneControlsState = Pick<
1717
SingleMetricViewerEmbeddableState,

x-pack/platform/plugins/shared/ml/public/embeddables/single_metric_viewer/single_metric_viewer_embeddable_factory.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { BehaviorSubject, Subscription, merge } from 'rxjs';
2424
import { initializeUnsavedChanges } from '@kbn/presentation-containers';
2525
import { ANOMALY_SINGLE_METRIC_VIEWER_EMBEDDABLE_TYPE } from '..';
2626
import type { MlPluginStart, MlStartDependencies } from '../../plugin';
27-
import type { SingleMetricViewerEmbeddableApi, SingleMetricViewerEmbeddableState } from '../types';
27+
import type { SingleMetricViewerEmbeddableApi } from '../types';
2828
import {
2929
initializeSingleMetricViewerControls,
3030
singleMetricViewerComparators,
@@ -34,6 +34,7 @@ import { getServices } from './get_services';
3434
import { useReactEmbeddableExecutionContext } from '../common/use_embeddable_execution_context';
3535
import { getSingleMetricViewerComponent } from '../../shared_components/single_metric_viewer';
3636
import { EmbeddableSingleMetricViewerUserInput } from './single_metric_viewer_setup_flyout';
37+
import type { SingleMetricViewerEmbeddableState } from './types';
3738

3839
export const getSingleMetricViewerEmbeddableFactory = (
3940
getStartServices: StartServicesAccessor<MlStartDependencies, MlPluginStart>

x-pack/platform/plugins/shared/ml/public/embeddables/single_metric_viewer/single_metric_viewer_initializer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { extractErrorMessage } from '@kbn/ml-error-utils';
2828
import type { MlJob } from '@elastic/elasticsearch/lib/api/types';
2929
import type { TimeRangeBounds } from '@kbn/ml-time-buckets';
3030
import type { MlApi } from '../../application/services/ml_api_service';
31-
import type { SingleMetricViewerEmbeddableInput } from '..';
3231
import { ML_PAGES } from '../../../common/constants/locator';
3332
import { SeriesControls } from '../../application/timeseriesexplorer/components/series_controls';
3433
import {
@@ -39,6 +38,7 @@ import { useMlLink } from '../../application/contexts/kibana';
3938
import { JobSelectorControl } from '../../alerting/job_selector';
4039
import type { SingleMetricViewerEmbeddableUserInput, MlEntity } from '..';
4140
import { getDefaultSingleMetricViewerPanelTitle } from './get_default_panel_title';
41+
import type { SingleMetricViewerEmbeddableInput } from './types';
4242

4343
export interface SingleMetricViewerInitializerProps {
4444
bounds: TimeRangeBounds;

x-pack/platform/plugins/shared/ml/public/embeddables/single_metric_viewer/single_metric_viewer_setup_flyout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
1212
import type { SharePluginStart } from '@kbn/share-plugin/public';
1313
import { StorageContextProvider } from '@kbn/ml-local-storage';
1414
import { Storage } from '@kbn/kibana-utils-plugin/public';
15-
import type { SingleMetricViewerEmbeddableUserInput, SingleMetricViewerEmbeddableInput } from '..';
15+
import type { SingleMetricViewerEmbeddableUserInput } from '..';
1616
import { SingleMetricViewerInitializer } from './single_metric_viewer_initializer';
1717
import type { MlApi } from '../../application/services/ml_api_service';
1818
import { ML_STORAGE_KEYS } from '../../../common/types/storage';
19+
import type { SingleMetricViewerEmbeddableInput } from './types';
1920

2021
export function EmbeddableSingleMetricViewerUserInput({
2122
coreStart,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 { MlEntityField } from '@kbn/ml-anomaly-utils';
9+
import type { TypeOf } from '@kbn/config-schema';
10+
import type {
11+
singleMetricViewerEmbeddableCustomInputSchema,
12+
singleMetricViewerEmbeddableInputSchema,
13+
singleMetricViewerEmbeddableStateSchema,
14+
singleMetricViewerEmbeddableUserInputSchema,
15+
} from '../../../server/embeddable/single_metric_viewer/single_metric_viewer';
16+
17+
export type MlEntity = Record<string, MlEntityField['fieldValue']>;
18+
19+
export type SingleMetricViewerEmbeddableUserInput = TypeOf<
20+
typeof singleMetricViewerEmbeddableUserInputSchema
21+
>;
22+
23+
export type SingleMetricViewerEmbeddableCustomInput = TypeOf<
24+
typeof singleMetricViewerEmbeddableCustomInputSchema
25+
>;
26+
27+
export type SingleMetricViewerEmbeddableState = TypeOf<
28+
typeof singleMetricViewerEmbeddableStateSchema
29+
>;
30+
31+
export type SingleMetricViewerEmbeddableInput = TypeOf<
32+
typeof singleMetricViewerEmbeddableInputSchema
33+
>;

x-pack/platform/plugins/shared/ml/public/embeddables/types.ts

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

88
import type { CoreStart } from '@kbn/core/public';
9-
import type { RefreshInterval } from '@kbn/data-plugin/common';
109
import type { DefaultEmbeddableApi } from '@kbn/embeddable-plugin/public';
1110
import type { Filter, Query, TimeRange } from '@kbn/es-query';
1211
import type { MlEntityField } from '@kbn/ml-anomaly-utils';
@@ -20,7 +19,6 @@ import type {
2019
PublishesTimeRange,
2120
PublishesWritableTitle,
2221
PublishesDataViews,
23-
SerializedTitles,
2422
} from '@kbn/presentation-publishing';
2523
import { type BehaviorSubject } from 'rxjs';
2624
import type { TypeOf } from '@kbn/config-schema';
@@ -53,21 +51,30 @@ import type {
5351
anomalySwimlaneEmbeddableUserInputSchema,
5452
anomalySwimlaneInitialInputSchema,
5553
} from '../../server/embeddable/schemas';
54+
import type {
55+
MlEntity,
56+
SingleMetricViewerEmbeddableState,
57+
SingleMetricViewerEmbeddableUserInput,
58+
} from './single_metric_viewer/types';
5659

5760
export type {
5861
AnomalySwimLaneEmbeddableState,
5962
AnomalySwimLaneEmbeddableApi,
6063
} from './anomaly_swimlane/types';
6164

65+
export type {
66+
MlEntity,
67+
SingleMetricViewerEmbeddableUserInput,
68+
SingleMetricViewerEmbeddableState,
69+
} from './single_metric_viewer/types';
70+
6271
/**
6372
* Common API for all ML embeddables
6473
*/
6574
export interface MlEmbeddableBaseApi<StateType extends object = object>
6675
extends DefaultEmbeddableApi<StateType>,
6776
PublishesTimeRange {}
6877

69-
export type MlEntity = Record<string, MlEntityField['fieldValue']>;
70-
7178
/** Manual input by the user */
7279
export type AnomalySwimlaneEmbeddableUserInput = TypeOf<
7380
typeof anomalySwimlaneEmbeddableUserInputSchema
@@ -168,36 +175,9 @@ export interface AnomalyChartsAttachmentApi extends AnomalyChartsApi {
168175
};
169176
}
170177

171-
/** Manual input by the user */
172-
export interface SingleMetricViewerEmbeddableUserInput {
173-
forecastId?: string;
174-
functionDescription?: string;
175-
jobIds: JobId[];
176-
selectedDetectorIndex: number;
177-
selectedEntities?: MlEntity;
178-
panelTitle?: string;
179-
}
180-
181-
export interface SingleMetricViewerEmbeddableCustomInput
182-
extends Omit<SingleMetricViewerEmbeddableUserInput, 'panelTitle'> {
183-
id?: string;
184-
filters?: Filter[];
185-
query?: Query;
186-
refreshConfig?: RefreshInterval;
187-
timeRange: TimeRange | undefined;
188-
}
189-
190-
export type SingleMetricViewerEmbeddableInput = SingleMetricViewerEmbeddableCustomInput & {
191-
title?: string;
192-
};
193-
194178
/**
195179
* Persisted state for the Single Metric Embeddable.
196180
*/
197-
export interface SingleMetricViewerEmbeddableState
198-
extends SerializedTitles,
199-
SingleMetricViewerEmbeddableCustomInput {}
200-
201181
export type SingleMetricViewerEmbeddableApi =
202182
MlEmbeddableBaseApi<SingleMetricViewerEmbeddableState> &
203183
PublishesWritableTitle &

x-pack/platform/plugins/shared/ml/server/embeddable/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77

88
export * from './anomaly_charts/anomaly_charts';
99
export * from './anomaly_swimlane/anomaly_swimlane';
10+
export * from './single_metric_viewer/single_metric_viewer';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { schema } from '@kbn/config-schema';
9+
import { filterSchema, querySchema, timeRangeSchema } from '@kbn/es-query-server';
10+
import { refreshIntervalSchema } from '@kbn/data-service-server';
11+
import { serializedTitlesSchema } from '@kbn/presentation-publishing-schemas';
12+
import { mlEntityFieldValueSchema } from '@kbn/ml-anomaly-utils/schemas';
13+
14+
const baseUserInputProps = schema.object({
15+
forecastId: schema.maybe(schema.string()),
16+
functionDescription: schema.maybe(schema.string()),
17+
jobIds: schema.arrayOf(schema.string()),
18+
selectedDetectorIndex: schema.number(),
19+
selectedEntities: schema.maybe(
20+
schema.recordOf(schema.string(), schema.maybe(mlEntityFieldValueSchema))
21+
),
22+
});
23+
24+
export const singleMetricViewerEmbeddableUserInputSchema = schema.object({
25+
...baseUserInputProps.getPropSchemas(),
26+
panelTitle: schema.maybe(schema.string()),
27+
});
28+
29+
export const singleMetricViewerEmbeddableCustomInputSchema = schema.object({
30+
...baseUserInputProps.getPropSchemas(),
31+
id: schema.maybe(schema.string()),
32+
filters: schema.maybe(schema.arrayOf(filterSchema)),
33+
query: schema.maybe(querySchema),
34+
refreshConfig: schema.maybe(refreshIntervalSchema),
35+
timeRange: schema.maybe(timeRangeSchema),
36+
});
37+
38+
export const singleMetricViewerEmbeddableInputSchema = schema.object({
39+
...singleMetricViewerEmbeddableCustomInputSchema.getPropSchemas(),
40+
title: schema.maybe(schema.string()),
41+
});
42+
43+
export const singleMetricViewerEmbeddableStateSchema = schema.object({
44+
...serializedTitlesSchema.getPropSchemas(),
45+
...singleMetricViewerEmbeddableCustomInputSchema.getPropSchemas(),
46+
});

0 commit comments

Comments
 (0)