useCombinedRule.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { useMemo } from 'react';
  2. import { useDispatch } from 'react-redux';
  3. import { useAsync } from 'react-use';
  4. import { CombinedRule, RuleIdentifier, RuleNamespace } from 'app/types/unified-alerting';
  5. import { RulerRulesConfigDTO } from 'app/types/unified-alerting-dto';
  6. import { fetchPromAndRulerRulesAction } from '../state/actions';
  7. import { AsyncRequestMapSlice, AsyncRequestState, initialAsyncRequestState } from '../utils/redux';
  8. import * as ruleId from '../utils/rule-id';
  9. import { isRulerNotSupportedResponse } from '../utils/rules';
  10. import { useCombinedRuleNamespaces } from './useCombinedRuleNamespaces';
  11. import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
  12. export function useCombinedRule(
  13. identifier: RuleIdentifier | undefined,
  14. ruleSourceName: string | undefined
  15. ): AsyncRequestState<CombinedRule> {
  16. const requestState = useCombinedRulesLoader(ruleSourceName);
  17. const combinedRules = useCombinedRuleNamespaces(ruleSourceName);
  18. const rule = useMemo(() => {
  19. if (!identifier || !ruleSourceName || combinedRules.length === 0) {
  20. return;
  21. }
  22. for (const namespace of combinedRules) {
  23. for (const group of namespace.groups) {
  24. for (const rule of group.rules) {
  25. const id = ruleId.fromCombinedRule(ruleSourceName, rule);
  26. if (ruleId.equal(id, identifier)) {
  27. return rule;
  28. }
  29. }
  30. }
  31. }
  32. return;
  33. }, [identifier, ruleSourceName, combinedRules]);
  34. return {
  35. ...requestState,
  36. result: rule,
  37. };
  38. }
  39. export function useCombinedRulesMatching(
  40. ruleName: string | undefined,
  41. ruleSourceName: string | undefined
  42. ): AsyncRequestState<CombinedRule[]> {
  43. const requestState = useCombinedRulesLoader(ruleSourceName);
  44. const combinedRules = useCombinedRuleNamespaces(ruleSourceName);
  45. const rules = useMemo(() => {
  46. if (!ruleName || !ruleSourceName || combinedRules.length === 0) {
  47. return [];
  48. }
  49. const rules: CombinedRule[] = [];
  50. for (const namespace of combinedRules) {
  51. for (const group of namespace.groups) {
  52. for (const rule of group.rules) {
  53. if (rule.name === ruleName) {
  54. rules.push(rule);
  55. }
  56. }
  57. }
  58. }
  59. return rules;
  60. }, [ruleName, ruleSourceName, combinedRules]);
  61. return {
  62. ...requestState,
  63. result: rules,
  64. };
  65. }
  66. function useCombinedRulesLoader(rulesSourceName: string | undefined): AsyncRequestState<void> {
  67. const dispatch = useDispatch();
  68. const promRuleRequests = useUnifiedAlertingSelector((state) => state.promRules);
  69. const promRuleRequest = getRequestState(rulesSourceName, promRuleRequests);
  70. const rulerRuleRequests = useUnifiedAlertingSelector((state) => state.rulerRules);
  71. const rulerRuleRequest = getRequestState(rulesSourceName, rulerRuleRequests);
  72. const { loading } = useAsync(async () => {
  73. if (!rulesSourceName) {
  74. return;
  75. }
  76. await dispatch(fetchPromAndRulerRulesAction({ rulesSourceName }));
  77. }, [dispatch, rulesSourceName]);
  78. return {
  79. loading,
  80. error: promRuleRequest.error ?? isRulerNotSupportedResponse(rulerRuleRequest) ? undefined : rulerRuleRequest.error,
  81. dispatched: promRuleRequest.dispatched && rulerRuleRequest.dispatched,
  82. };
  83. }
  84. function getRequestState(
  85. ruleSourceName: string | undefined,
  86. slice: AsyncRequestMapSlice<RulerRulesConfigDTO | RuleNamespace[] | null>
  87. ): AsyncRequestState<RulerRulesConfigDTO | RuleNamespace[] | null> {
  88. if (!ruleSourceName) {
  89. return initialAsyncRequestState;
  90. }
  91. const state = slice[ruleSourceName];
  92. if (!state) {
  93. return initialAsyncRequestState;
  94. }
  95. return state;
  96. }