Skip to content

Commit ce06ccb

Browse files
authored
change samples to saved object (opensearch-project#427)
* change samples to saved object Signed-off-by: Derek Ho <dxho@amazon.com> * add sample to so Signed-off-by: Derek Ho <dxho@amazon.com> * fix saved object sample Signed-off-by: Derek Ho <dxho@amazon.com> * fix up Signed-off-by: Derek Ho <dxho@amazon.com> * add back toast Signed-off-by: Derek Ho <dxho@amazon.com> * revert file Signed-off-by: Derek Ho <dxho@amazon.com> * refactoring Signed-off-by: Derek Ho <dxho@amazon.com> * use constant Signed-off-by: Derek Ho <dxho@amazon.com> --------- Signed-off-by: Derek Ho <dxho@amazon.com>
1 parent 080f7f7 commit ce06ccb

File tree

5 files changed

+104
-91
lines changed

5 files changed

+104
-91
lines changed

common/constants/custom_panels.ts

+78
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import { v4 as uuidv4 } from 'uuid';
7+
68
export const CUSTOM_PANELS_API_PREFIX = '/api/observability/operational_panels';
79
export const CUSTOM_PANELS_DOCUMENTATION_URL =
810
'https://opensearch.org/docs/latest/observability-plugin/operational-panels/';
@@ -11,3 +13,79 @@ export const CREATE_PANEL_MESSAGE = 'Enter a name to describe the purpose of thi
1113
export const CUSTOM_PANELS_SAVED_OBJECT_TYPE = 'observability-panel';
1214

1315
export const CUSTOM_PANEL_SLICE = 'customPanel';
16+
17+
export const samplePanelName = '[Logs] Web traffic Panel';
18+
19+
export const createDemoPanel = (savedVisualizationIds: string[]) => {
20+
return {
21+
name: samplePanelName,
22+
visualizations: [
23+
{
24+
id: 'panel_viz_' + uuidv4(),
25+
savedVisualizationId: savedVisualizationIds[0],
26+
x: 4,
27+
y: 6,
28+
w: 8,
29+
h: 2,
30+
},
31+
{
32+
id: 'panel_viz_' + uuidv4(),
33+
savedVisualizationId: savedVisualizationIds[1],
34+
x: 0,
35+
y: 2,
36+
w: 12,
37+
h: 2,
38+
},
39+
{
40+
id: 'panel_viz_' + uuidv4(),
41+
savedVisualizationId: savedVisualizationIds[2],
42+
x: 0,
43+
y: 0,
44+
w: 4,
45+
h: 2,
46+
},
47+
{
48+
id: 'panel_viz_' + uuidv4(),
49+
savedVisualizationId: savedVisualizationIds[3],
50+
x: 4,
51+
y: 0,
52+
w: 4,
53+
h: 2,
54+
},
55+
{
56+
id: 'panel_viz_' + uuidv4(),
57+
savedVisualizationId: savedVisualizationIds[4],
58+
x: 8,
59+
y: 0,
60+
w: 4,
61+
h: 2,
62+
},
63+
{
64+
id: 'panel_viz_' + uuidv4(),
65+
savedVisualizationId: savedVisualizationIds[5],
66+
x: 0,
67+
y: 4,
68+
w: 4,
69+
h: 2,
70+
},
71+
{
72+
id: 'panel_viz_' + uuidv4(),
73+
savedVisualizationId: savedVisualizationIds[6],
74+
x: 0,
75+
y: 6,
76+
w: 4,
77+
h: 2,
78+
},
79+
{
80+
id: 'panel_viz_' + uuidv4(),
81+
savedVisualizationId: savedVisualizationIds[7],
82+
x: 4,
83+
y: 4,
84+
w: 8,
85+
h: 2,
86+
},
87+
],
88+
timeRange: { to: 'now/y', from: 'now/y' },
89+
queryFilter: { query: '', language: 'ppl' },
90+
};
91+
};

public/components/custom_panels/home.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ import PPLService from '../../services/requests/ppl';
2828
import { CustomPanelTable } from './custom_panel_table';
2929
import { CustomPanelView } from './custom_panel_view';
3030
import { CustomPanelViewSO } from './custom_panel_view_so';
31-
import { fetchPanels, uuidRx } from './redux/panel_slice';
31+
import {
32+
createPanel,
33+
createPanelSample,
34+
createPanelWithVizs,
35+
deletePanel,
36+
fetchPanels,
37+
newPanelTemplate,
38+
uuidRx,
39+
} from './redux/panel_slice';
3240

3341
// import { ObjectFetcher } from '../common/objectFetcher';
3442

@@ -123,15 +131,7 @@ export const Home = ({
123131
.get(`${OBSERVABILITY_BASE}${EVENT_ANALYTICS}${SAVED_OBJECTS}/addSampleSavedObjects/panels`)
124132
.then((resp) => (savedVisualizationIds = [...resp.savedVizIds]));
125133

126-
await http
127-
.post(`${CUSTOM_PANELS_API_PREFIX}/panels/addSamplePanels`, {
128-
body: JSON.stringify({
129-
savedVisualizationIds,
130-
}),
131-
})
132-
.then((res) => {
133-
dispatch(fetchPanels());
134-
});
134+
dispatch(createPanelSample(savedVisualizationIds));
135135
setToast(`Sample panels successfully added.`);
136136
} catch (err: any) {
137137
setToast('Error adding sample panels.', 'danger');

public/components/custom_panels/redux/panel_slice.ts

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
CUSTOM_PANELS_API_PREFIX,
1212
CUSTOM_PANELS_SAVED_OBJECT_TYPE,
1313
CUSTOM_PANEL_SLICE,
14+
createDemoPanel,
1415
} from '../../../../common/constants/custom_panels';
1516
import {
1617
CustomPanelListType,
@@ -22,6 +23,7 @@ import {
2223
import { coreRefs } from '../../../framework/core_refs';
2324
import { SavedObject, SimpleSavedObject } from '../../../../../../src/core/public';
2425
import { isNameValid } from '../helpers/utils';
26+
import { samplePanelName } from '../../../../common/constants/custom_panels';
2527
import {
2628
addMultipleVisualizations,
2729
addVisualizationPanel,
@@ -230,6 +232,19 @@ export const createPanel = (panel) => async (dispatch, getState) => {
230232
window.location.replace(`#/${newPanel.id}`);
231233
};
232234

235+
export const createPanelSample = (vizIds) => async (dispatch, getState) => {
236+
const samplePanel = {
237+
...createDemoPanel(vizIds),
238+
dateCreated: new Date().getTime(),
239+
dateModified: new Date().getTime(),
240+
title: samplePanelName,
241+
};
242+
const newSOPanel = await savedObjectPanelsClient.create(samplePanel);
243+
const newPanel = savedObjectToCustomPanel(newSOPanel);
244+
const panelList = getState().customPanel.panelList;
245+
dispatch(setPanelList([...panelList, newPanel]));
246+
};
247+
233248
export const clonePanel = (panel, newPanelName) => async (dispatch, getState) => {
234249
const { id, ...panelCopy } = {
235250
...panel,

server/adaptors/custom_panels/custom_panel_adaptor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { v4 as uuidv4 } from 'uuid';
77
import { PanelType, VisualizationType } from '../../../common/types/custom_panels';
88
import { ILegacyScopedClusterClient } from '../../../../../src/core/server';
9-
import { createDemoPanel } from '../../common/helpers/custom_panels/sample_panels';
9+
import { createDemoPanel } from '../../../common/constants/custom_panels';
1010

1111
interface boxType {
1212
x1: number;

server/common/helpers/custom_panels/sample_panels.ts

-80
This file was deleted.

0 commit comments

Comments
 (0)