-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
saved_visualize_utils.ts
404 lines (359 loc) · 11.9 KB
/
saved_visualize_utils.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* 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 _ from 'lodash';
import type {
SavedObjectsFindOptionsReference,
SavedObjectsFindOptions,
SavedObjectsClientContract,
SavedObjectAttributes,
SavedObjectReference,
} from '@kbn/core/public';
import type { OverlayStart } from '@kbn/core/public';
import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/public';
import {
extractSearchSourceReferences,
injectSearchSourceReferences,
parseSearchSourceJSON,
DataPublicPluginStart,
} from '@kbn/data-plugin/public';
import type { SavedObjectsTaggingApi } from '@kbn/saved-objects-tagging-oss-plugin/public';
import type { SpacesPluginStart } from '@kbn/spaces-plugin/public';
import { saveWithConfirmation, checkForDuplicateTitle } from './saved_objects_utils';
import { VisualizationsAppExtension } from '../vis_types/vis_type_alias_registry';
import type {
VisSavedObject,
SerializedVis,
ISavedVis,
SaveVisOptions,
GetVisOptions,
} from '../types';
import type { TypesStart, BaseVisType } from '../vis_types';
// @ts-ignore
import { updateOldState } from '../legacy/vis_update_state';
import { injectReferences, extractReferences } from './saved_visualization_references';
import { OVERWRITE_REJECTED, SAVE_DUPLICATE_REJECTED } from './saved_objects_utils/constants';
export const SAVED_VIS_TYPE = 'visualization';
const getDefaults = (opts: GetVisOptions) => ({
title: '',
visState: !opts.type ? null : { type: opts.type },
uiStateJSON: '{}',
description: '',
savedSearchId: opts.savedSearchId,
version: 1,
});
export function getFullPath(id: string) {
return `/app/visualize#/edit/${id}`;
}
export function urlFor(id: string) {
return `#/edit/${encodeURIComponent(id)}`;
}
export function mapHitSource(
visTypes: Pick<TypesStart, 'get'>,
{
attributes,
id,
references,
updatedAt,
}: {
attributes: SavedObjectAttributes;
id: string;
references: SavedObjectReference[];
updatedAt?: string;
}
) {
const newAttributes: {
id: string;
references: SavedObjectReference[];
url: string;
savedObjectType?: string;
editUrl?: string;
updatedAt?: string;
type?: BaseVisType;
icon?: BaseVisType['icon'];
image?: BaseVisType['image'];
typeTitle?: BaseVisType['title'];
error?: string;
} = {
id,
references,
url: urlFor(id),
updatedAt,
...attributes,
};
let typeName = attributes.typeName;
if (attributes.visState) {
try {
typeName = JSON.parse(String(attributes.visState)).type;
} catch (e) {
/* missing typename handled below */
}
}
if (!typeName || !visTypes.get(typeName as string)) {
newAttributes.error = 'Unknown visualization type';
return newAttributes;
}
newAttributes.type = visTypes.get(typeName as string);
newAttributes.savedObjectType = 'visualization';
newAttributes.icon = newAttributes.type?.icon;
newAttributes.image = newAttributes.type?.image;
newAttributes.typeTitle = newAttributes.type?.title;
newAttributes.editUrl = `/edit/${id}`;
return newAttributes;
}
export const convertToSerializedVis = (savedVis: VisSavedObject): SerializedVis => {
const { id, title, description, visState, uiStateJSON, searchSourceFields } = savedVis;
const aggs = searchSourceFields && searchSourceFields.index ? visState.aggs || [] : visState.aggs;
return {
id,
title,
type: visState.type,
description,
params: visState.params,
uiState: JSON.parse(uiStateJSON || '{}'),
data: {
aggs,
searchSource: searchSourceFields!,
savedSearchId: savedVis.savedSearchId,
},
};
};
export const convertFromSerializedVis = (vis: SerializedVis): ISavedVis => {
return {
id: vis.id,
title: vis.title,
description: vis.description,
visState: {
title: vis.title,
type: vis.type,
aggs: vis.data.aggs,
params: vis.params,
},
uiStateJSON: JSON.stringify(vis.uiState),
searchSourceFields: vis.data.searchSource,
savedSearchId: vis.data.savedSearchId,
};
};
export async function findListItems(
savedObjectsClient: SavedObjectsClientContract,
visTypes: Pick<TypesStart, 'get' | 'getAliases'>,
search: string,
size: number,
references?: SavedObjectsFindOptionsReference[]
) {
const visAliases = visTypes.getAliases();
const extensions = visAliases
.map((v) => v.appExtensions?.visualizations)
.filter(Boolean) as VisualizationsAppExtension[];
const extensionByType = extensions.reduce((acc, m) => {
return m!.docTypes.reduce((_acc, type) => {
acc[type] = m;
return acc;
}, acc);
}, {} as { [visType: string]: VisualizationsAppExtension });
const searchOption = (field: string, ...defaults: string[]) =>
_(extensions).map(field).concat(defaults).compact().flatten().uniq().value() as string[];
const searchOptions: SavedObjectsFindOptions = {
type: searchOption('docTypes', 'visualization'),
searchFields: searchOption('searchFields', 'title^3', 'description'),
search: search ? `${search}*` : undefined,
perPage: size,
page: 1,
defaultSearchOperator: 'AND' as 'AND',
hasReference: references,
};
const { total, savedObjects } = await savedObjectsClient.find<SavedObjectAttributes>(
searchOptions
);
return {
total,
hits: savedObjects.map((savedObject) => {
const config = extensionByType[savedObject.type];
if (config) {
return {
...config.toListItem(savedObject),
references: savedObject.references,
};
} else {
return mapHitSource(visTypes, savedObject);
}
}),
};
}
export async function getSavedVisualization(
services: {
savedObjectsClient: SavedObjectsClientContract;
search: DataPublicPluginStart['search'];
dataViews: DataPublicPluginStart['dataViews'];
spaces?: SpacesPluginStart;
savedObjectsTagging?: SavedObjectsTaggingApi;
},
opts?: GetVisOptions | string
): Promise<VisSavedObject> {
if (typeof opts !== 'object') {
opts = { id: opts } as GetVisOptions;
}
const id = (opts.id as string) || '';
const savedObject = {
id,
migrationVersion: opts.migrationVersion,
displayName: SAVED_VIS_TYPE,
getEsType: () => SAVED_VIS_TYPE,
getDisplayName: () => SAVED_VIS_TYPE,
searchSource: opts.searchSource ? services.search.searchSource.createEmpty() : undefined,
} as VisSavedObject;
const defaultsProps = getDefaults(opts);
if (!id) {
Object.assign(savedObject, defaultsProps);
return savedObject;
}
const {
saved_object: resp,
outcome,
alias_target_id: aliasTargetId,
alias_purpose: aliasPurpose,
} = await services.savedObjectsClient.resolve<SavedObjectAttributes>(SAVED_VIS_TYPE, id);
if (!resp._version) {
throw new SavedObjectNotFound(SAVED_VIS_TYPE, id || '');
}
const attributes = _.cloneDeep(resp.attributes);
if (attributes.visState && typeof attributes.visState === 'string') {
attributes.visState = JSON.parse(attributes.visState);
}
// assign the defaults to the response
_.defaults(attributes, defaultsProps);
Object.assign(savedObject, attributes);
savedObject.lastSavedTitle = savedObject.title;
savedObject.sharingSavedObjectProps = {
aliasTargetId,
outcome,
aliasPurpose,
errorJSON:
outcome === 'conflict' && services.spaces
? JSON.stringify({
targetType: SAVED_VIS_TYPE,
sourceId: id,
targetSpace: (await services.spaces.getActiveSpace()).id,
})
: undefined,
};
const meta = (attributes.kibanaSavedObjectMeta || {}) as SavedObjectAttributes;
if (meta.searchSourceJSON) {
try {
let searchSourceValues = parseSearchSourceJSON(meta.searchSourceJSON as string);
if (opts.searchSource) {
searchSourceValues = injectSearchSourceReferences(
searchSourceValues as any,
resp.references
);
savedObject.searchSource = await services.search.searchSource.create(searchSourceValues);
} else {
savedObject.searchSourceFields = searchSourceValues;
}
} catch (error: any) {
throw error;
}
}
if (resp.references && resp.references.length > 0) {
injectReferences(savedObject, resp.references);
}
if (services.savedObjectsTagging) {
savedObject.tags = services.savedObjectsTagging.ui.getTagIdsFromReferences(resp.references);
}
savedObject.visState = await updateOldState(savedObject.visState);
return savedObject;
}
export async function saveVisualization(
savedObject: VisSavedObject,
{
confirmOverwrite = false,
isTitleDuplicateConfirmed = false,
onTitleDuplicate,
copyOnSave = false,
}: SaveVisOptions,
services: {
savedObjectsClient: SavedObjectsClientContract;
overlays: OverlayStart;
savedObjectsTagging?: SavedObjectsTaggingApi;
}
) {
// Save the original id in case the save fails.
const originalId = savedObject.id;
// Read https://github.com/elastic/kibana/issues/9056 and
// https://github.com/elastic/kibana/issues/9012 for some background into why this copyOnSave variable
// exists.
// The goal is to move towards a better rename flow, but since our users have been conditioned
// to expect a 'save as' flow during a rename, we are keeping the logic the same until a better
// UI/UX can be worked out.
if (copyOnSave) {
delete savedObject.id;
}
const attributes: SavedObjectAttributes = {
visState: JSON.stringify(savedObject.visState),
title: savedObject.title,
uiStateJSON: savedObject.uiStateJSON,
description: savedObject.description,
savedSearchId: savedObject.savedSearchId,
version: savedObject.version,
};
let references: SavedObjectReference[] = [];
if (savedObject.searchSource) {
const { searchSourceJSON, references: searchSourceReferences } =
savedObject.searchSource.serialize();
attributes.kibanaSavedObjectMeta = { searchSourceJSON };
references.push(...searchSourceReferences);
}
if (savedObject.searchSourceFields) {
const [searchSourceFields, searchSourceReferences] = extractSearchSourceReferences(
savedObject.searchSourceFields
);
const searchSourceJSON = JSON.stringify(searchSourceFields);
attributes.kibanaSavedObjectMeta = { searchSourceJSON };
references.push(...searchSourceReferences);
}
if (services.savedObjectsTagging) {
references = services.savedObjectsTagging.ui.updateTagsReferences(
references,
savedObject.tags || []
);
}
const extractedRefs = extractReferences({ attributes, references });
if (!extractedRefs.references) {
throw new Error('References not returned from extractReferences');
}
try {
await checkForDuplicateTitle(
savedObject,
copyOnSave,
isTitleDuplicateConfirmed,
onTitleDuplicate,
services as any
);
const createOpt = {
id: savedObject.id,
migrationVersion: savedObject.migrationVersion,
references: extractedRefs.references,
};
const resp = confirmOverwrite
? await saveWithConfirmation(attributes, savedObject, createOpt, services)
: await services.savedObjectsClient.create(SAVED_VIS_TYPE, extractedRefs.attributes, {
...createOpt,
overwrite: true,
});
savedObject.id = resp.id;
savedObject.lastSavedTitle = savedObject.title;
return savedObject.id;
} catch (err: any) {
savedObject.id = originalId;
if (err && [OVERWRITE_REJECTED, SAVE_DUPLICATE_REJECTED].includes(err.message)) {
return '';
}
return Promise.reject(err);
}
}
export const shouldShowMissedDataViewError = (error: Error): error is SavedObjectNotFound =>
error instanceof SavedObjectNotFound && error.savedObjectType === 'data view';