useExternalAmSelector.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { useSelector } from 'react-redux';
  2. import { StoreState } from '../../../../types';
  3. const SUFFIX_REGEX = /\/api\/v[1|2]\/alerts/i;
  4. type AlertmanagerConfig = { url: string; status: string; actualUrl: string };
  5. export function useExternalAmSelector(): AlertmanagerConfig[] | [] {
  6. const discoveredAlertmanagers = useSelector(
  7. (state: StoreState) => state.unifiedAlerting.externalAlertmanagers.discoveredAlertmanagers.result?.data
  8. );
  9. const alertmanagerConfig = useSelector(
  10. (state: StoreState) => state.unifiedAlerting.externalAlertmanagers.alertmanagerConfig.result?.alertmanagers
  11. );
  12. if (!discoveredAlertmanagers || !alertmanagerConfig) {
  13. return [];
  14. }
  15. const enabledAlertmanagers: AlertmanagerConfig[] = [];
  16. const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers.droppedAlertManagers.map((am) => ({
  17. url: am.url.replace(SUFFIX_REGEX, ''),
  18. status: 'dropped',
  19. actualUrl: am.url,
  20. }));
  21. for (const url of alertmanagerConfig) {
  22. if (discoveredAlertmanagers.activeAlertManagers.length === 0) {
  23. enabledAlertmanagers.push({
  24. url: url,
  25. status: 'pending',
  26. actualUrl: '',
  27. });
  28. } else {
  29. const matchingActiveAM = discoveredAlertmanagers.activeAlertManagers.find(
  30. (am) => am.url === `${url}/api/v2/alerts`
  31. );
  32. matchingActiveAM
  33. ? enabledAlertmanagers.push({
  34. url: matchingActiveAM.url.replace(SUFFIX_REGEX, ''),
  35. status: 'active',
  36. actualUrl: matchingActiveAM.url,
  37. })
  38. : enabledAlertmanagers.push({
  39. url: url,
  40. status: 'pending',
  41. actualUrl: '',
  42. });
  43. }
  44. }
  45. return [...enabledAlertmanagers, ...droppedAlertmanagers];
  46. }