annotations_srv.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (item.panelId && options.panel.id !== item.panelId) {
  34. return false;
  35. }
  36. return true;
  37. });
  38. // look for alert state for this panel
  39. var alertState = _.find(results[2], {panelId: options.panel.id});
  40. return {
  41. annotations: annotations,
  42. alertState: alertState,
  43. };
  44. }).catch(err => {
  45. this.$rootScope.appEvent('alert-error', ['Annotations failed', (err.message || err)]);
  46. });
  47. }
  48. getPanelAnnotations(options) {
  49. var panel = options.panel;
  50. var dashboard = options.dashboard;
  51. if (panel) {
  52. return this.backendSrv.get('/api/annotations', {
  53. from: options.range.from.valueOf(),
  54. to: options.range.to.valueOf(),
  55. limit: 100,
  56. panelId: panel.id,
  57. dashboardId: dashboard.id,
  58. }).then(results => {
  59. return this.translateQueryResult({iconColor: '#AA0000', name: 'panel-alert'}, results);
  60. });
  61. }
  62. return this.$q.when([]);
  63. }
  64. getAlertStates(options) {
  65. if (!options.dashboard.id) {
  66. return this.$q.when([]);
  67. }
  68. // ignore if no alerts
  69. if (options.panel && !options.panel.alert) {
  70. return this.$q.when([]);
  71. }
  72. if (options.range.raw.to !== 'now') {
  73. return this.$q.when([]);
  74. }
  75. if (this.alertStatesPromise) {
  76. return this.alertStatesPromise;
  77. }
  78. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {dashboardId: options.dashboard.id});
  79. return this.alertStatesPromise;
  80. }
  81. getGlobalAnnotations(options) {
  82. var dashboard = options.dashboard;
  83. if (dashboard.annotations.list.length === 0) {
  84. return this.$q.when([]);
  85. }
  86. if (this.globalAnnotationsPromise) {
  87. return this.globalAnnotationsPromise;
  88. }
  89. var annotations = _.filter(dashboard.annotations.list, {enable: true});
  90. var range = this.timeSrv.timeRange();
  91. this.globalAnnotationsPromise = this.$q.all(_.map(annotations, annotation => {
  92. if (annotation.snapshotData) {
  93. return this.translateQueryResult(annotation, annotation.snapshotData);
  94. }
  95. return this.datasourceSrv.get(annotation.datasource).then(datasource => {
  96. // issue query against data source
  97. return datasource.annotationQuery({range: range, rangeRaw: range.raw, annotation: annotation});
  98. })
  99. .then(results => {
  100. // store response in annotation object if this is a snapshot call
  101. if (dashboard.snapshot) {
  102. annotation.snapshotData = angular.copy(results);
  103. }
  104. // translate result
  105. return this.translateQueryResult(annotation, results);
  106. });
  107. }));
  108. return this.globalAnnotationsPromise;
  109. }
  110. postAnnotation(annotation) {
  111. return this.backendSrv.post('/api/annotations', annotation);
  112. }
  113. translateQueryResult(annotation, results) {
  114. for (var item of results) {
  115. item.source = annotation;
  116. item.min = item.time;
  117. item.max = item.time;
  118. item.scope = 1;
  119. item.eventType = annotation.name;
  120. }
  121. return results;
  122. }
  123. }
  124. coreModule.service('annotationsSrv', AnnotationsSrv);