annotations_srv.ts 4.8 KB

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