useHasRuler.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import { useCallback } from 'react';
  2. import { RulesSource } from 'app/types/unified-alerting';
  3. import { getRulesSourceName, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
  4. import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
  5. // datasource has ruler if it's grafana managed or if we're able to load rules from it
  6. export function useHasRuler() {
  7. const rulerRules = useUnifiedAlertingSelector((state) => state.rulerRules);
  8. const hasRuler = useCallback(
  9. (rulesSource: string | RulesSource) => {
  10. const rulesSourceName = typeof rulesSource === 'string' ? rulesSource : rulesSource.name;
  11. return rulesSourceName === GRAFANA_RULES_SOURCE_NAME || !!rulerRules[rulesSourceName]?.result;
  12. },
  13. [rulerRules]
  14. );
  15. const rulerRulesLoaded = useCallback(
  16. (rulesSource: RulesSource) => {
  17. const rulesSourceName = getRulesSourceName(rulesSource);
  18. const result = rulerRules[rulesSourceName]?.result;
  19. return Boolean(result);
  20. },
  21. [rulerRules]
  22. );
  23. return { hasRuler, rulerRulesLoaded };
  24. }