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 { 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. for (let i = 0; i < annotations.length; i++) {
  54. const a = annotations[i];
  55. a.isRegion = a.time !== a.timeEnd;
  56. }
  57. // look for alert state for this panel
  58. const alertState: any = _.find(results[1], { panelId: options.panel.id });
  59. return {
  60. annotations: annotations,
  61. alertState: alertState,
  62. };
  63. })
  64. .catch(err => {
  65. if (!err.message && err.data && err.data.message) {
  66. err.message = err.data.message;
  67. }
  68. console.log('AnnotationSrv.query error', err);
  69. this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', err.message || err]);
  70. return [];
  71. });
  72. }
  73. getAlertStates(options: any) {
  74. if (!options.dashboard.id) {
  75. return this.$q.when([]);
  76. }
  77. // ignore if no alerts
  78. if (options.panel && !options.panel.alert) {
  79. return this.$q.when([]);
  80. }
  81. if (options.range.raw.to !== 'now') {
  82. return this.$q.when([]);
  83. }
  84. if (this.alertStatesPromise) {
  85. return this.alertStatesPromise;
  86. }
  87. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {
  88. dashboardId: options.dashboard.id,
  89. });
  90. return this.alertStatesPromise;
  91. }
  92. getGlobalAnnotations(options: any) {
  93. const dashboard = options.dashboard;
  94. if (this.globalAnnotationsPromise) {
  95. return this.globalAnnotationsPromise;
  96. }
  97. const range = this.timeSrv.timeRange();
  98. const promises = [];
  99. const dsPromises = [];
  100. for (const annotation of dashboard.annotations.list) {
  101. if (!annotation.enable) {
  102. continue;
  103. }
  104. if (annotation.snapshotData) {
  105. return this.translateQueryResult(annotation, annotation.snapshotData);
  106. }
  107. const datasourcePromise = this.datasourceSrv.get(annotation.datasource);
  108. dsPromises.push(datasourcePromise);
  109. promises.push(
  110. datasourcePromise
  111. .then((datasource: any) => {
  112. // issue query against data source
  113. return datasource.annotationQuery({
  114. range: range,
  115. rangeRaw: range.raw,
  116. annotation: annotation,
  117. dashboard: dashboard,
  118. });
  119. })
  120. .then(results => {
  121. // store response in annotation object if this is a snapshot call
  122. if (dashboard.snapshot) {
  123. annotation.snapshotData = angular.copy(results);
  124. }
  125. // translate result
  126. return this.translateQueryResult(annotation, results);
  127. })
  128. );
  129. }
  130. this.datasourcePromises = this.$q.all(dsPromises);
  131. this.globalAnnotationsPromise = this.$q.all(promises);
  132. return this.globalAnnotationsPromise;
  133. }
  134. saveAnnotationEvent(annotation: AnnotationEvent) {
  135. this.globalAnnotationsPromise = null;
  136. return this.backendSrv.post('/api/annotations', annotation);
  137. }
  138. updateAnnotationEvent(annotation: AnnotationEvent) {
  139. this.globalAnnotationsPromise = null;
  140. return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
  141. }
  142. deleteAnnotationEvent(annotation: AnnotationEvent) {
  143. this.globalAnnotationsPromise = null;
  144. const deleteUrl = `/api/annotations/${annotation.id}`;
  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);