annotations_srv.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import './editor_ctrl';
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. export class AnnotationsSrv {
  6. globalAnnotationsPromise: any;
  7. alertStatesPromise: any;
  8. /** @ngInject */
  9. constructor(private $rootScope, private $q, private datasourceSrv, private backendSrv, private timeSrv) {
  10. $rootScope.onAppEvent('refresh', this.clearCache.bind(this), $rootScope);
  11. $rootScope.onAppEvent('dashboard-initialized', this.clearCache.bind(this), $rootScope);
  12. }
  13. clearCache() {
  14. this.globalAnnotationsPromise = null;
  15. this.alertStatesPromise = null;
  16. }
  17. getAnnotations(options) {
  18. return this.$q
  19. .all([this.getGlobalAnnotations(options), this.getAlertStates(options)])
  20. .then(results => {
  21. // combine the annotations and flatten results
  22. var annotations = _.flattenDeep(results[0]);
  23. // filter out annotations that do not belong to requesting panel
  24. annotations = _.filter(annotations, item => {
  25. // if event has panel id and query is of type dashboard then panel and requesting panel id must match
  26. if (item.panelId && item.source.type === 'dashboard') {
  27. return item.panelId === options.panel.id;
  28. }
  29. return true;
  30. });
  31. annotations = dedupAnnotations(annotations);
  32. annotations = makeRegions(annotations, options);
  33. // look for alert state for this panel
  34. var alertState = _.find(results[1], {panelId: options.panel.id});
  35. return {
  36. annotations: annotations,
  37. alertState: alertState,
  38. };
  39. })
  40. .catch(err => {
  41. if (!err.message && err.data && err.data.message) {
  42. err.message = err.data.message;
  43. }
  44. console.log('AnnotationSrv.query error', err);
  45. this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', err.message || err]);
  46. return [];
  47. });
  48. }
  49. getAlertStates(options) {
  50. if (!options.dashboard.id) {
  51. return this.$q.when([]);
  52. }
  53. // ignore if no alerts
  54. if (options.panel && !options.panel.alert) {
  55. return this.$q.when([]);
  56. }
  57. if (options.range.raw.to !== 'now') {
  58. return this.$q.when([]);
  59. }
  60. if (this.alertStatesPromise) {
  61. return this.alertStatesPromise;
  62. }
  63. this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {
  64. dashboardId: options.dashboard.id,
  65. });
  66. return this.alertStatesPromise;
  67. }
  68. getGlobalAnnotations(options) {
  69. var dashboard = options.dashboard;
  70. if (this.globalAnnotationsPromise) {
  71. return this.globalAnnotationsPromise;
  72. }
  73. var range = this.timeSrv.timeRange();
  74. var promises = [];
  75. for (let annotation of dashboard.annotations.list) {
  76. if (!annotation.enable) {
  77. continue;
  78. }
  79. if (annotation.snapshotData) {
  80. return this.translateQueryResult(annotation, annotation.snapshotData);
  81. }
  82. promises.push(
  83. this.datasourceSrv
  84. .get(annotation.datasource)
  85. .then(datasource => {
  86. // issue query against data source
  87. return datasource.annotationQuery({
  88. range: range,
  89. rangeRaw: range.raw,
  90. annotation: annotation,
  91. dashboard: dashboard,
  92. });
  93. })
  94. .then(results => {
  95. // store response in annotation object if this is a snapshot call
  96. if (dashboard.snapshot) {
  97. annotation.snapshotData = angular.copy(results);
  98. }
  99. // translate result
  100. return this.translateQueryResult(annotation, results);
  101. }),
  102. );
  103. }
  104. this.globalAnnotationsPromise = this.$q.all(promises);
  105. return this.globalAnnotationsPromise;
  106. }
  107. saveAnnotationEvent(annotation) {
  108. this.globalAnnotationsPromise = null;
  109. return this.backendSrv.post('/api/annotations', annotation);
  110. }
  111. updateAnnotationEvent(annotation) {
  112. this.globalAnnotationsPromise = null;
  113. return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation);
  114. }
  115. deleteAnnotationEvent(annotation) {
  116. this.globalAnnotationsPromise = null;
  117. let deleteUrl = `/api/annotations/${annotation.id}`;
  118. if (annotation.isRegion) {
  119. deleteUrl = `/api/annotations/region/${annotation.regionId}`;
  120. }
  121. return this.backendSrv.delete(deleteUrl);
  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. }
  133. return results;
  134. }
  135. }
  136. /**
  137. * This function converts annotation events into set
  138. * of single events and regions (event consist of two)
  139. * @param annotations
  140. * @param options
  141. */
  142. function makeRegions(annotations, options) {
  143. let [regionEvents, singleEvents] = _.partition(annotations, 'regionId');
  144. let regions = getRegions(regionEvents, options.range);
  145. annotations = _.concat(regions, singleEvents);
  146. return annotations;
  147. }
  148. function getRegions(events, range) {
  149. let region_events = _.filter(events, event => {
  150. return event.regionId;
  151. });
  152. let regions = _.groupBy(region_events, 'regionId');
  153. regions = _.compact(
  154. _.map(regions, region_events => {
  155. let region_obj = _.head(region_events);
  156. if (region_events && region_events.length > 1) {
  157. region_obj.timeEnd = region_events[1].time;
  158. region_obj.isRegion = true;
  159. return region_obj;
  160. } else {
  161. if (region_events && region_events.length) {
  162. // Don't change proper region object
  163. if (!region_obj.time || !region_obj.timeEnd) {
  164. // This is cut region
  165. if (isStartOfRegion(region_obj)) {
  166. region_obj.timeEnd = range.to.valueOf() - 1;
  167. } else {
  168. // Start time = null
  169. region_obj.timeEnd = region_obj.time;
  170. region_obj.time = range.from.valueOf() + 1;
  171. }
  172. region_obj.isRegion = true;
  173. }
  174. return region_obj;
  175. }
  176. }
  177. }),
  178. );
  179. return regions;
  180. }
  181. function isStartOfRegion(event): boolean {
  182. return event.id && event.id === event.regionId;
  183. }
  184. function dedupAnnotations(annotations) {
  185. let dedup = [];
  186. // Split events by annotationId property existance
  187. let events = _.partition(annotations, 'id');
  188. let eventsById = _.groupBy(events[0], 'id');
  189. dedup = _.map(eventsById, eventGroup => {
  190. if (eventGroup.length > 1 && !_.every(eventGroup, isPanelAlert)) {
  191. // Get first non-panel alert
  192. return _.find(eventGroup, event => {
  193. return event.eventType !== 'panel-alert';
  194. });
  195. } else {
  196. return _.head(eventGroup);
  197. }
  198. });
  199. dedup = _.concat(dedup, events[1]);
  200. return dedup;
  201. }
  202. function isPanelAlert(event) {
  203. return event.eventType === 'panel-alert';
  204. }
  205. coreModule.service('annotationsSrv', AnnotationsSrv);