annotations_srv.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ///<reference path="../../headers/common.d.ts" />
  2. import './editor_ctrl';
  3. import angular from 'angular';
  4. import _ from 'lodash';
  5. import coreModule from 'app/core/core_module';
  6. export class AnnotationsSrv {
  7. globalAnnotationsPromise: any;
  8. alertStatesPromise: any;
  9. /** @ngInject */
  10. constructor(private $rootScope,
  11. private $q,
  12. private datasourceSrv,
  13. private backendSrv,
  14. private timeSrv) {
  15. $rootScope.onAppEvent('refresh', this.clearCache.bind(this), $rootScope);
  16. $rootScope.onAppEvent('dashboard-initialized', this.clearCache.bind(this), $rootScope);
  17. }
  18. clearCache() {
  19. this.globalAnnotationsPromise = null;
  20. this.alertStatesPromise = null;
  21. }
  22. getAnnotations(options) {
  23. return this.$q.all([
  24. this.getGlobalAnnotations(options),
  25. this.getPanelAnnotations(options),
  26. this.getAlertStates(options)
  27. ]).then(results => {
  28. // combine the annotations and flatten results
  29. var annotations = _.flattenDeep([results[0], results[1]]);
  30. // filter out annotations that do not belong to requesting panel
  31. annotations = _.filter(annotations, item => {
  32. // shownIn === 1 requires annotation matching panel id
  33. if (item.source.showIn === 1) {
  34. if (item.panelId && options.panel.id === item.panelId) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. return true;
  40. });
  41. // look for alert state for this panel
  42. var alertState = _.find(results[2], {panelId: options.panel.id});
  43. return {
  44. annotations: annotations,
  45. alertState: alertState,
  46. };
  47. }).catch(err => {
  48. if (!err.message && err.data && err.data.message) {
  49. err.message = err.data.message;
  50. }
  51. this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', (err.message || err)]);
  52. return [];
  53. });
  54. }
  55. getPanelAnnotations(options) {
  56. var panel = options.panel;
  57. var dashboard = options.dashboard;
  58. if (dashboard.id && panel && panel.alert) {
  59. return this.backendSrv.get('/api/annotations', {
  60. from: options.range.from.valueOf(),
  61. to: options.range.to.valueOf(),
  62. limit: 100,
  63. panelId: panel.id,
  64. dashboardId: dashboard.id,
  65. }).then(results => {
  66. // this built in annotation source name `panel-alert` is used in annotation tooltip
  67. // to know that this annotation is from panel alert
  68. return this.translateQueryResult({iconColor: '#AA0000', name: 'panel-alert'}, results);
  69. });
  70. }
  71. return this.$q.when([]);
  72. }
  73. getAlertStates(options) {
  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', {dashboardId: options.dashboard.id});
  88. return this.alertStatesPromise;
  89. }
  90. getGlobalAnnotations(options) {
  91. var dashboard = options.dashboard;
  92. if (dashboard.annotations.list.length === 0) {
  93. return this.$q.when([]);
  94. }
  95. if (this.globalAnnotationsPromise) {
  96. return this.globalAnnotationsPromise;
  97. }
  98. var annotations = _.filter(dashboard.annotations.list, {enable: true});
  99. var range = this.timeSrv.timeRange();
  100. this.globalAnnotationsPromise = this.$q.all(_.map(annotations, annotation => {
  101. if (annotation.snapshotData) {
  102. return this.translateQueryResult(annotation, annotation.snapshotData);
  103. }
  104. return this.datasourceSrv.get(annotation.datasource).then(datasource => {
  105. // issue query against data source
  106. return datasource.annotationQuery({range: range, rangeRaw: range.raw, annotation: annotation});
  107. })
  108. .then(results => {
  109. // store response in annotation object if this is a snapshot call
  110. if (dashboard.snapshot) {
  111. annotation.snapshotData = angular.copy(results);
  112. }
  113. // translate result
  114. return this.translateQueryResult(annotation, results);
  115. });
  116. }));
  117. return this.globalAnnotationsPromise;
  118. }
  119. saveAnnotationEvent(annotation) {
  120. this.globalAnnotationsPromise = null;
  121. return this.backendSrv.post('/api/annotations', annotation);
  122. }
  123. translateQueryResult(annotation, results) {
  124. // if annotation has snapshotData
  125. // make clone and remove it
  126. if (annotation.snapshotData) {
  127. annotation = angular.copy(annotation);
  128. delete annotation.snapshotData;
  129. }
  130. for (var item of results) {
  131. item.source = annotation;
  132. item.min = item.time;
  133. item.max = item.time;
  134. item.scope = 1;
  135. item.eventType = annotation.name;
  136. }
  137. return results;
  138. }
  139. }
  140. coreModule.service('annotationsSrv', AnnotationsSrv);