-
Notifications
You must be signed in to change notification settings - Fork 57
/
custom_panel_adaptor.ts
464 lines (429 loc) · 14.4 KB
/
custom_panel_adaptor.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { v4 as uuidv4 } from 'uuid';
import { PanelType, VisualizationType } from '../../../common/types/custom_panels';
import { ILegacyScopedClusterClient } from '../../../../../src/core/server';
import { createDemoPanel } from '../../common/helpers/custom_panels/sample_panels';
interface boxType {
x1: number;
y1: number;
x2: number;
y2: number;
}
export class CustomPanelsAdaptor {
// index a panel
indexPanel = async function (
client: ILegacyScopedClusterClient,
panelBody: PanelType
): Promise<{ objectId: string }> {
try {
const response = await client.callAsCurrentUser('observability.createObject', {
body: {
operationalPanel: panelBody,
},
});
return response;
} catch (error) {
throw new Error('Index Panel Error:' + error);
}
};
// update a panel
updatePanel = async function (
client: ILegacyScopedClusterClient,
panelId: string,
updatePanelBody: Partial<PanelType>
) {
try {
const response = await client.callAsCurrentUser('observability.updateObjectById', {
objectId: panelId,
body: {
operationalPanel: updatePanelBody,
},
});
return response;
} catch (error) {
throw new Error('Update Panel Error:' + error);
}
};
// fetch a panel by id
getPanel = async function (client: ILegacyScopedClusterClient, panelId: string) {
try {
const response = await client.callAsCurrentUser('observability.getObjectById', {
objectId: panelId,
});
return response.observabilityObjectList[0];
} catch (error) {
throw new Error('Get Panel Error:' + error);
}
};
// gets list of panels stored in index
viewPanelList = async function (client: ILegacyScopedClusterClient) {
try {
const response = await client.callAsCurrentUser('observability.getObject', {
objectType: 'operationalPanel',
maxItems: 10000,
});
return response.observabilityObjectList
.filter((panel: any) => !panel.operationalPanel.applicationId)
.map((panel: any) => ({
name: panel.operationalPanel.name,
id: panel.objectId,
dateCreated: panel.createdTimeMs,
dateModified: panel.lastUpdatedTimeMs,
}));
} catch (error) {
throw new Error('View Panel List Error:' + error);
}
};
// Delete a panel by Id
deletePanel = async function (client: ILegacyScopedClusterClient, panelId: string) {
try {
const response = await client.callAsCurrentUser('observability.deleteObjectById', {
objectId: panelId,
});
return { status: 'OK', message: response };
} catch (error) {
throw new Error('Delete Panel Error:' + error);
}
};
// Delete a panel by Id
deletePanelList = async function (client: ILegacyScopedClusterClient, panelIdList: string) {
try {
const response = await client.callAsCurrentUser('observability.deleteObjectByIdList', {
objectIdList: panelIdList,
});
return { status: 'OK', message: response };
} catch (error) {
throw new Error('Delete Panel List Error:' + error);
}
};
// Create a new Panel
createNewPanel = async (
client: ILegacyScopedClusterClient,
panelName: string,
appId?: string
) => {
const panelBody: PanelType = {
name: panelName,
visualizations: [],
timeRange: {
to: 'now',
from: 'now-1d',
},
queryFilter: {
query: '',
language: 'ppl',
},
};
if (appId) {
panelBody.applicationId = appId;
panelBody.timeRange = {
to: 'now',
from: 'now-24h',
};
}
try {
const response = await this.indexPanel(client, panelBody);
return response.objectId;
} catch (error) {
throw new Error('Create Panel Error:' + error);
}
};
// Rename an existing panel
renamePanel = async (client: ILegacyScopedClusterClient, panelId: string, panelName: string) => {
const updatePanelBody = {
name: panelName,
};
try {
const response = await this.updatePanel(client, panelId, updatePanelBody);
return response.objectId;
} catch (error) {
throw new Error('Rename Panel Error:' + error);
}
};
// Clone an existing panel
clonePanel = async (client: ILegacyScopedClusterClient, panelId: string, panelName: string) => {
const updatePanelBody = {
name: panelName,
};
try {
const getPanel = await this.getPanel(client, panelId);
const clonePanelBody = {
name: panelName,
visualizations: getPanel.operationalPanel.visualizations,
timeRange: getPanel.operationalPanel.timeRange,
queryFilter: getPanel.operationalPanel.queryFilter,
};
const indexResponse = await this.indexPanel(client, clonePanelBody);
const getClonedPanel = await this.getPanel(client, indexResponse.objectId);
return {
clonePanelId: getClonedPanel.objectId,
dateCreated: getClonedPanel.createdTimeMs,
dateModified: getClonedPanel.lastUpdatedTimeMs,
};
} catch (error) {
throw new Error('Clone Panel Error:' + error);
}
};
// Add filters to an existing panel
addPanelFilter = async (
client: ILegacyScopedClusterClient,
panelId: string,
query: string,
language: string,
to: string,
from: string
) => {
const updatePanelBody = {
timeRange: {
to,
from,
},
queryFilter: {
query,
language,
},
};
try {
const response = await this.updatePanel(client, panelId, updatePanelBody);
return response.objectId;
} catch (error) {
throw new Error('Add Panel Filter Error:' + error);
}
};
// parses fetched saved visualization
parseSavedVisualizations = (visualization: any) => {
return {
id: visualization.objectId,
name: visualization.savedVisualization.name,
query: visualization.savedVisualization.query,
type: visualization.savedVisualization.type,
timeField: visualization.savedVisualization.selected_timestamp.name,
selected_date_range: visualization.savedVisualization.selected_date_range,
selected_fields: visualization.savedVisualization.selected_fields,
user_configs: visualization.savedVisualization.hasOwnProperty('user_configs')
? JSON.parse(visualization.savedVisualization.user_configs)
: {},
sub_type: visualization.savedVisualization.hasOwnProperty('sub_type')
? visualization.savedVisualization.sub_type
: '',
units_of_measure: visualization.savedVisualization.hasOwnProperty('units_of_measure')
? visualization.savedVisualization.units_of_measure
: '',
...(visualization.savedVisualization.application_id
? { application_id: visualization.savedVisualization.application_id }
: {}),
};
};
// gets all saved visualizations
getAllSavedVisualizations = async (client: ILegacyScopedClusterClient) => {
try {
const response = await client.callAsCurrentUser('observability.getObject', {
objectType: 'savedVisualization',
});
return response.observabilityObjectList.map((visualization: any) =>
this.parseSavedVisualizations(visualization)
);
} catch (error) {
throw new Error('View Saved Visualizations Error:' + error);
}
};
// gets list of savedVisualizations by Id
getSavedVisualizationById = async (
client: ILegacyScopedClusterClient,
savedVisualizationId: string
) => {
try {
const response = await client.callAsCurrentUser('observability.getObjectById', {
objectId: savedVisualizationId,
});
const visualization = response.observabilityObjectList[0];
return this.parseSavedVisualizations(visualization);
} catch (error) {
throw new Error('Fetch Saved Visualizations By Id Error:' + error);
}
};
// Get All Visualizations from a Panel
getVisualizations = async (client: ILegacyScopedClusterClient, panelId: string) => {
try {
const response = await client.callAsCurrentUser('observability.getObjectById', {
objectId: panelId,
});
return response.observabilityObjectList[0].operationalPanel.visualizations;
} catch (error) {
throw new Error('Get Visualizations Error:' + error);
}
};
calculatOverlapArea = (bb1: boxType, bb2: boxType) => {
const x_left = Math.max(bb1.x1, bb2.x1);
const y_top = Math.max(bb1.y1, bb2.y1);
const x_right = Math.min(bb1.x2, bb2.x2);
const y_bottom = Math.min(bb1.y2, bb2.y2);
if (x_right < x_left || y_bottom < y_top) return 0;
return (x_right - x_left) * (y_bottom - y_top);
};
getTotalOverlapArea = (panelVisualizations: VisualizationType[]) => {
const newVizBox = { x1: 0, y1: 0, x2: 6, y2: 4 };
const currentVizBoxes = panelVisualizations.map((visualization) => {
return {
x1: visualization.x,
y1: visualization.y,
x2: visualization.x + visualization.w,
y2: visualization.y + visualization.h,
};
});
let isOverlapping = 0;
currentVizBoxes.map((viz) => {
isOverlapping += this.calculatOverlapArea(viz, newVizBox);
});
return isOverlapping;
};
// We want to check if the new visualization being added, can be placed at { x: 0, y: 0, w: 6, h: 4 };
// To check this we try to calculate overlap between all the current visualizations and new visualization
// if there is no overalap (i.e Total Overlap Area is 0), we place the new viz. in default position
// else, we add it to the bottom of the panel
getNewVizDimensions = (panelVisualizations: VisualizationType[]) => {
let maxY: number = 0;
let maxYH: number = 0;
// check if we can place the new visualization at default location
if (this.getTotalOverlapArea(panelVisualizations) === 0) {
return { x: 0, y: 0, w: 6, h: 4 };
}
// else place the new visualization at the bottom of the panel
panelVisualizations.map((panelVisualization: VisualizationType) => {
if (panelVisualization.y >= maxY) {
maxY = panelVisualization.y;
maxYH = panelVisualization.h;
}
});
return { x: 0, y: maxY + maxYH, w: 6, h: 4 };
};
// Add Visualization in the Panel
addVisualization = async (
client: ILegacyScopedClusterClient,
panelId: string,
savedVisualizationId: string,
oldVisualizationId?: string
) => {
try {
const allPanelVisualizations = await this.getVisualizations(client, panelId);
let newDimensions;
let visualizationsList = [] as VisualizationType[];
if (oldVisualizationId === undefined) {
newDimensions = this.getNewVizDimensions(allPanelVisualizations);
visualizationsList = allPanelVisualizations;
} else {
allPanelVisualizations.map((visualization: VisualizationType) => {
if (visualization.id !== oldVisualizationId) {
visualizationsList.push(visualization);
} else {
newDimensions = {
x: visualization.x,
y: visualization.y,
w: visualization.w,
h: visualization.h,
};
}
});
}
const newPanelVisualizations = [
...visualizationsList,
{
id: 'panel_viz_' + uuidv4(),
savedVisualizationId,
...newDimensions,
},
];
const updatePanelResponse = await this.updatePanel(client, panelId, {
visualizations: newPanelVisualizations,
});
return newPanelVisualizations;
} catch (error) {
throw new Error('Add/Replace Visualization Error:' + error);
}
};
// Add Multiple visualizations in a Panel
addMultipleVisualizations = async (
client: ILegacyScopedClusterClient,
panelId: string,
savedVisualizationIds: string[]
) => {
try {
let allPanelVisualizations = await this.getVisualizations(client, panelId);
let newDimensions;
let visualizationsList = [...allPanelVisualizations];
savedVisualizationIds.map((savedVisualizationId) => {
newDimensions = this.getNewVizDimensions(visualizationsList);
visualizationsList = [
...visualizationsList,
{
id: 'panel_viz_' + uuidv4(),
savedVisualizationId,
...newDimensions,
},
];
});
const updatePanelResponse = await this.updatePanel(client, panelId, {
visualizations: visualizationsList,
});
return visualizationsList;
} catch (error) {
throw new Error('Add/Replace Visualization Error:' + error);
}
};
// Edits all Visualizations in the Panel
editVisualization = async (
client: ILegacyScopedClusterClient,
panelId: string,
visualizationParams: Array<{
i: string;
x: number;
y: number;
w: number;
h: number;
}>
) => {
try {
const allPanelVisualizations = await this.getVisualizations(client, panelId);
const filteredPanelVisualizations = [] as VisualizationType[];
for (let i = 0; i < allPanelVisualizations.length; i++) {
for (let j = 0; j < visualizationParams.length; j++) {
if (allPanelVisualizations[i].id === visualizationParams[j].i) {
filteredPanelVisualizations.push({
...allPanelVisualizations[i],
x: visualizationParams[j].x,
y: visualizationParams[j].y,
w: visualizationParams[j].w,
h: visualizationParams[j].h,
});
}
}
}
const updatePanelResponse = await this.updatePanel(client, panelId, {
visualizations: filteredPanelVisualizations,
});
return filteredPanelVisualizations;
} catch (error) {
throw new Error('Edit Visualizations Error:' + error);
}
};
// Create Sample Panels
addSamplePanels = async (client: ILegacyScopedClusterClient, savedVisualizationIds: string[]) => {
try {
const panelBody = createDemoPanel(savedVisualizationIds);
const indexResponse = await this.indexPanel(client, panelBody);
const fetchPanel = await this.getPanel(client, indexResponse.objectId);
const fetchResponse = {
name: fetchPanel.operationalPanel.name,
id: fetchPanel.objectId,
dateCreated: fetchPanel.createdTimeMs,
dateModified: fetchPanel.lastUpdatedTimeMs,
};
return [fetchResponse];
} catch (error) {
throw new Error('Create Panel Error:' + error);
}
};
}