Skip to content

Commit

Permalink
Fix saving multiple metrics to SOpanels (#407) (#408)
Browse files Browse the repository at this point in the history
* fix saving multiple metrics to SOpanels

Signed-off-by: Shenoy Pratik <[email protected]>

* remove unused variable

Signed-off-by: Shenoy Pratik <[email protected]>

---------

Signed-off-by: Shenoy Pratik <[email protected]>
(cherry picked from commit a4cb6d1)

Co-authored-by: Shenoy Pratik <[email protected]>
  • Loading branch information
2 people authored and Peter Fitzgibbons committed Apr 20, 2023
1 parent 6832a76 commit e4593a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
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 @@ -158,6 +161,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

0 comments on commit e4593a1

Please sign in to comment.