annotations_srv.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. this.$rootScope.appEvent('alert-error', ['Annotations failed', (err.message || err)]);
  50. });
  51. }
  52. getPanelAnnotations(options) {
  53. var panel = options.panel;
  54. var dashboard = options.dashboard;
  55. if (dashboard.id && panel && panel.alert) {
  56. return this.backendSrv.get('/api/annotations', {
  57. from: options.range.from.valueOf(),
  58. to: options.range.to.valueOf(),
  59. limit: 100,
  60. panelId: panel.id,
  61. dashboardId: dashboard.id,
  62. }).then(results => {
  63. // this built in annotation source name `panel-alert` is used in annotation tooltip
  64. // to know that this annotation is from panel alert
  65. return this.translateQueryResult({iconColor: '#AA0000', name: 'panel-alert'}, results);
  66. });
  67. }
  68. return this.$q.when([]);
  69. }
  70. getAlertStates(options) {
  71. if (!options.dashboard.id) {
  72. return this.$q.when([]);
  73. }
  74. // ignore if no alerts
  75. if (options.panel && !options.panel.alert) {
  76. return this.$q.when([]);
  77. }
  78. if (options.range.raw.to !== 'now') {
  79. return this.$q.when([]);
  80. }
  81. if (this.alertStatesPromise) {
  82. return this.alertStatesPromise;
  83. }
  84. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {dashboardId: options.dashboard.id});
  85. return this.alertStatesPromise;
  86. }
  87. getGlobalAnnotations(options) {
  88. var dashboard = options.dashboard;
  89. if (dashboard.annotations.list.length === 0) {
  90. return this.$q.when([]);
  91. }
  92. if (this.globalAnnotationsPromise) {
  93. return this.globalAnnotationsPromise;
  94. }
  95. var annotations = _.filter(dashboard.annotations.list, {enable: true});
  96. var range = this.timeSrv.timeRange();
  97. this.globalAnnotationsPromise = this.$q.all(_.map(annotations, annotation => {
  98. if (annotation.snapshotData) {
  99. return this.translateQueryResult(annotation, annotation.snapshotData);
  100. }
  101. return this.datasourceSrv.get(annotation.datasource).then(datasource => {
  102. // issue query against data source
  103. return datasource.annotationQuery({range: range, rangeRaw: range.raw, annotation: annotation});
  104. })
  105. .then(results => {
  106. // store response in annotation object if this is a snapshot call
  107. if (dashboard.snapshot) {
  108. annotation.snapshotData = angular.copy(results);
  109. }
  110. // translate result
  111. return this.translateQueryResult(annotation, results);
  112. });
  113. }));
  114. return this.globalAnnotationsPromise;
  115. }
  116. saveAnnotationEvent(annotation) {
  117. this.globalAnnotationsPromise = null;
  118. return this.backendSrv.post('/api/annotations', annotation);
  119. }
  120. translateQueryResult(annotation, results) {
  121. // if annotation has snapshotData
  122. // make clone and remove it
  123. if (annotation.snapshotData) {
  124. annotation = angular.copy(annotation);
  125. delete annotation.snapshotData;
  126. }
  127. for (var item of results) {
  128. item.source = annotation;
  129. item.min = item.time;
  130. item.max = item.time;
  131. item.scope = 1;
  132. item.eventType = annotation.name;
  133. }
  134. return results;
  135. }
  136. }
  137. coreModule.service('annotationsSrv', AnnotationsSrv);