annotations_srv.ts 5.5 KB

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