annotations_srv.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. export class AnnotationsSrv {
  15. globalAnnotationsPromise: any;
  16. alertStatesPromise: any;
  17. datasourcePromises: any;
  18. /** @ngInject */
  19. constructor(
  20. private $rootScope: any,
  21. private $q: IQService,
  22. private datasourceSrv: DatasourceSrv,
  23. private backendSrv: BackendSrv,
  24. private timeSrv: TimeSrv
  25. ) {}
  26. init(dashboard: DashboardModel) {
  27. // always clearPromiseCaches when loading new dashboard
  28. this.clearPromiseCaches();
  29. // clear promises on refresh events
  30. dashboard.on('refresh', this.clearPromiseCaches.bind(this));
  31. }
  32. clearPromiseCaches() {
  33. this.globalAnnotationsPromise = null;
  34. this.alertStatesPromise = null;
  35. this.datasourcePromises = null;
  36. }
  37. getAnnotations(options: any) {
  38. return this.$q
  39. .all([this.getGlobalAnnotations(options), this.getAlertStates(options)])
  40. .then(results => {
  41. // combine the annotations and flatten results
  42. let annotations: any[] = _.flattenDeep(results[0]);
  43. // filter out annotations that do not belong to requesting panel
  44. annotations = _.filter(annotations, item => {
  45. // if event has panel id and query is of type dashboard then panel and requesting panel id must match
  46. if (item.panelId && item.source.type === 'dashboard') {
  47. return item.panelId === options.panel.id;
  48. }
  49. return true;
  50. });
  51. annotations = dedupAnnotations(annotations);
  52. annotations = makeRegions(annotations, options);
  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: any) {
  131. this.globalAnnotationsPromise = null;
  132. return this.backendSrv.post('/api/annotations', annotation);
  133. }
  134. updateAnnotationEvent(annotation: { id: any }) {
  135. this.globalAnnotationsPromise = null;
  136. return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
  137. }
  138. deleteAnnotationEvent(annotation: { id: any; isRegion: any; regionId: any }) {
  139. this.globalAnnotationsPromise = null;
  140. let deleteUrl = `/api/annotations/${annotation.id}`;
  141. if (annotation.isRegion) {
  142. deleteUrl = `/api/annotations/region/${annotation.regionId}`;
  143. }
  144. return this.backendSrv.delete(deleteUrl);
  145. }
  146. translateQueryResult(annotation: any, results: any) {
  147. // if annotation has snapshotData
  148. // make clone and remove it
  149. if (annotation.snapshotData) {
  150. annotation = angular.copy(annotation);
  151. delete annotation.snapshotData;
  152. }
  153. for (const item of results) {
  154. item.source = annotation;
  155. }
  156. return results;
  157. }
  158. }
  159. coreModule.service('annotationsSrv', AnnotationsSrv);