annotations_srv.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Libaries
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. // Components
  5. import './editor_ctrl';
  6. import coreModule from 'app/core/core_module';
  7. // Utils & Services
  8. import { makeRegions, dedupAnnotations } from './events_processing';
  9. // Types
  10. import { DashboardModel } from '../dashboard/state/DashboardModel';
  11. export class AnnotationsSrv {
  12. globalAnnotationsPromise: any;
  13. alertStatesPromise: any;
  14. datasourcePromises: any;
  15. /** @ngInject */
  16. constructor(private $rootScope, private $q, private datasourceSrv, private backendSrv, private timeSrv) {}
  17. init(dashboard: DashboardModel) {
  18. // always clearPromiseCaches when loading new dashboard
  19. this.clearPromiseCaches();
  20. // clear promises on refresh events
  21. dashboard.on('refresh', this.clearPromiseCaches.bind(this));
  22. }
  23. clearPromiseCaches() {
  24. this.globalAnnotationsPromise = null;
  25. this.alertStatesPromise = null;
  26. this.datasourcePromises = 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. let annotations: any[] = _.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. const alertState: any = _.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', ['Annotation Query Failed', err.message || err]);
  57. return [];
  58. });
  59. }
  60. getAlertStates(options) {
  61. if (!options.dashboard.id) {
  62. return this.$q.when([]);
  63. }
  64. // ignore if no alerts
  65. if (options.panel && !options.panel.alert) {
  66. return this.$q.when([]);
  67. }
  68. if (options.range.raw.to !== 'now') {
  69. return this.$q.when([]);
  70. }
  71. if (this.alertStatesPromise) {
  72. return this.alertStatesPromise;
  73. }
  74. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {
  75. dashboardId: options.dashboard.id,
  76. });
  77. return this.alertStatesPromise;
  78. }
  79. getGlobalAnnotations(options) {
  80. const dashboard = options.dashboard;
  81. if (this.globalAnnotationsPromise) {
  82. return this.globalAnnotationsPromise;
  83. }
  84. const range = this.timeSrv.timeRange();
  85. const promises = [];
  86. const dsPromises = [];
  87. for (const annotation of dashboard.annotations.list) {
  88. if (!annotation.enable) {
  89. continue;
  90. }
  91. if (annotation.snapshotData) {
  92. return this.translateQueryResult(annotation, annotation.snapshotData);
  93. }
  94. const datasourcePromise = this.datasourceSrv.get(annotation.datasource);
  95. dsPromises.push(datasourcePromise);
  96. promises.push(
  97. datasourcePromise
  98. .then(datasource => {
  99. // issue query against data source
  100. return datasource.annotationQuery({
  101. range: range,
  102. rangeRaw: range.raw,
  103. annotation: annotation,
  104. dashboard: dashboard,
  105. });
  106. })
  107. .then(results => {
  108. // store response in annotation object if this is a snapshot call
  109. if (dashboard.snapshot) {
  110. annotation.snapshotData = angular.copy(results);
  111. }
  112. // translate result
  113. return this.translateQueryResult(annotation, results);
  114. })
  115. );
  116. }
  117. this.datasourcePromises = this.$q.all(dsPromises);
  118. this.globalAnnotationsPromise = this.$q.all(promises);
  119. return this.globalAnnotationsPromise;
  120. }
  121. saveAnnotationEvent(annotation) {
  122. this.globalAnnotationsPromise = null;
  123. return this.backendSrv.post('/api/annotations', annotation);
  124. }
  125. updateAnnotationEvent(annotation) {
  126. this.globalAnnotationsPromise = null;
  127. return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
  128. }
  129. deleteAnnotationEvent(annotation) {
  130. this.globalAnnotationsPromise = null;
  131. let deleteUrl = `/api/annotations/${annotation.id}`;
  132. if (annotation.isRegion) {
  133. deleteUrl = `/api/annotations/region/${annotation.regionId}`;
  134. }
  135. return this.backendSrv.delete(deleteUrl);
  136. }
  137. translateQueryResult(annotation, results) {
  138. // if annotation has snapshotData
  139. // make clone and remove it
  140. if (annotation.snapshotData) {
  141. annotation = angular.copy(annotation);
  142. delete annotation.snapshotData;
  143. }
  144. for (const item of results) {
  145. item.source = annotation;
  146. }
  147. return results;
  148. }
  149. }
  150. coreModule.service('annotationsSrv', AnnotationsSrv);