grafana/public/app/plugins/panel/alertGroups/AlertGroupsPanel.tsx
Nathan Rodman b153bb6101
Alerting: Add alertmanager notifications panel (#37078)
* Add filter parsing to rule list filters

* Add unit tests for label parsing

* Make label operators an enum

* add example for parsing function

* Update labels operator regex

* Use tooltip for query syntax example

* refactor to use Matchers for filtering

* wip: initial alertmanager notifications panel

* Panel for alertmanager notificaitons

* add filtering for notifications list

* remove icon

* rename am notifications to alert groups

* naming fixes

* Feature toggle

* Add toggle for expand all

* add pluralize

* add action buttons

* test work in progress

* Tests for alert groups panel

* Add useEffect for expandAll prop change

* Set panel to alpha state

* Fix colors

* fix polling interval callback

Co-authored-by: Domas <domas.lapinskas@grafana.com>

Co-authored-by: Domas <domas.lapinskas@grafana.com>
2021-08-17 15:02:03 -07:00

67 lines
2.5 KiB
TypeScript

import React, { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { PanelProps } from '@grafana/data';
import { CustomScrollbar } from '@grafana/ui';
import { config } from '@grafana/runtime';
import { AlertmanagerGroup, Matcher } from 'app/plugins/datasource/alertmanager/types';
import { fetchAlertGroupsAction } from 'app/features/alerting/unified/state/actions';
import { initialAsyncRequestState } from 'app/features/alerting/unified/utils/redux';
import { NOTIFICATIONS_POLL_INTERVAL_MS } from 'app/features/alerting/unified/utils/constants';
import { useUnifiedAlertingSelector } from 'app/features/alerting/unified/hooks/useUnifiedAlertingSelector';
import { AlertGroup } from './AlertGroup';
import { AlertGroupPanelOptions } from './types';
import { parseMatchers } from 'app/features/alerting/unified/utils/alertmanager';
import { useFilteredGroups } from './useFilteredGroups';
export const AlertGroupsPanel = (props: PanelProps<AlertGroupPanelOptions>) => {
const dispatch = useDispatch();
const isAlertingEnabled = config.featureToggles.ngalert;
const expandAll = props.options.expandAll;
const alertManagerSourceName = props.options.alertmanager;
const alertGroups = useUnifiedAlertingSelector((state) => state.amAlertGroups) || initialAsyncRequestState;
const results: AlertmanagerGroup[] = alertGroups[alertManagerSourceName || '']?.result || [];
const matchers: Matcher[] = props.options.labels ? parseMatchers(props.options.labels) : [];
const filteredResults = useFilteredGroups(results, matchers);
useEffect(() => {
function fetchNotifications() {
if (alertManagerSourceName) {
dispatch(fetchAlertGroupsAction(alertManagerSourceName));
}
}
fetchNotifications();
const interval = setInterval(fetchNotifications, NOTIFICATIONS_POLL_INTERVAL_MS);
return () => {
clearInterval(interval);
};
}, [dispatch, alertManagerSourceName]);
const hasResults = filteredResults.length > 0;
return (
<CustomScrollbar autoHeightMax="100%" autoHeightMin="100%">
{isAlertingEnabled && (
<div>
{hasResults &&
filteredResults.map((group) => {
return (
<AlertGroup
alertManagerSourceName={alertManagerSourceName}
key={JSON.stringify(group.labels)}
group={group}
expandAll={expandAll}
/>
);
})}
{!hasResults && 'No alerts'}
</div>
)}
</CustomScrollbar>
);
};