RuleDetailsDataSources.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { css } from '@emotion/css';
  2. import React, { useMemo } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { getDataSourceSrv } from '@grafana/runtime';
  5. import { useStyles2 } from '@grafana/ui';
  6. import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
  7. import { CombinedRule, RulesSource } from 'app/types/unified-alerting';
  8. import { isCloudRulesSource } from '../../utils/datasource';
  9. import { isGrafanaRulerRule } from '../../utils/rules';
  10. import { DetailsField } from '../DetailsField';
  11. type Props = {
  12. rule: CombinedRule;
  13. rulesSource: RulesSource;
  14. };
  15. export function RuleDetailsDataSources(props: Props): JSX.Element | null {
  16. const { rulesSource, rule } = props;
  17. const styles = useStyles2(getStyles);
  18. const dataSources: Array<{ name: string; icon?: string }> = useMemo(() => {
  19. if (isCloudRulesSource(rulesSource)) {
  20. return [{ name: rulesSource.name, icon: rulesSource.meta.info.logos.small }];
  21. }
  22. if (isGrafanaRulerRule(rule.rulerRule)) {
  23. const { data } = rule.rulerRule.grafana_alert;
  24. const unique = data.reduce((dataSources, query) => {
  25. const ds = getDataSourceSrv().getInstanceSettings(query.datasourceUid);
  26. if (!ds || ds.uid === ExpressionDatasourceUID) {
  27. return dataSources;
  28. }
  29. dataSources[ds.name] = { name: ds.name, icon: ds.meta.info.logos.small };
  30. return dataSources;
  31. }, {} as Record<string, { name: string; icon?: string }>);
  32. return Object.values(unique);
  33. }
  34. return [];
  35. }, [rule, rulesSource]);
  36. if (dataSources.length === 0) {
  37. return null;
  38. }
  39. return (
  40. <DetailsField label="Data source">
  41. {dataSources.map(({ name, icon }, index) => (
  42. <div key={name}>
  43. {icon && (
  44. <>
  45. <img alt={`${name} datasource logo`} className={styles.dataSourceIcon} src={icon} />{' '}
  46. </>
  47. )}
  48. {name}
  49. </div>
  50. ))}
  51. </DetailsField>
  52. );
  53. }
  54. function getStyles(theme: GrafanaTheme2) {
  55. const size = theme.spacing(2);
  56. return {
  57. dataSourceIcon: css`
  58. width: ${size};
  59. height: ${size};
  60. `,
  61. };
  62. }