gaugesEngine.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. *
  3. *
  4. */
  5. function MySQLBackendGaugesEngine() {
  6. var self = this;
  7. }
  8. /**
  9. *
  10. *
  11. */
  12. MySQLBackendGaugesEngine.prototype.buildQuerries = function(gauges, time_stamp) {
  13. var querries = [];
  14. // Iterate on each gauge
  15. for(var gaugeName in gauges) {
  16. var gaugeValue = gauges[gaugeName];
  17. if(gaugeValue === 0) {
  18. continue;
  19. } else {
  20. /**********************************************************************
  21. * Edit following line to customize where statsd datas are inserted
  22. *
  23. * Parameters :
  24. * - gaugeName: Gauge name
  25. * - gaugeValue: Gauge value
  26. */
  27. // This SQL request checks if the last value for this particular gauge is the same as gaugeValue.
  28. // If it is the same, we do nothing.
  29. // If it is different, we insert a new line.
  30. // If gaugeName does not exist in the table, we insert a new line
  31. // The -678 value, is totally arbitrary, I just assumed that there was never gonna be a gauge with a -678 value. You can change it to any value not used by your gauges ;)
  32. querries.push("INSERT INTO `gauges_statistics` (`timestamp`, `name`, `value`) VALUES ("+time_stamp+", '"+gaugeName+"', "+gaugeValue+");");
  33. // querries.push("insert into `gauges_statistics` select "+time_stamp+", '"+gaugeName+"', "+gaugeValue+" from dual where (select if(max(value),max(value),-678) from `gauges_statistics` where name = '"+gaugeName+"') = -678 OR (select value from `gauges_statistics` where name = '"+gaugeName+"' order by timestamp desc limit 0,1) <> "+gaugeValue+";")
  34. }
  35. }
  36. return querries;
  37. }
  38. /**
  39. *
  40. *
  41. */
  42. exports.init = function() {
  43. var instance = new MySQLBackendGaugesEngine();
  44. return instance;
  45. };