Files
grafana/public/app/features/alerting/unified/PanelAlertTabContent.tsx
Sonia Aguilar 4b720206d4 Alerting: Reduce number of request fetching rules in the dashboard view using rtkq (#86991)
* Reduce number of request fetching rules in the dashboard view using rtkq

* Fix UnifiedAlertStatesWorker work

* refactor ungroupRulesByFileName

* Address review comments and fix test

* fix DashboardQueryRunner test

* Fix tests

* Update AlertStatesDataLayer to use RTKQ

* Fix PanelAlertTabContent test

* Fix PanelAlertTabContent test after adding RTKQ

* fix test and address PR review comments

* Update useCombinedRuleNamespaces to have both dashboardUID and panelId as optional params and rename the hook

* Address review pr comment

* remove test about template variables

* Use poll interval in useCombinedRules
2024-05-10 12:27:06 +02:00

93 lines
2.9 KiB
TypeScript

import { css } from '@emotion/css';
import React from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { Alert, CustomScrollbar, LoadingPlaceholder, useStyles2 } from '@grafana/ui';
import { contextSrv } from 'app/core/services/context_srv';
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
import { NewRuleFromPanelButton } from './components/panel-alerts-tab/NewRuleFromPanelButton';
import { RulesTable } from './components/rules/RulesTable';
import { usePanelCombinedRules } from './hooks/usePanelCombinedRules';
import { getRulesPermissions } from './utils/access-control';
import { stringifyErrorLike } from './utils/misc';
interface Props {
dashboard: DashboardModel;
panel: PanelModel;
}
export const PanelAlertTabContent = ({ dashboard, panel }: Props) => {
const styles = useStyles2(getStyles);
const { errors, loading, rules } = usePanelCombinedRules({
dashboardUID: dashboard.uid,
panelId: panel.id,
poll: true,
});
const permissions = getRulesPermissions('grafana');
const canCreateRules = contextSrv.hasPermission(permissions.create);
const alert = errors.length ? (
<Alert title="Errors loading rules" severity="error">
{errors.map((error, index) => (
<div key={index}>Failed to load Grafana rules state: {stringifyErrorLike(error)}</div>
))}
</Alert>
) : null;
if (loading && !rules.length) {
return (
<div className={styles.innerWrapper}>
{alert}
<LoadingPlaceholder text="Loading rules..." />
</div>
);
}
if (rules.length) {
return (
<CustomScrollbar autoHeightMin="100%">
<div className={styles.innerWrapper}>
{alert}
<RulesTable rules={rules} />
{!!dashboard.meta.canSave && canCreateRules && (
<NewRuleFromPanelButton className={styles.newButton} panel={panel} dashboard={dashboard} />
)}
</div>
</CustomScrollbar>
);
}
return (
<div data-testid={selectors.components.PanelAlertTabContent.content} className={styles.noRulesWrapper}>
{alert}
{!!dashboard.uid && (
<>
<p>There are no alert rules linked to this panel.</p>
{!!dashboard.meta.canSave && canCreateRules && <NewRuleFromPanelButton panel={panel} dashboard={dashboard} />}
</>
)}
{!dashboard.uid && !!dashboard.meta.canSave && (
<Alert severity="info" title="Dashboard not saved">
Dashboard must be saved before alerts can be added.
</Alert>
)}
</div>
);
};
const getStyles = (theme: GrafanaTheme2) => ({
newButton: css({
marginTop: theme.spacing(3),
}),
innerWrapper: css({
padding: theme.spacing(2),
}),
noRulesWrapper: css({
margin: theme.spacing(2),
backgroundColor: theme.colors.background.secondary,
padding: theme.spacing(3),
}),
});