Skip to content

Commit 7a9f740

Browse files
authored
change samples to saved object (#427) (#436)
* change samples to saved object * add sample to so * fix saved object sample * fix up * add back toast * revert file * refactoring * use constant --------- (cherry picked from commit ce06ccb) Signed-off-by: Derek Ho <dxho@amazon.com>
1 parent a9d1d37 commit 7a9f740

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
@@ -23,13 +23,21 @@ import PPLService from '../../services/requests/ppl';
2323
import { CustomPanelTable } from './custom_panel_table';
2424
import { CustomPanelView } from './custom_panel_view';
2525
import { CustomPanelViewSO } from './custom_panel_view_so';
26-
import { fetchPanels, uuidRx } from './redux/panel_slice';
2726
import { REDIRECT_TAB, TAB_CREATED_TYPE, TAB_ID_TXT_PFX } from '../../../common/constants/explorer';
2827
import { init as initFields } from '../event_analytics/redux/slices/field_slice';
2928
import { init as initPatterns } from '../event_analytics/redux/slices/patterns_slice';
3029
import { init as initQueryResult } from '../event_analytics/redux/slices/query_result_slice';
3130
import { changeQuery, init as initQuery } from '../event_analytics/redux/slices/query_slice';
3231
import { addTab, setSelectedQueryTab } from '../event_analytics/redux/slices/query_tab_slice';
32+
import {
33+
createPanel,
34+
createPanelSample,
35+
createPanelWithVizs,
36+
deletePanel,
37+
fetchPanels,
38+
newPanelTemplate,
39+
uuidRx,
40+
} from './redux/panel_slice';
3341
import { useToast } from '../common/toast';
3442
import { coreRefs } from '../../framework/core_refs';
3543

@@ -149,15 +157,7 @@ export const Home = ({
149157
.get(`${OBSERVABILITY_BASE}${EVENT_ANALYTICS}${SAVED_OBJECTS}/addSampleSavedObjects/panels`)
150158
.then((resp) => (savedVisualizationIds = [...resp.savedVizIds]));
151159

152-
await http
153-
.post(`${CUSTOM_PANELS_API_PREFIX}/panels/addSamplePanels`, {
154-
body: JSON.stringify({
155-
savedVisualizationIds,
156-
}),
157-
})
158-
.then((res) => {
159-
dispatch(fetchPanels());
160-
});
160+
dispatch(createPanelSample(savedVisualizationIds));
161161
setToast(`Sample panels successfully added.`);
162162
} catch (err: any) {
163163
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,
@@ -247,6 +249,19 @@ export const createPanel = (panel) => async (dispatch, getState) => {
247249
}
248250
};
249251

252+
export const createPanelSample = (vizIds) => async (dispatch, getState) => {
253+
const samplePanel = {
254+
...createDemoPanel(vizIds),
255+
dateCreated: new Date().getTime(),
256+
dateModified: new Date().getTime(),
257+
title: samplePanelName,
258+
};
259+
const newSOPanel = await savedObjectPanelsClient.create(samplePanel);
260+
const newPanel = savedObjectToCustomPanel(newSOPanel);
261+
const panelList = getState().customPanel.panelList;
262+
dispatch(setPanelList([...panelList, newPanel]));
263+
};
264+
250265
export const clonePanel = (panel, newPanelName) => async (dispatch, getState) => {
251266
try {
252267
const { id, ...panelCopy } = {

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)