AlertGroupsPanel.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useEffect } from 'react';
  2. import { useDispatch } from 'react-redux';
  3. import { PanelProps } from '@grafana/data';
  4. import { config } from '@grafana/runtime';
  5. import { CustomScrollbar } from '@grafana/ui';
  6. import { useUnifiedAlertingSelector } from 'app/features/alerting/unified/hooks/useUnifiedAlertingSelector';
  7. import { fetchAlertGroupsAction } from 'app/features/alerting/unified/state/actions';
  8. import { parseMatchers } from 'app/features/alerting/unified/utils/alertmanager';
  9. import { NOTIFICATIONS_POLL_INTERVAL_MS } from 'app/features/alerting/unified/utils/constants';
  10. import { initialAsyncRequestState } from 'app/features/alerting/unified/utils/redux';
  11. import { AlertmanagerGroup, Matcher } from 'app/plugins/datasource/alertmanager/types';
  12. import { AlertGroup } from './AlertGroup';
  13. import { AlertGroupPanelOptions } from './types';
  14. import { useFilteredGroups } from './useFilteredGroups';
  15. export const AlertGroupsPanel = (props: PanelProps<AlertGroupPanelOptions>) => {
  16. const dispatch = useDispatch();
  17. const isAlertingEnabled = config.unifiedAlertingEnabled;
  18. const expandAll = props.options.expandAll;
  19. const alertManagerSourceName = props.options.alertmanager;
  20. const alertGroups = useUnifiedAlertingSelector((state) => state.amAlertGroups) || initialAsyncRequestState;
  21. const results: AlertmanagerGroup[] = alertGroups[alertManagerSourceName || '']?.result || [];
  22. const matchers: Matcher[] = props.options.labels ? parseMatchers(props.options.labels) : [];
  23. const filteredResults = useFilteredGroups(results, matchers);
  24. useEffect(() => {
  25. function fetchNotifications() {
  26. if (alertManagerSourceName) {
  27. dispatch(fetchAlertGroupsAction(alertManagerSourceName));
  28. }
  29. }
  30. fetchNotifications();
  31. const interval = setInterval(fetchNotifications, NOTIFICATIONS_POLL_INTERVAL_MS);
  32. return () => {
  33. clearInterval(interval);
  34. };
  35. }, [dispatch, alertManagerSourceName]);
  36. const hasResults = filteredResults.length > 0;
  37. return (
  38. <CustomScrollbar autoHeightMax="100%" autoHeightMin="100%">
  39. {isAlertingEnabled && (
  40. <div>
  41. {hasResults &&
  42. filteredResults.map((group) => {
  43. return (
  44. <AlertGroup
  45. alertManagerSourceName={alertManagerSourceName}
  46. key={JSON.stringify(group.labels)}
  47. group={group}
  48. expandAll={expandAll}
  49. />
  50. );
  51. })}
  52. {!hasResults && 'No alerts'}
  53. </div>
  54. )}
  55. </CustomScrollbar>
  56. );
  57. };