Procházet zdrojové kódy

Annotations refactoring

Torkel Ödegaard před 12 roky
rodič
revize
7b906aa229
1 změnil soubory, kde provedl 64 přidání a 49 odebrání
  1. 64 49
      src/app/services/annotationsSrv.js

+ 64 - 49
src/app/services/annotationsSrv.js

@@ -10,6 +10,7 @@ define([
   module.service('annotationsSrv', function(dashboard, datasourceSrv, $q, alertSrv, $rootScope) {
   module.service('annotationsSrv', function(dashboard, datasourceSrv, $q, alertSrv, $rootScope) {
     var promiseCached;
     var promiseCached;
     var annotationPanel;
     var annotationPanel;
+    var list = [];
 
 
     this.init = function() {
     this.init = function() {
       $rootScope.$on('refresh', this.clearCache);
       $rootScope.$on('refresh', this.clearCache);
@@ -24,11 +25,12 @@ define([
 
 
     this.clearCache = function() {
     this.clearCache = function() {
       promiseCached = null;
       promiseCached = null;
+      list = [];
     };
     };
 
 
     this.getAnnotations = function(rangeUnparsed) {
     this.getAnnotations = function(rangeUnparsed) {
       if (!annotationPanel.enable) {
       if (!annotationPanel.enable) {
-        return $q.when(null);
+        return $q.when([]);
       }
       }
 
 
       if (promiseCached) {
       if (promiseCached) {
@@ -39,22 +41,21 @@ define([
       var graphiteEvents = this.getGraphiteEvents(rangeUnparsed);
       var graphiteEvents = this.getGraphiteEvents(rangeUnparsed);
 
 
       promiseCached = $q.all([graphiteMetrics, graphiteEvents])
       promiseCached = $q.all([graphiteMetrics, graphiteEvents])
-        .then(function(allAnnotations) {
-          var nonNull = _.filter(allAnnotations, function(value) { return value !== null; });
-          return _.flatten(nonNull);
+        .then(function() {
+          return list;
         });
         });
 
 
       return promiseCached;
       return promiseCached;
     };
     };
 
 
     this.getGraphiteEvents = function(rangeUnparsed) {
     this.getGraphiteEvents = function(rangeUnparsed) {
-      var annotations = _.where(annotationPanel.annotations, { type: 'graphite events', enable: true });
-      var tags = _.pluck(annotations, 'tags');
-
-      if (tags.length === 0) {
+      var annotations = this.getAnnotationsByType('graphite events');
+      if (annotations.length === 0) {
         return $q.when(null);
         return $q.when(null);
       }
       }
 
 
+      var tags = _.pluck(annotations, 'tags');
+
       var eventsQuery = {
       var eventsQuery = {
         range: rangeUnparsed,
         range: rangeUnparsed,
         tags: tags.join(' '),
         tags: tags.join(' '),
@@ -62,69 +63,83 @@ define([
 
 
       return datasourceSrv.default.events(eventsQuery)
       return datasourceSrv.default.events(eventsQuery)
         .then(function(results) {
         .then(function(results) {
-          var list = [];
           _.each(results.data, function (event) {
           _.each(results.data, function (event) {
             list.push(createAnnotation(annotations[0], event.when * 1000, event.what, event.tags, event.data));
             list.push(createAnnotation(annotations[0], event.when * 1000, event.what, event.tags, event.data));
           });
           });
-          return list;
         })
         })
-        .then(null, function() {
-          alertSrv.set('Annotations','Could not fetch annotations','error');
-        });
+        .then(null, errorHandler);
     };
     };
 
 
-    this.getGraphiteMetrics = function(rangeUnparsed) {
-      var graphiteAnnotations = _.where(annotationPanel.annotations, { type: 'graphite metric', enable: true });
-      var graphiteTargets = _.map(graphiteAnnotations, function(annotation) {
-        return { target: annotation.target };
+    this.getAnnotationsByType = function(type) {
+      return _.where(annotationPanel.annotations, {
+        type: type,
+        enable: true
       });
       });
+    };
 
 
-      if (graphiteTargets.length === 0) {
+    this.getGraphiteMetrics = function(rangeUnparsed) {
+      var annotations = this.getAnnotationsByType('graphite metric');
+      if (annotations.length === 0) {
         return $q.when(null);
         return $q.when(null);
       }
       }
 
 
-      var graphiteQuery = {
-        range: rangeUnparsed,
-        targets: graphiteTargets,
-        format: 'json',
-        maxDataPoints: 100
-      };
+      var promises = _.map(annotations, function(annotation) {
+        var graphiteQuery = {
+          range: rangeUnparsed,
+          targets: [{ target: annotation.target }],
+          format: 'json',
+          maxDataPoints: 100
+        };
 
 
-      return datasourceSrv.default.query(graphiteQuery)
-        .then(function(results) {
-          return _.reduce(results.data, function(list, target) {
-            _.each(target.datapoints, function (values) {
-              if (values[0] === null) {
-                return;
-              }
+        var receiveFunc = _.partial(receiveGraphiteMetrics, annotation);
 
 
-              list.push(createAnnotation(graphiteAnnotations[0], values[1] * 1000, target.target));
-            });
+        return datasourceSrv.default.query(graphiteQuery)
+          .then(receiveFunc)
+          .then(null, errorHandler);
+      });
 
 
-            return list;
-          }, []);
-        })
-        .then(null, function() {
-          alertSrv.set('Annotations','Could not fetch annotations','error');
-        });
+      return $q.all(promises);
     };
     };
 
 
-    function createAnnotation(annotation, time, description, tags, data) {
-      var tooltip = "<small><b>" + description + "</b><br/>";
-      if (tags) {
-        tooltip += (tags || '') + '<br/>';
+    function errorHandler(err) {
+      console.log('Annotation error: ', err);
+      alertSrv.set('Annotations','Could not fetch annotations','error');
+    }
+
+    function receiveGraphiteMetrics(annotation, results) {
+      for (var i = 0; i < results.data.length; i++) {
+        var target = results.data[i];
+
+        for (var y = 0; y < target.datapoints.length; y++) {
+          var datapoint = target.datapoints[y];
+
+          if (datapoint[0] !== null) {
+            list.push(createAnnotation({
+              annotation: annotation,
+              time: datapoint[1] * 1000,
+              description: target.target
+            }));
+          }
+        }
+      }
+    }
+
+    function createAnnotation(options) {
+      var tooltip = "<small><b>" + options.description + "</b><br/>";
+      if (options.tags) {
+        tooltip += (options.tags || '') + '<br/>';
       }
       }
-      tooltip += '<i>' + moment(time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
-      if (data) {
+      tooltip += '<i>' + moment(options.time).format('YYYY-MM-DD HH:mm:ss') + '</i><br/>';
+      if (options.data) {
         tooltip += data;
         tooltip += data;
       }
       }
       tooltip += "</small>";
       tooltip += "</small>";
 
 
       return {
       return {
-        annotation: annotation,
-        min: time,
-        max: time,
-        eventType: annotation.name,
+        annotation: options.annotation,
+        min: options.time,
+        max: options.time,
+        eventType: options.annotation.name,
         title: null,
         title: null,
         description: tooltip,
         description: tooltip,
         score: 1
         score: 1