ソースを参照

Extracted multi-graphite query function, fixed loadRecursive

The new function in metricKeys generalizes what my loadAll
from the previous commit did, it takes a request and a
callback. The request is executed on each graphite installation
and the callback is executed for each result. This makes
implementing loadAll and loadRecursive almost as simple as
it was.
Harald Kraemer 11 年 前
コミット
52e1f5273c
1 ファイル変更15 行追加10 行削除
  1. 15 10
      src/app/controllers/metricKeys.js

+ 15 - 10
src/app/controllers/metricKeys.js

@@ -52,18 +52,23 @@ function (angular, _, config) {
     $scope.loadAll = function() {
     $scope.loadAll = function() {
       $scope.infoText = "Fetching all metrics from graphite...";
       $scope.infoText = "Fetching all metrics from graphite...";
 
 
-      return $q.all( _.map( config.datasources, function( datasource ) {
-          if ( datasource.type = 'graphite' ) {
-              return $http.get( datasource.url + "/metrics/index.json" )
-                    .then( saveMetricsArray );
-          } 
-      } ) ).then( function() {
+      getFromEachGraphite( '/metrics/index.json', saveMetricsArray )
+        .then( function() {
           $scope.infoText = "Indexing complete!";
           $scope.infoText = "Indexing complete!";
-      }).then(null, function(err) {
+        }).then(null, function(err) {
           $scope.errorText = err;
           $scope.errorText = err;
-      });
+        });
     };
     };
 
 
+    function getFromEachGraphite( request, data_callback, error_callback ) {
+      return $q.all( _.map( config.datasources, function( datasource ) {
+        if ( datasource.type = 'graphite' ) {
+          return $http.get( datasource.url + request )
+            .then( data_callback, error_callback );
+        } 
+      } ) );
+    }
+
     function saveMetricsArray(data, currentIndex)
     function saveMetricsArray(data, currentIndex)
     {
     {
       if (!data && !data.data && data.data.length === 0) {
       if (!data && !data.data && data.data.length === 0) {
@@ -82,6 +87,7 @@ function (angular, _, config) {
         });
         });
     }
     }
 
 
+    
     function deleteIndex()
     function deleteIndex()
     {
     {
       var deferred = $q.defer();
       var deferred = $q.defer();
@@ -157,7 +163,6 @@ function (angular, _, config) {
     function saveMetricKey(metricId) {
     function saveMetricKey(metricId) {
 
 
       // Create request with id as title. Rethink this.
       // Create request with id as title. Rethink this.
-      console.log( config.grafana_metrics_index, metricId );
       var request = $scope.ejs.Document(config.grafana_metrics_index, 'metricKey', metricId).source({
       var request = $scope.ejs.Document(config.grafana_metrics_index, 'metricKey', metricId).source({
         metricPath: metricId
         metricPath: metricId
       });
       });
@@ -175,7 +180,7 @@ function (angular, _, config) {
 
 
     function loadMetricsRecursive(metricPath)
     function loadMetricsRecursive(metricPath)
     {
     {
-      return $http.get(config.graphiteUrl + '/metrics/find/?query=' + metricPath).then(receiveMetric);
+      return getFromEachGraphite( '/metrics/find/?query=' + metricPath, receiveMetric );
     }
     }
 
 
   });
   });