getAlertingValidationMessage.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { DataQuery, DataSourceRef, DataTransformerConfig } from '@grafana/data';
  2. import { DataSourceSrv } from '@grafana/runtime';
  3. export const getDefaultCondition = () => ({
  4. type: 'query',
  5. query: { params: ['A', '5m', 'now'] },
  6. reducer: { type: 'avg', params: [] as any[] },
  7. evaluator: { type: 'gt', params: [null] as any[] },
  8. operator: { type: 'and' },
  9. });
  10. export const getAlertingValidationMessage = async (
  11. transformations: DataTransformerConfig[] | undefined,
  12. targets: DataQuery[],
  13. datasourceSrv: DataSourceSrv,
  14. datasource: DataSourceRef | null
  15. ): Promise<string> => {
  16. if (targets.length === 0) {
  17. return 'Could not find any metric queries';
  18. }
  19. if (transformations && transformations.length) {
  20. return 'Transformations are not supported in alert queries';
  21. }
  22. let alertingNotSupported = 0;
  23. let templateVariablesNotSupported = 0;
  24. for (const target of targets) {
  25. const dsRef = target.datasource || datasource;
  26. const ds = await datasourceSrv.get(dsRef);
  27. if (!ds.meta.alerting) {
  28. alertingNotSupported++;
  29. } else if (ds.targetContainsTemplate && ds.targetContainsTemplate(target)) {
  30. templateVariablesNotSupported++;
  31. }
  32. }
  33. if (alertingNotSupported === targets.length) {
  34. return 'The datasource does not support alerting queries';
  35. }
  36. if (templateVariablesNotSupported === targets.length) {
  37. return 'Template variables are not supported in alert queries';
  38. }
  39. return '';
  40. };