Skip to content

Commit 1ecd7ea

Browse files
authored
Metrics analytics support for SOpanels (opensearch-project#386)
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
1 parent 23e09e5 commit 1ecd7ea

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

.cypress/integration/2_notebooks.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ describe('Testing paragraphs', () => {
497497

498498
cy.get('.euiButton__text').contains('Create notebook').should('exist');
499499
});
500-
500+
501501
it('Cleans up test notebooks', () => {
502502
cy.get('[data-test-subj="notebook-notebook-actions-button"]').click();
503503
cy.wait(delay);

public/components/metrics/helpers/utils.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { CoreStart } from '../../../../../../src/core/public';
2020
import { MetricType } from '../../../../common/types/metrics';
2121
import { VisualizationType } from '../../../../common/types/custom_panels';
2222
import { DEFAULT_METRIC_HEIGHT, DEFAULT_METRIC_WIDTH } from '../../../../common/constants/metrics';
23-
import { UNITS_OF_MEASURE } from '../../../../common/constants/explorer';
2423
import { updateQuerySpanInterval } from '../../custom_panels/helpers/utils';
2524

2625
export const onTimeChange = (

public/components/metrics/top_menu/__tests__/metrics_export_panel.test.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ import {
1313
sampleSortedMetricsLayout,
1414
sampleVisualizationById,
1515
} from '../../../../../test/metrics_contants';
16-
import { createStore } from '@reduxjs/toolkit';
16+
import { applyMiddleware, createStore } from '@reduxjs/toolkit';
1717
import { rootReducer } from '../../../../framework/redux/reducers';
1818
import { Provider } from 'react-redux';
1919
import { HttpResponse } from '../../../../../../../src/core/public';
2020
import { MetricsExportPanel } from '../metrics_export_panel';
2121
import { EuiComboBoxOptionOption } from '@elastic/eui';
22+
import thunk from 'redux-thunk';
23+
import { coreRefs } from '../../../../framework/core_refs';
2224

2325
describe('Export Metrics Panel Component', () => {
2426
configure({ adapter: new Adapter() });
25-
const store = createStore(rootReducer);
27+
const store = createStore(rootReducer, applyMiddleware(thunk));
28+
coreRefs.savedObjectsClient.find = jest.fn(() =>
29+
Promise.resolve({
30+
savedObjects: [],
31+
then: () => Promise.resolve(),
32+
})
33+
);
2634

2735
it('renders Export Metrics Panel Component', async () => {
2836
let httpFlag = 1;

public/components/metrics/top_menu/metrics_export_panel.tsx

+14-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import {
1818
import { createPrometheusMetricById } from '../helpers/utils';
1919
import { MetricType } from '../../../../common/types/metrics';
2020
import { fetchVisualizationById } from '../../custom_panels/helpers/utils';
21+
import { useDispatch, useSelector } from 'react-redux';
22+
import {
23+
fetchPanels,
24+
selectPanelList,
25+
} from '../../../../public/components/custom_panels/redux/panel_slice';
2126

2227
interface MetricsExportPanelProps {
2328
http: CoreStart['http'];
@@ -49,14 +54,13 @@ export const MetricsExportPanel = ({
4954

5055
const [errorResponse, setErrorResponse] = useState('');
5156

52-
const getCustomPanelList = async () => {
53-
http
54-
.get(`${CUSTOM_PANELS_API_PREFIX}/panels`)
55-
.then((res: any) => {
56-
setOptions(res.panels || []);
57-
})
58-
.catch((error: any) => console.error(error));
59-
};
57+
const customPanels = useSelector(selectPanelList);
58+
59+
const dispatch = useDispatch();
60+
61+
useEffect(() => {
62+
dispatch(fetchPanels());
63+
}, []);
6064

6165
const fetchAllvisualizationsById = async () => {
6266
let tempVisualizationsMetaData = await Promise.all(
@@ -70,7 +74,6 @@ export const MetricsExportPanel = ({
7074
};
7175

7276
useEffect(() => {
73-
getCustomPanelList();
7477
fetchAllvisualizationsById();
7578
}, []);
7679

@@ -92,10 +95,10 @@ export const MetricsExportPanel = ({
9295
setSelectedPanelOptions(options);
9396
}}
9497
selectedOptions={selectedPanelOptions}
95-
options={options.map((option: CustomPanelOptions) => {
98+
options={customPanels.map((option: any) => {
9699
return {
97100
panel: option,
98-
label: option.name,
101+
label: option.title,
99102
};
100103
})}
101104
isClearable={true}

public/components/metrics/top_menu/top_menu.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import SavedObjects from '../../../services/saved_objects/event_analytics/saved_
3737
import { sortMetricLayout, updateMetricsWithSelections } from '../helpers/utils';
3838
import { CUSTOM_PANELS_API_PREFIX } from '../../../../common/constants/custom_panels';
3939
import { MetricsExportPanel } from './metrics_export_panel';
40+
import { addVizToPanels, uuidRx } from '../../custom_panels/redux/panel_slice';
4041

4142
interface TopMenuProps {
4243
http: CoreStart['http'];
@@ -191,8 +192,11 @@ export const TopMenu = ({
191192
if (selectedPanelOptions.length > 0) {
192193
try {
193194
const allMetricIds = savedMetricIds.map((metric) => metric.objectId);
194-
savedMetricsInPanels = await Promise.all(
195-
selectedPanelOptions.map((panel) => {
195+
const soPanels = selectedPanelOptions.filter((panel) => uuidRx.test(panel.panel.id));
196+
const opsPanels = selectedPanelOptions.filter((panel) => !uuidRx.test(panel.panel.id));
197+
198+
const savedMetricsInOpsPanels = await Promise.all(
199+
opsPanels.map((panel) => {
196200
return http.post(`${CUSTOM_PANELS_API_PREFIX}/visualizations/multiple`, {
197201
body: JSON.stringify({
198202
panelId: panel.panel.id,
@@ -201,6 +205,10 @@ export const TopMenu = ({
201205
});
202206
})
203207
);
208+
209+
allMetricIds.forEach((metricId) => {
210+
dispatch(addVizToPanels(soPanels, metricId));
211+
});
204212
} catch (e) {
205213
const message = 'Issue in saving metrics to panels';
206214
console.error(message, e);

0 commit comments

Comments
 (0)