annotationSupport.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { AnnotationQuery } from '@grafana/data';
  2. import { AnnotationQueryEditor } from './components/AnnotationQueryEditor';
  3. import { isCloudWatchAnnotation } from './guards';
  4. import { CloudWatchAnnotationQuery, CloudWatchQuery, LegacyAnnotationQuery } from './types';
  5. export const CloudWatchAnnotationSupport = {
  6. // converts legacy angular style queries to new format. Also sets the same default values as in the deprecated angular directive
  7. prepareAnnotation: (
  8. query: LegacyAnnotationQuery | AnnotationQuery<CloudWatchAnnotationQuery>
  9. ): AnnotationQuery<CloudWatchAnnotationQuery> => {
  10. if (isCloudWatchAnnotation(query)) {
  11. return query;
  12. }
  13. return {
  14. // setting AnnotationQuery props explicitly since spreading would incorrectly use props that should be on the target only
  15. datasource: query.datasource,
  16. enable: query.enable,
  17. iconColor: query.iconColor,
  18. name: query.name,
  19. builtIn: query.builtIn,
  20. hide: query.hide,
  21. target: {
  22. ...query.target,
  23. ...query,
  24. statistic: query.statistic || 'Average',
  25. region: query.region || 'default',
  26. queryMode: 'Annotations',
  27. refId: query.refId || 'annotationQuery',
  28. },
  29. };
  30. },
  31. // return undefined if query is not complete so that annotation query execution is quietly skipped
  32. prepareQuery: (anno: AnnotationQuery<CloudWatchAnnotationQuery>): CloudWatchQuery | undefined => {
  33. if (!anno.target) {
  34. return undefined;
  35. }
  36. const {
  37. prefixMatching,
  38. actionPrefix,
  39. alarmNamePrefix,
  40. statistic,
  41. namespace,
  42. metricName,
  43. dimensions = {},
  44. } = anno.target;
  45. const validPrefixMatchingQuery = !!prefixMatching && !!actionPrefix && !!alarmNamePrefix;
  46. const validMetricStatQuery =
  47. !prefixMatching && !!namespace && !!metricName && !!statistic && !!Object.values(dimensions).length;
  48. if (validPrefixMatchingQuery || validMetricStatQuery) {
  49. return anno.target;
  50. }
  51. return undefined;
  52. },
  53. QueryEditor: AnnotationQueryEditor,
  54. };