annotations_srv.ts 5.1 KB

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