Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change samples to saved object #427

Merged
merged 9 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions public/components/custom_panels/helpers/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const isNameValid = (name: string) => {
return name.length >= 50 || name.length === 0 ? false : true;
};

export const samplePanelName = '[Logs] Web traffic Panel';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: should we move this to common/constants?


// DateTime convertor to required format
export const convertDateTime = (datetime: string, isStart = true, formatted = true) => {
let returnTime: undefined | Moment;
Expand Down
20 changes: 10 additions & 10 deletions public/components/custom_panels/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ import PPLService from '../../services/requests/ppl';
import { CustomPanelTable } from './custom_panel_table';
import { CustomPanelView } from './custom_panel_view';
import { CustomPanelViewSO } from './custom_panel_view_so';
import { deletePanel, fetchPanels, uuidRx } from './redux/panel_slice';
import {
createPanel,
createPanelSample,
createPanelWithVizs,
deletePanel,
fetchPanels,
newPanelTemplate,
uuidRx,
} from './redux/panel_slice';

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

Expand Down Expand Up @@ -182,15 +190,7 @@ export const Home = ({
.get(`${OBSERVABILITY_BASE}${EVENT_ANALYTICS}${SAVED_OBJECTS}/addSampleSavedObjects/panels`)
.then((resp) => (savedVisualizationIds = [...resp.savedVizIds]));

await http
.post(`${CUSTOM_PANELS_API_PREFIX}/panels/addSamplePanels`, {
body: JSON.stringify({
savedVisualizationIds,
}),
})
.then((res) => {
dispatch(fetchPanels());
});
dispatch(createPanelSample(savedVisualizationIds));
setToast(`Sample panels successfully added.`);
} catch (err: any) {
setToast('Error adding sample panels.', 'danger');
Expand Down
16 changes: 15 additions & 1 deletion public/components/custom_panels/redux/panel_slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import {
} from '../../../../common/types/custom_panels';
import { coreRefs } from '../../../framework/core_refs';
import { SavedObject, SimpleSavedObject } from '../../../../../../src/core/public';
import { isNameValid } from '../helpers/utils';
import { isNameValid, samplePanelName } from '../helpers/utils';
import {
addMultipleVisualizations,
addVisualizationPanel,
} from '../helpers/add_visualization_helper';
import { createDemoPanel } from '../../../../server/common/helpers/custom_panels/sample_panels';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to /common/constants/utils? Cause, the createDemoPanel function is defined in the server whereas it is being used in public component.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, +1, please relocate createDemoPanel fn

Copy link
Collaborator Author

@derek-ho derek-ho Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also being used in server here:

import { createDemoPanel } from '../../common/helpers/custom_panels/sample_panels';
@pjfitzgibbons @ps48 take a look and let me know what you think. I think the original use case is to use it in the server, I am just using it as well to create the SO panel


interface InitialState {
id: string;
Expand Down Expand Up @@ -218,6 +219,19 @@ export const createPanel = (panel) => async (dispatch, getState) => {
window.location.replace(`#/${newPanel.id}`);
};

export const createPanelSample = (vizIds) => async (dispatch, getState) => {
const samplePanel = {
...createDemoPanel(vizIds),
dateCreated: new Date().getTime(),
dateModified: new Date().getTime(),
title: samplePanelName,
};
const newSOPanel = await savedObjectPanelsClient.create(samplePanel);
const newPanel = savedObjectToCustomPanel(newSOPanel);
const panelList = getState().customPanel.panelList;
dispatch(setPanelList([...panelList, newPanel]));
};

export const clonePanel = (panel, newPanelName) => async (dispatch, getState) => {
const { id, ...panelCopy } = {
...panel,
Expand Down