Browse Source

adding sets support

dpacaud 12 years ago
parent
commit
468ee86118
3 changed files with 106 additions and 4 deletions
  1. 37 0
      engines/setsEngine.js
  2. 61 4
      mysql-backend.js
  3. 8 0
      tables/sets_statistics.sql

+ 37 - 0
engines/setsEngine.js

@@ -0,0 +1,37 @@
+/**
+ *
+ *
+ */
+function MySQLBackendGaugesEngine() {
+	var self = this;
+}
+
+
+/**
+ *
+ *
+ */
+MySQLBackendGaugesEngine.prototype.buildQuerries = function(sets, time_stamp) {
+
+	var querries = [];
+	 // Iterate on each gauge
+    for(var setName in sets) {
+      var setCount = sets[setName].values().length;
+      if(setCount === 0) {
+        continue;
+      } else {
+          querries.push("insert into `sets_statistics` values (" + time_stamp + ",'" + setName + "'," + setCount + ");");  
+      }
+    }
+    return querries;
+}
+
+
+/**
+ *
+ *
+ */
+exports.init = function() {
+	var instance = new MySQLBackendGaugesEngine();
+  return instance;
+};

+ 61 - 4
mysql-backend.js

@@ -65,7 +65,7 @@ function StatdMySQLBackend(startupTime, config, emitter) {
 
   //Default tables
   if(!this.config.tables) {
-    this.config.tables = {counters: ["counters_statistics"], gauges: ["gauges_statistics"], timers:["timers_statistics"]};
+    this.config.tables = {counters: ["counters_statistics"], gauges: ["gauges_statistics"], timers:["timers_statistics"],sets:["sets_statistics"]};
   }
 
   // Default engines
@@ -74,7 +74,7 @@ function StatdMySQLBackend(startupTime, config, emitter) {
       counters: ["engines/countersEngine.js"],
       gauges: ["engines/gaugesEngine.js"],
       timers: ["engines/timersEngine.js"],
-      sets: []
+      sets: ["engines/setsEngine.js"]
     };
   }
   
@@ -346,7 +346,7 @@ StatdMySQLBackend.prototype.onFlush = function(time_stamp, metrics) {
   var sets = metrics['sets'];
   var pctThreshold = metrics['pctThreshold'];
 
-  console.log("METRICS : \n " + util.inspect(metrics) + "\n ===========================");
+  //console.log("METRICS : \n " + util.inspect(metrics) + "\n ===========================");
 
   // Handle statsd counters
   self.handleCounters(counters,time_stamp);
@@ -356,6 +356,9 @@ StatdMySQLBackend.prototype.onFlush = function(time_stamp, metrics) {
   
   // Handle statsd timers
   self.handleTimers(timers,time_stamp);
+  
+  // Handle stastd sets
+  self.handleSets(sets,time_stamp);
 
 }
 
@@ -485,7 +488,6 @@ StatdMySQLBackend.prototype.handleGauges = function(_gauges, time_stamp) {
  * @param _timers received timers
  * @param time_stamp flush time_stamp 
  */
- //TODO : à implémenter
 StatdMySQLBackend.prototype.handleTimers = function(_timers, time_stamp) {
   var self = this;
   
@@ -534,6 +536,61 @@ StatdMySQLBackend.prototype.handleTimers = function(_timers, time_stamp) {
   }
 }
 
+/**
+ * Handle and process received sets 
+ * 
+ * @param _sets received sets
+ * @param time_stamp flush time_stamp 
+ */
+StatdMySQLBackend.prototype.handleSets = function(_sets, time_stamp) {
+  var self = this;
+  
+  var setsSize = 0
+  for(var s in _sets) { setsSize++; }
+
+  // If timers received
+  if(setsSize > 0) {
+    console.log("sets received !");
+    console.log("Sets = " + util.inspect(_sets));
+    var querries = [];
+
+    // Open MySQL connection
+    var canExecuteQuerries = self.openMySqlConnection();
+    if(canExecuteQuerries) {
+        //////////////////////////////////////////////////////////////////////
+        // Call buildQuerries method on each counterEngine
+        for(var setsEngineIndex in self.engines.sets) {
+          console.log("setsEngineIndex = " + setsEngineIndex);
+          var setsEngine = self.engines.sets[setsEngineIndex];
+
+          // Add current engine querries to querries list
+          var engineQuerries = setsEngine.buildQuerries(_sets, time_stamp);
+          querries = querries.concat(engineQuerries);
+
+          // Insert data into database every 100 query
+          if(querries.length >= 100) {
+            // Execute querries
+            self.executeQuerries(querries);
+            querries = [];
+          }
+
+        }
+
+        if(querries.length > 0) {
+          // Execute querries
+          self.executeQuerries(querries);
+          querries = [];
+        }
+    } else {
+      console.log("Unable to open db connection !");
+    }
+
+    // Close MySQL Connection
+    self.closeMySqlConnection();
+  }
+}
+
+
 /**
  * MISSING DOCUMENTATION 
  * 

+ 8 - 0
tables/sets_statistics.sql

@@ -0,0 +1,8 @@
+-- Stadard DELIMITER is $$
+
+-- Timers statistics table
+CREATE  TABLE `sets_statistics` (
+	`timestamp` BIGINT NOT NULL ,
+	`name` VARCHAR(255) NOT NULL ,
+	`value` BIGINT NOT NULL ,
+PRIMARY KEY (`timestamp`,`name`) )$$