annotations_srv.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import './editor_ctrl';
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. import { makeRegions, dedupAnnotations } from './events_processing';
  6. export class AnnotationsSrv {
  7. globalAnnotationsPromise: any;
  8. alertStatesPromise: any;
  9. datasourcePromises: any;
  10. /** @ngInject */
  11. constructor(private $rootScope, private $q, private datasourceSrv, private backendSrv, private timeSrv) {
  12. $rootScope.onAppEvent('refresh', this.clearCache.bind(this), $rootScope);
  13. $rootScope.onAppEvent('dashboard-initialized', this.clearCache.bind(this), $rootScope);
  14. }
  15. clearCache() {
  16. this.globalAnnotationsPromise = null;
  17. this.alertStatesPromise = null;
  18. this.datasourcePromises = null;
  19. }
  20. getAnnotations(options) {
  21. return this.$q
  22. .all([this.getGlobalAnnotations(options), this.getAlertStates(options)])
  23. .then(results => {
  24. // combine the annotations and flatten results
  25. let annotations = _.flattenDeep(results[0]);
  26. // filter out annotations that do not belong to requesting panel
  27. annotations = _.filter(annotations, item => {
  28. // if event has panel id and query is of type dashboard then panel and requesting panel id must match
  29. if (item.panelId && item.source.type === 'dashboard') {
  30. return item.panelId === options.panel.id;
  31. }
  32. return true;
  33. });
  34. annotations = dedupAnnotations(annotations);
  35. annotations = makeRegions(annotations, options);
  36. // look for alert state for this panel
  37. const alertState = _.find(results[1], { panelId: options.panel.id });
  38. return {
  39. annotations: annotations,
  40. alertState: alertState,
  41. };
  42. })
  43. .catch(err => {
  44. if (!err.message && err.data && err.data.message) {
  45. err.message = err.data.message;
  46. }
  47. console.log('AnnotationSrv.query error', err);
  48. this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', err.message || err]);
  49. return [];
  50. });
  51. }
  52. getAlertStates(options) {
  53. if (!options.dashboard.id) {
  54. return this.$q.when([]);
  55. }
  56. // ignore if no alerts
  57. if (options.panel && !options.panel.alert) {
  58. return this.$q.when([]);
  59. }
  60. if (options.range.raw.to !== 'now') {
  61. return this.$q.when([]);
  62. }
  63. if (this.alertStatesPromise) {
  64. return this.alertStatesPromise;
  65. }
  66. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {
  67. dashboardId: options.dashboard.id,
  68. });
  69. return this.alertStatesPromise;
  70. }
  71. getGlobalAnnotations(options) {
  72. const dashboard = options.dashboard;
  73. if (this.globalAnnotationsPromise) {
  74. return this.globalAnnotationsPromise;
  75. }
  76. const range = this.timeSrv.timeRange();
  77. const promises = [];
  78. const dsPromises = [];
  79. for (const annotation of dashboard.annotations.list) {
  80. if (!annotation.enable) {
  81. continue;
  82. }
  83. if (annotation.snapshotData) {
  84. return this.translateQueryResult(annotation, annotation.snapshotData);
  85. }
  86. const datasourcePromise = this.datasourceSrv.get(annotation.datasource);
  87. dsPromises.push(datasourcePromise);
  88. promises.push(
  89. datasourcePromise
  90. .then(datasource => {
  91. // issue query against data source
  92. return datasource.annotationQuery({
  93. range: range,
  94. rangeRaw: range.raw,
  95. annotation: annotation,
  96. dashboard: dashboard,
  97. });
  98. })
  99. .then(results => {
  100. // store response in annotation object if this is a snapshot call
  101. if (dashboard.snapshot) {
  102. annotation.snapshotData = angular.copy(results);
  103. }
  104. // translate result
  105. return this.translateQueryResult(annotation, results);
  106. })
  107. );
  108. }
  109. this.datasourcePromises = this.$q.all(dsPromises);
  110. this.globalAnnotationsPromise = this.$q.all(promises);
  111. return this.globalAnnotationsPromise;
  112. }
  113. saveAnnotationEvent(annotation) {
  114. this.globalAnnotationsPromise = null;
  115. return this.backendSrv.post('/api/annotations', annotation);
  116. }
  117. updateAnnotationEvent(annotation) {
  118. this.globalAnnotationsPromise = null;
  119. return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
  120. }
  121. deleteAnnotationEvent(annotation) {
  122. this.globalAnnotationsPromise = null;
  123. let deleteUrl = `/api/annotations/${annotation.id}`;
  124. if (annotation.isRegion) {
  125. deleteUrl = `/api/annotations/region/${annotation.regionId}`;
  126. }
  127. return this.backendSrv.delete(deleteUrl);
  128. }
  129. translateQueryResult(annotation, results) {
  130. // if annotation has snapshotData
  131. // make clone and remove it
  132. if (annotation.snapshotData) {
  133. annotation = angular.copy(annotation);
  134. delete annotation.snapshotData;
  135. }
  136. for (const item of results) {
  137. item.source = annotation;
  138. }
  139. return results;
  140. }
  141. }
  142. coreModule.service('annotationsSrv', AnnotationsSrv);