gaugesEngine.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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` 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+";")
  33. }
  34. }
  35. return querries;
  36. }
  37. /**
  38. *
  39. *
  40. */
  41. exports.init = function() {
  42. var instance = new MySQLBackendGaugesEngine();
  43. return instance;
  44. };