forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_actions_service_enhancements.ts
220 lines (200 loc) · 7.17 KB
/
ui_actions_service_enhancements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { SerializableRecord } from '@kbn/utility-types';
import { ILicense } from '@kbn/licensing-plugin/common/types';
import { LicensingPluginSetup, LicensingPluginStart } from '@kbn/licensing-plugin/public';
import { SavedObjectReference } from '@kbn/core/types';
import { PersistableStateDefinition } from '@kbn/kibana-utils-plugin/common';
import { DrilldownDefinition } from '../drilldowns';
import {
ActionFactory,
ActionFactoryDefinition,
BaseActionConfig,
BaseActionFactoryContext,
SerializedEvent,
} from '../dynamic_actions';
import { ActionFactoryRegistry } from '../types';
import { DynamicActionsState } from '../../common/types';
export type { DynamicActionsState };
export interface UiActionsServiceEnhancementsParams {
readonly actionFactories?: ActionFactoryRegistry;
readonly getLicense?: () => ILicense;
readonly featureUsageSetup?: LicensingPluginSetup['featureUsage'];
readonly getFeatureUsageStart: () => LicensingPluginStart['featureUsage'] | undefined;
}
export class UiActionsServiceEnhancements
implements PersistableStateDefinition<DynamicActionsState>
{
protected readonly actionFactories: ActionFactoryRegistry;
protected readonly deps: Omit<UiActionsServiceEnhancementsParams, 'actionFactories'>;
constructor({ actionFactories = new Map(), ...deps }: UiActionsServiceEnhancementsParams) {
this.actionFactories = actionFactories;
this.deps = deps;
}
/**
* Register an action factory. Action factories are used to configure and
* serialize/deserialize dynamic actions.
*/
public readonly registerActionFactory = <
Config extends BaseActionConfig = BaseActionConfig,
ExecutionContext extends object = object,
FactoryContext extends BaseActionFactoryContext = BaseActionFactoryContext
>(
definition: ActionFactoryDefinition<Config, ExecutionContext, FactoryContext>
) => {
if (this.actionFactories.has(definition.id)) {
throw new Error(`ActionFactory [actionFactory.id = ${definition.id}] already registered.`);
}
const actionFactory = new ActionFactory<Config, ExecutionContext, FactoryContext>(
definition,
this.deps
);
this.actionFactories.set(
actionFactory.id,
actionFactory as unknown as ActionFactory<
SerializableRecord,
ExecutionContext,
BaseActionFactoryContext
>
);
this.registerFeatureUsage(definition as unknown as ActionFactoryDefinition);
};
public readonly getActionFactory = (actionFactoryId: string): ActionFactory => {
const actionFactory = this.actionFactories.get(actionFactoryId);
if (!actionFactory) {
throw new Error(`Action factory [actionFactoryId = ${actionFactoryId}] does not exist.`);
}
return actionFactory;
};
public readonly hasActionFactory = (actionFactoryId: string): boolean => {
return this.actionFactories.has(actionFactoryId);
};
/**
* Returns an array of all action factories.
*/
public readonly getActionFactories = (): ActionFactory[] => {
return [...this.actionFactories.values()];
};
/**
* Convenience method to register a {@link DrilldownDefinition | drilldown}.
*/
public readonly registerDrilldown = <
Config extends BaseActionConfig = BaseActionConfig,
ExecutionContext extends object = object,
FactoryContext extends BaseActionFactoryContext = BaseActionFactoryContext
>({
id: factoryId,
isBeta,
order,
CollectConfig,
createConfig,
isConfigValid,
getDisplayName,
actionMenuItem,
euiIcon,
execute,
getHref,
minimalLicense,
licenseFeatureName,
supportedTriggers,
isCompatible,
isConfigurable,
telemetry,
extract,
inject,
}: DrilldownDefinition<Config, ExecutionContext, FactoryContext>): void => {
const actionFactory: ActionFactoryDefinition<Config, ExecutionContext, FactoryContext> = {
id: factoryId,
isBeta,
minimalLicense,
licenseFeatureName,
order,
CollectConfig,
createConfig,
isConfigValid,
getDisplayName,
supportedTriggers,
telemetry,
extract,
inject,
getIconType: () => euiIcon,
isCompatible: async (context) => !isConfigurable || isConfigurable(context),
create: (serializedAction) => ({
id: '',
type: factoryId,
getIconType: () => euiIcon,
getDisplayName: () => serializedAction.name,
MenuItem: actionMenuItem
? () => {
const comp = actionMenuItem();
return {
render: (el, { context }) => {
comp.render(el, { context, config: serializedAction });
},
unmount: comp.unmount,
};
}
: undefined,
execute: async (context) => await execute(serializedAction.config, context),
getHref: getHref ? async (context) => getHref(serializedAction.config, context) : undefined,
isCompatible: isCompatible
? async (context) => isCompatible(serializedAction.config, context)
: undefined,
}),
} as ActionFactoryDefinition<Config, ExecutionContext, FactoryContext>;
this.registerActionFactory(actionFactory);
};
private registerFeatureUsage = (definition: ActionFactoryDefinition): void => {
if (!definition.minimalLicense || !definition.licenseFeatureName) return;
if (this.deps.featureUsageSetup) {
this.deps.featureUsageSetup.register(
definition.licenseFeatureName,
definition.minimalLicense
);
}
};
public readonly telemetry = (
state: DynamicActionsState,
telemetry: Record<string, string | number | boolean> = {}
) => {
let telemetryData = telemetry;
state.events.forEach((event: SerializedEvent) => {
if (this.actionFactories.has(event.action.factoryId)) {
telemetryData = this.actionFactories
.get(event.action.factoryId)!
.telemetry(event, telemetryData);
}
});
return telemetryData;
};
public readonly extract = (state: DynamicActionsState) => {
const references: SavedObjectReference[] = [];
const newState = {
events: state.events.map((event: SerializedEvent) => {
const result = this.actionFactories.has(event.action.factoryId)
? this.actionFactories.get(event.action.factoryId)!.extract(event)
: {
state: event,
references: [],
};
references.push(...result.references);
return result.state;
}),
};
return { state: newState, references };
};
public readonly inject = (state: DynamicActionsState, references: SavedObjectReference[]) => {
return {
events: state.events.map((event: SerializedEvent) => {
return this.actionFactories.has(event.action.factoryId)
? this.actionFactories.get(event.action.factoryId)!.inject(event, references)
: event;
}),
};
};
}