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

Alerting: Show panels within collapsed rows in dashboard picker #75490

Merged
merged 3 commits into from
Sep 27, 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
@@ -1,4 +1,4 @@
import { findByRole, findByText, findByTitle, getByTestId, render } from '@testing-library/react';
import { findByRole, findByText, findByTitle, getByTestId, queryByText, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
Expand Down Expand Up @@ -163,6 +163,71 @@ describe('AnnotationsField', function () {
expect(annotationValueElements[1]).toHaveTextContent('2');
});

it('should not show rows as panels', async function () {
mockSearchApiResponse(server, [
mockDashboardSearchItem({ title: 'My dashboard', uid: 'dash-test-uid', type: DashboardSearchItemType.DashDB }),
]);

mockGetDashboardResponse(
mockDashboardDto({
title: 'My dashboard',
uid: 'dash-test-uid',
panels: [
{ id: 1, title: 'Row panel', type: 'row' },
{ id: 2, title: 'First panel', type: 'timeseries' },
],
})
);

const user = userEvent.setup();

render(<FormWrapper />);

await user.click(ui.setDashboardButton.get());
expect(ui.dashboardPicker.confirmButton.get()).toBeDisabled();

await user.click(await findByTitle(ui.dashboardPicker.dialog.get(), 'My dashboard'));

expect(await findByText(ui.dashboardPicker.dialog.get(), 'First panel')).toBeInTheDocument();
expect(await queryByText(ui.dashboardPicker.dialog.get(), 'Row panel')).not.toBeInTheDocument();
});

it('should show panels within collapsed rows', async function () {
mockSearchApiResponse(server, [
mockDashboardSearchItem({ title: 'My dashboard', uid: 'dash-test-uid', type: DashboardSearchItemType.DashDB }),
]);

mockGetDashboardResponse(
mockDashboardDto({
title: 'My dashboard',
uid: 'dash-test-uid',
panels: [
{ id: 1, title: 'First panel', type: 'timeseries' },
{
id: 2,
title: 'Row panel',
collapsed: true,
type: 'row',
panels: [{ id: 3, title: 'Panel within collapsed row', type: 'timeseries' }],
},
],
})
);

const user = userEvent.setup();

render(<FormWrapper />);

await user.click(ui.setDashboardButton.get());
expect(ui.dashboardPicker.confirmButton.get()).toBeDisabled();

await user.click(await findByTitle(ui.dashboardPicker.dialog.get(), 'My dashboard'));

expect(await findByText(ui.dashboardPicker.dialog.get(), 'First panel')).toBeInTheDocument();
expect(await queryByText(ui.dashboardPicker.dialog.get(), 'Row panel')).not.toBeInTheDocument();
expect(await findByText(ui.dashboardPicker.dialog.get(), 'Panel within collapsed row')).toBeInTheDocument();
});

// this test _should_ work in theory but something is stopping the 'onClick' function on the dashboard item
// to trigger "handleDashboardChange" – skipping it for now but has been manually tested.
it.skip('should update existing dashboard and panel identifies', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Annotation, annotationLabels } from '../../utils/constants';

import AnnotationHeaderField from './AnnotationHeaderField';
import DashboardAnnotationField from './DashboardAnnotationField';
import { DashboardPicker, PanelDTO } from './DashboardPicker';
import { DashboardPicker, mergePanels, PanelDTO } from './DashboardPicker';
import { NeedHelpInfo } from './NeedHelpInfo';
import { RuleEditorSection } from './RuleEditorSection';

Expand Down Expand Up @@ -53,7 +53,9 @@ const AnnotationsStep = () => {
}

setSelectedDashboard(dashboardResult?.dashboard);
const currentPanel = dashboardResult?.dashboard?.panels?.find((panel) => panel.id.toString() === selectedPanelId);

const allPanels = mergePanels(dashboardResult);
const currentPanel = allPanels.find((panel) => panel.id.toString() === selectedPanelId);
setSelectedPanel(currentPanel);
}, [selectedPanelId, dashboardResult, isDashboardFetching]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Tooltip,
useStyles2,
} from '@grafana/ui';
import { DashboardDTO } from 'app/types';

import { dashboardApi } from '../../api/dashboardApi';

Expand Down Expand Up @@ -47,6 +48,18 @@ interface DashboardPickerProps {
onDismiss: () => void;
}

export function mergePanels(dashboardResult: DashboardDTO | undefined) {
const panels = dashboardResult?.dashboard?.panels?.filter((panel) => panel.type !== 'row') || [];
const nestedPanels =
dashboardResult?.dashboard?.panels
?.filter((row: { collapsed: boolean }) => row.collapsed)
.map((collapsedRow: { panels: PanelDTO[] }) => collapsedRow.panels) || [];

const allDashboardPanels = [...panels, ...nestedPanels.flat()];

return allDashboardPanels;
}

export const DashboardPicker = ({ dashboardUid, panelId, isOpen, onChange, onDismiss }: DashboardPickerProps) => {
const styles = useStyles2(getPickerStyles);

Expand All @@ -72,12 +85,14 @@ export const DashboardPicker = ({ dashboardUid, panelId, isOpen, onChange, onDis
setSelectedPanelId(undefined);
}, []);

const allDashboardPanels = mergePanels(dashboardResult);

const filteredPanels =
dashboardResult?.dashboard?.panels
allDashboardPanels
?.filter((panel) => panel.title?.toLowerCase().includes(panelFilter.toLowerCase()))
.sort(panelSort) ?? [];

const currentPanel: PanelDTO | undefined = dashboardResult?.dashboard?.panels?.find(
const currentPanel: PanelDTO | undefined = allDashboardPanels.find(
(panel: PanelDTO) => isValidPanelIdentifier(panel) && panel.id?.toString() === selectedPanelId
);

Expand Down