annotations_srv.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. console.log(item);
  34. // shownIn === 1 requires annotation matching panel id
  35. if (item.source.showIn === 1) {
  36. if (item.panelId && options.panel.id === item.panelId) {
  37. return true;
  38. }
  39. return false;
  40. }
  41. return true;
  42. });
  43. // look for alert state for this panel
  44. var alertState = _.find(results[2], {panelId: options.panel.id});
  45. return {
  46. annotations: annotations,
  47. alertState: alertState,
  48. };
  49. }).catch(err => {
  50. this.$rootScope.appEvent('alert-error', ['Annotations failed', (err.message || err)]);
  51. });
  52. }
  53. getPanelAnnotations(options) {
  54. var panel = options.panel;
  55. var dashboard = options.dashboard;
  56. if (panel && panel.alert) {
  57. return this.backendSrv.get('/api/annotations', {
  58. from: options.range.from.valueOf(),
  59. to: options.range.to.valueOf(),
  60. limit: 100,
  61. panelId: panel.id,
  62. dashboardId: dashboard.id,
  63. }).then(results => {
  64. return this.translateQueryResult({iconColor: '#AA0000', name: 'panel-alert'}, results);
  65. });
  66. }
  67. return this.$q.when([]);
  68. }
  69. getAlertStates(options) {
  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', {dashboardId: options.dashboard.id});
  84. return this.alertStatesPromise;
  85. }
  86. getGlobalAnnotations(options) {
  87. var dashboard = options.dashboard;
  88. if (dashboard.annotations.list.length === 0) {
  89. return this.$q.when([]);
  90. }
  91. if (this.globalAnnotationsPromise) {
  92. return this.globalAnnotationsPromise;
  93. }
  94. var annotations = _.filter(dashboard.annotations.list, {enable: true});
  95. var range = this.timeSrv.timeRange();
  96. this.globalAnnotationsPromise = this.$q.all(_.map(annotations, annotation => {
  97. if (annotation.snapshotData) {
  98. return this.translateQueryResult(annotation, annotation.snapshotData);
  99. }
  100. return this.datasourceSrv.get(annotation.datasource).then(datasource => {
  101. // issue query against data source
  102. return datasource.annotationQuery({range: range, rangeRaw: range.raw, annotation: annotation});
  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. return this.globalAnnotationsPromise;
  114. }
  115. saveAnnotationEvent(annotation) {
  116. this.globalAnnotationsPromise = null;
  117. return this.backendSrv.post('/api/annotations', annotation);
  118. }
  119. translateQueryResult(annotation, results) {
  120. for (var item of results) {
  121. item.source = annotation;
  122. item.min = item.time;
  123. item.max = item.time;
  124. item.scope = 1;
  125. item.eventType = annotation.name;
  126. }
  127. return results;
  128. }
  129. }
  130. coreModule.service('annotationsSrv', AnnotationsSrv);