2024-01-29 16:09:10 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
import { useAsync } from 'react-use';
|
|
|
|
|
|
|
|
|
|
import { LoadingPlaceholder } from '@grafana/ui';
|
2024-02-16 14:47:03 +01:00
|
|
|
import { getDashboardScenePageStateManager } from 'app/features/dashboard-scene/pages/DashboardScenePageStateManager';
|
|
|
|
|
import { DashboardRoutes, useDispatch } from 'app/types';
|
2024-01-29 16:09:10 +01:00
|
|
|
|
|
|
|
|
import { RulesTable } from '../components/rules/RulesTable';
|
|
|
|
|
import { useCombinedRuleNamespaces } from '../hooks/useCombinedRuleNamespaces';
|
|
|
|
|
import { fetchPromAndRulerRulesAction } from '../state/actions';
|
|
|
|
|
import { Annotation } from '../utils/constants';
|
|
|
|
|
import { GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
|
|
|
|
|
|
2024-02-16 14:47:03 +01:00
|
|
|
import LegacyAlertsDeprecationNotice from './LegacyAlertsDeprecationNotice';
|
|
|
|
|
|
2024-01-29 16:09:10 +01:00
|
|
|
interface Props {
|
|
|
|
|
dashboardUid: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function AlertRulesDrawerContent({ dashboardUid }: Props) {
|
|
|
|
|
const dispatch = useDispatch();
|
2024-02-16 14:47:03 +01:00
|
|
|
const dashboardStateManager = getDashboardScenePageStateManager();
|
|
|
|
|
|
|
|
|
|
const { value: dashboard, loading: loadingDashboardState } = useAsync(() => {
|
|
|
|
|
return dashboardStateManager
|
|
|
|
|
.fetchDashboard({
|
|
|
|
|
uid: dashboardUid,
|
|
|
|
|
route: DashboardRoutes.Normal,
|
|
|
|
|
})
|
|
|
|
|
.then((data) => (data ? data.dashboard : undefined));
|
|
|
|
|
}, [dashboardStateManager]);
|
2024-01-29 16:09:10 +01:00
|
|
|
|
2024-02-16 14:47:03 +01:00
|
|
|
const { loading: loadingAlertRules } = useAsync(async () => {
|
2024-01-29 16:09:10 +01:00
|
|
|
await dispatch(fetchPromAndRulerRulesAction({ rulesSourceName: GRAFANA_RULES_SOURCE_NAME }));
|
|
|
|
|
}, [dispatch]);
|
|
|
|
|
|
|
|
|
|
const grafanaNamespaces = useCombinedRuleNamespaces(GRAFANA_RULES_SOURCE_NAME);
|
|
|
|
|
const rules = grafanaNamespaces
|
|
|
|
|
.flatMap((ns) => ns.groups)
|
|
|
|
|
.flatMap((g) => g.rules)
|
|
|
|
|
.filter((rule) => rule.annotations[Annotation.dashboardUID] === dashboardUid);
|
|
|
|
|
|
2024-02-16 14:47:03 +01:00
|
|
|
const loading = loadingDashboardState || loadingAlertRules;
|
|
|
|
|
|
2024-01-29 16:09:10 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{loading ? (
|
|
|
|
|
<LoadingPlaceholder text="Loading alert rules" />
|
|
|
|
|
) : (
|
2024-02-16 14:47:03 +01:00
|
|
|
<>
|
|
|
|
|
<LegacyAlertsDeprecationNotice dashboard={dashboard} />
|
|
|
|
|
<RulesTable rules={rules} showNextEvaluationColumn={false} showGroupColumn={false} />
|
|
|
|
|
</>
|
2024-01-29 16:09:10 +01:00
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|