AlertGroupHeader.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import pluralize from 'pluralize';
  2. import React from 'react';
  3. import { useStyles2 } from '@grafana/ui';
  4. import { AlertmanagerGroup, AlertState } from 'app/plugins/datasource/alertmanager/types';
  5. import { getNotificationsTextColors } from '../../styles/notifications';
  6. interface Props {
  7. group: AlertmanagerGroup;
  8. }
  9. export const AlertGroupHeader = ({ group }: Props) => {
  10. const textStyles = useStyles2(getNotificationsTextColors);
  11. const total = group.alerts.length;
  12. const countByStatus = group.alerts.reduce((statusObj, alert) => {
  13. if (statusObj[alert.status.state]) {
  14. statusObj[alert.status.state] += 1;
  15. } else {
  16. statusObj[alert.status.state] = 1;
  17. }
  18. return statusObj;
  19. }, {} as Record<AlertState, number>);
  20. return (
  21. <div>
  22. {`${total} ${pluralize('alert', total)}: `}
  23. {Object.entries(countByStatus).map(([state, count], index) => {
  24. return (
  25. <span
  26. key={`${JSON.stringify(group.labels)}-notifications-${index}`}
  27. className={textStyles[state as AlertState]}
  28. >
  29. {index > 0 && ', '}
  30. {`${count} ${state}`}
  31. </span>
  32. );
  33. })}
  34. </div>
  35. );
  36. };