useRuleSourcesWithRuler.ts 812 B

12345678910111213141516171819
  1. import { DataSourceInstanceSettings } from '@grafana/data';
  2. import { PromBasedDataSource } from 'app/types/unified-alerting';
  3. import { getDataSourceByName } from '../utils/datasource';
  4. import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
  5. export function useRulesSourcesWithRuler(): DataSourceInstanceSettings[] {
  6. const dataSources = useUnifiedAlertingSelector((state) => state.dataSources);
  7. const dataSourcesWithRuler = Object.values(dataSources)
  8. .map((ds) => ds.result)
  9. .filter((ds): ds is PromBasedDataSource => Boolean(ds?.rulerConfig));
  10. // try fetching rules for each prometheus to see if it has ruler
  11. return dataSourcesWithRuler
  12. .map((ds) => getDataSourceByName(ds.name))
  13. .filter((dsConfig): dsConfig is DataSourceInstanceSettings => Boolean(dsConfig));
  14. }