annotations_srv.ts 4.9 KB

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