Skip to content

Commit

Permalink
Metrics analytics support for SOpanels (opensearch-project#386)
Browse files Browse the repository at this point in the history
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
  • Loading branch information
ps48 authored Apr 18, 2023
1 parent 23e09e5 commit 1ecd7ea
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .cypress/integration/2_notebooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ describe('Testing paragraphs', () => {

cy.get('.euiButton__text').contains('Create notebook').should('exist');
});

it('Cleans up test notebooks', () => {
cy.get('[data-test-subj="notebook-notebook-actions-button"]').click();
cy.wait(delay);
Expand Down
1 change: 0 additions & 1 deletion public/components/metrics/helpers/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { CoreStart } from '../../../../../../src/core/public';
import { MetricType } from '../../../../common/types/metrics';
import { VisualizationType } from '../../../../common/types/custom_panels';
import { DEFAULT_METRIC_HEIGHT, DEFAULT_METRIC_WIDTH } from '../../../../common/constants/metrics';
import { UNITS_OF_MEASURE } from '../../../../common/constants/explorer';
import { updateQuerySpanInterval } from '../../custom_panels/helpers/utils';

export const onTimeChange = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ import {
sampleSortedMetricsLayout,
sampleVisualizationById,
} from '../../../../../test/metrics_contants';
import { createStore } from '@reduxjs/toolkit';
import { applyMiddleware, createStore } from '@reduxjs/toolkit';
import { rootReducer } from '../../../../framework/redux/reducers';
import { Provider } from 'react-redux';
import { HttpResponse } from '../../../../../../../src/core/public';
import { MetricsExportPanel } from '../metrics_export_panel';
import { EuiComboBoxOptionOption } from '@elastic/eui';
import thunk from 'redux-thunk';
import { coreRefs } from '../../../../framework/core_refs';

describe('Export Metrics Panel Component', () => {
configure({ adapter: new Adapter() });
const store = createStore(rootReducer);
const store = createStore(rootReducer, applyMiddleware(thunk));
coreRefs.savedObjectsClient.find = jest.fn(() =>
Promise.resolve({
savedObjects: [],
then: () => Promise.resolve(),
})
);

it('renders Export Metrics Panel Component', async () => {
let httpFlag = 1;
Expand Down
25 changes: 14 additions & 11 deletions public/components/metrics/top_menu/metrics_export_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import {
import { createPrometheusMetricById } from '../helpers/utils';
import { MetricType } from '../../../../common/types/metrics';
import { fetchVisualizationById } from '../../custom_panels/helpers/utils';
import { useDispatch, useSelector } from 'react-redux';
import {
fetchPanels,
selectPanelList,
} from '../../../../public/components/custom_panels/redux/panel_slice';

interface MetricsExportPanelProps {
http: CoreStart['http'];
Expand Down Expand Up @@ -49,14 +54,13 @@ export const MetricsExportPanel = ({

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

const getCustomPanelList = async () => {
http
.get(`${CUSTOM_PANELS_API_PREFIX}/panels`)
.then((res: any) => {
setOptions(res.panels || []);
})
.catch((error: any) => console.error(error));
};
const customPanels = useSelector(selectPanelList);

const dispatch = useDispatch();

useEffect(() => {
dispatch(fetchPanels());
}, []);

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

useEffect(() => {
getCustomPanelList();
fetchAllvisualizationsById();
}, []);

Expand All @@ -92,10 +95,10 @@ export const MetricsExportPanel = ({
setSelectedPanelOptions(options);
}}
selectedOptions={selectedPanelOptions}
options={options.map((option: CustomPanelOptions) => {
options={customPanels.map((option: any) => {
return {
panel: option,
label: option.name,
label: option.title,
};
})}
isClearable={true}
Expand Down
12 changes: 10 additions & 2 deletions public/components/metrics/top_menu/top_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import SavedObjects from '../../../services/saved_objects/event_analytics/saved_
import { sortMetricLayout, updateMetricsWithSelections } from '../helpers/utils';
import { CUSTOM_PANELS_API_PREFIX } from '../../../../common/constants/custom_panels';
import { MetricsExportPanel } from './metrics_export_panel';
import { addVizToPanels, uuidRx } from '../../custom_panels/redux/panel_slice';

interface TopMenuProps {
http: CoreStart['http'];
Expand Down Expand Up @@ -191,8 +192,11 @@ export const TopMenu = ({
if (selectedPanelOptions.length > 0) {
try {
const allMetricIds = savedMetricIds.map((metric) => metric.objectId);
savedMetricsInPanels = await Promise.all(
selectedPanelOptions.map((panel) => {
const soPanels = selectedPanelOptions.filter((panel) => uuidRx.test(panel.panel.id));
const opsPanels = selectedPanelOptions.filter((panel) => !uuidRx.test(panel.panel.id));

const savedMetricsInOpsPanels = await Promise.all(
opsPanels.map((panel) => {
return http.post(`${CUSTOM_PANELS_API_PREFIX}/visualizations/multiple`, {
body: JSON.stringify({
panelId: panel.panel.id,
Expand All @@ -201,6 +205,10 @@ 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 1ecd7ea

Please sign in to comment.