annotations_srv.ts 4.9 KB

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