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

[Backport 2.7] Fix saving multiple metrics to SOpanels #408

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,30 @@ export const addVisualizationPanel = (
throw new Error('Add/Replace Visualization Error:' + error);
}
};

// Add Multiple visualizations in a Panel
export const addMultipleVisualizations = (
savedVisualizationIds: string[],
allPanelVisualizations: VisualizationType[]
) => {
try {
let newDimensions;
let visualizationsList = [...allPanelVisualizations];

savedVisualizationIds.map((savedVisualizationId) => {
newDimensions = getNewVizDimensions(visualizationsList);
visualizationsList = [
...visualizationsList,
{
id: 'panel_viz_' + uuidv4(),
savedVisualizationId,
...newDimensions,
},
];
});

return visualizationsList;
} catch (error) {
throw new Error('Add Multiple Visualization Error:' + error);
}
};
22 changes: 21 additions & 1 deletion public/components/custom_panels/redux/panel_slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
import { coreRefs } from '../../../framework/core_refs';
import { SavedObject, SimpleSavedObject } from '../../../../../../src/core/public';
import { isNameValid } from '../helpers/utils';
import { addVisualizationPanel } from '../helpers/add_visualization_helper';
import {
addMultipleVisualizations,
addVisualizationPanel,
} from '../helpers/add_visualization_helper';

interface InitialState {
id: string;
Expand Down Expand Up @@ -163,6 +166,23 @@ export const addVizToPanels = (panels, vizId) => async (dispatch, getState) => {
});
};

export const addMultipleVizToPanels = (panels, vizIds) => async (dispatch, getState) => {
forEach(panels, (oldPanel) => {
const panel = getState().customPanel.panelList.find((p) => p.id === oldPanel.panel.id);

const allVisualizations = panel!.visualizations;

const visualizationsWithNewPanel = addMultipleVisualizations(vizIds, allVisualizations);

const updatedPanel = { ...panel, visualizations: visualizationsWithNewPanel };
try {
dispatch(updatePanel(updatedPanel));
} catch (err) {
console.error(err?.body?.message || err);
}
});
};

export const replaceVizInPanel = (oldPanel, oldVizId, vizId) => async (dispatch, getState) => {
const panel = getState().customPanel.panelList.find((p) => p.id === oldPanel.id);

Expand Down
8 changes: 2 additions & 6 deletions public/components/metrics/top_menu/top_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { resolutionOptions } from '../../../../common/constants/metrics';
import { MetricType } from '../../../../common/types/metrics';
import { uiSettingsService } from '../../../../common/utils';
import SavedObjects from '../../../services/saved_objects/event_analytics/saved_objects';
import { addVizToPanels, uuidRx } from '../../custom_panels/redux/panel_slice';
import { sortMetricLayout, updateMetricsWithSelections } from '../helpers/utils';
import {
allAvailableMetricsSelector,
Expand All @@ -37,6 +36,7 @@ import {
import { SearchBar } from '../sidebar/search_bar';
import { MetricsExportPanel } from './metrics_export_panel';
import './top_menu.scss';
import { addMultipleVizToPanels, uuidRx } from '../../custom_panels/redux/panel_slice';

interface TopMenuProps {
http: CoreStart['http'];
Expand Down Expand Up @@ -158,7 +158,6 @@ export const TopMenu = ({

const handleSavingObjects = async () => {
let savedMetricIds = [];
const savedMetricsInPanels = [];

try {
savedMetricIds = await Promise.all(
Expand Down Expand Up @@ -194,6 +193,7 @@ export const TopMenu = ({
const soPanels = selectedPanelOptions.filter((panel) => uuidRx.test(panel.panel.id));
const opsPanels = selectedPanelOptions.filter((panel) => !uuidRx.test(panel.panel.id));

dispatch(addMultipleVizToPanels(soPanels, allMetricIds));
const savedMetricsInOpsPanels = await Promise.all(
opsPanels.map((panel) => {
return http.post(`${CUSTOM_PANELS_API_PREFIX}/visualizations/multiple`, {
Expand All @@ -204,10 +204,6 @@ export const TopMenu = ({
});
})
);

allMetricIds.forEach((metricId) => {
dispatch(addVizToPanels(soPanels, metricId));
});
} catch (e) {
const message = 'Issue in saving metrics to panels';
console.error(message, e);
Expand Down