annotations_srv.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 { makeRegions, dedupAnnotations } from './events_processing';
  9. // Types
  10. import { DashboardModel } from '../dashboard/state/DashboardModel';
  11. import DatasourceSrv from '../plugins/datasource_srv';
  12. import { BackendSrv } from 'app/core/services/backend_srv';
  13. import { TimeSrv } from '../dashboard/services/TimeSrv';
  14. import { AnnotationEvent } from '@grafana/data';
  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: any[] = _.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. annotations = makeRegions(annotations, options);
  54. // look for alert state for this panel
  55. const alertState: any = _.find(results[1], { panelId: options.panel.id });
  56. return {
  57. annotations: annotations,
  58. alertState: alertState,
  59. };
  60. })
  61. .catch(err => {
  62. if (!err.message && err.data && err.data.message) {
  63. err.message = err.data.message;
  64. }
  65. console.log('AnnotationSrv.query error', err);
  66. this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', err.message || err]);
  67. return [];
  68. });
  69. }
  70. getAlertStates(options: any) {
  71. if (!options.dashboard.id) {
  72. return this.$q.when([]);
  73. }
  74. // ignore if no alerts
  75. if (options.panel && !options.panel.alert) {
  76. return this.$q.when([]);
  77. }
  78. if (options.range.raw.to !== 'now') {
  79. return this.$q.when([]);
  80. }
  81. if (this.alertStatesPromise) {
  82. return this.alertStatesPromise;
  83. }
  84. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {
  85. dashboardId: options.dashboard.id,
  86. });
  87. return this.alertStatesPromise;
  88. }
  89. getGlobalAnnotations(options: any) {
  90. const dashboard = options.dashboard;
  91. if (this.globalAnnotationsPromise) {
  92. return this.globalAnnotationsPromise;
  93. }
  94. const range = this.timeSrv.timeRange();
  95. const promises = [];
  96. const dsPromises = [];
  97. for (const annotation of dashboard.annotations.list) {
  98. if (!annotation.enable) {
  99. continue;
  100. }
  101. if (annotation.snapshotData) {
  102. return this.translateQueryResult(annotation, annotation.snapshotData);
  103. }
  104. const datasourcePromise = this.datasourceSrv.get(annotation.datasource);
  105. dsPromises.push(datasourcePromise);
  106. promises.push(
  107. datasourcePromise
  108. .then((datasource: any) => {
  109. // issue query against data source
  110. return datasource.annotationQuery({
  111. range: range,
  112. rangeRaw: range.raw,
  113. annotation: annotation,
  114. dashboard: dashboard,
  115. });
  116. })
  117. .then(results => {
  118. // store response in annotation object if this is a snapshot call
  119. if (dashboard.snapshot) {
  120. annotation.snapshotData = angular.copy(results);
  121. }
  122. // translate result
  123. return this.translateQueryResult(annotation, results);
  124. })
  125. );
  126. }
  127. this.datasourcePromises = this.$q.all(dsPromises);
  128. this.globalAnnotationsPromise = this.$q.all(promises);
  129. return this.globalAnnotationsPromise;
  130. }
  131. saveAnnotationEvent(annotation: AnnotationEvent) {
  132. this.globalAnnotationsPromise = null;
  133. return this.backendSrv.post('/api/annotations', annotation);
  134. }
  135. updateAnnotationEvent(annotation: AnnotationEvent) {
  136. this.globalAnnotationsPromise = null;
  137. return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
  138. }
  139. deleteAnnotationEvent(annotation: AnnotationEvent) {
  140. this.globalAnnotationsPromise = null;
  141. let deleteUrl = `/api/annotations/${annotation.id}`;
  142. if (annotation.isRegion) {
  143. deleteUrl = `/api/annotations/region/${annotation.regionId}`;
  144. }
  145. return this.backendSrv.delete(deleteUrl);
  146. }
  147. translateQueryResult(annotation: any, results: any) {
  148. // if annotation has snapshotData
  149. // make clone and remove it
  150. if (annotation.snapshotData) {
  151. annotation = angular.copy(annotation);
  152. delete annotation.snapshotData;
  153. }
  154. for (const item of results) {
  155. item.source = annotation;
  156. }
  157. return results;
  158. }
  159. }
  160. coreModule.service('annotationsSrv', AnnotationsSrv);