stats.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  2. Gun.on('opt', function(root){
  3. this.to.next(root);
  4. if(root.once){ return }
  5. if(typeof process === 'undefined'){ return }
  6. if(typeof require === 'undefined'){ return }
  7. if(false === root.opt.stats){ return }
  8. var noop = function(){};
  9. var os = require('os') || {};
  10. var fs = require('fs') || {};
  11. fs.existsSync = fs.existsSync || require('path').existsSync;
  12. if(!fs.existsSync){ return }
  13. if(!process){ return }
  14. process.uptime = process.uptime || noop;
  15. process.cpuUsage = process.cpuUsage || noop;
  16. process.memoryUsage = process.memoryUsage || noop;
  17. os.totalmem = os.totalmem || noop;
  18. os.freemem = os.freemem || noop;
  19. os.loadavg = os.loadavg || noop;
  20. os.cpus = os.cpus || noop;
  21. setTimeout(function(){
  22. root.stats = Gun.obj.ify((fs.existsSync(__dirname+'/../stats.'+root.opt.file) && fs.readFileSync(__dirname+'/../stats.'+root.opt.file).toString())) || {};
  23. root.stats.up = root.stats.up || {};
  24. root.stats.up.start = root.stats.up.start || +(new Date);
  25. root.stats.up.count = (root.stats.up.count || 0) + 1;
  26. root.stats.stay = root.stats.stay || {};
  27. root.stats.gap = {};
  28. root.stats.over = +new Date;
  29. },1);
  30. setInterval(function(){
  31. if(!root.stats){ root.stats = {} }
  32. var S = +new Date;
  33. var stats = root.stats, tmp;
  34. stats.over = S - (stats.over||S);
  35. (stats.up||{}).time = process.uptime();
  36. stats.memory = process.memoryUsage() || {};
  37. stats.memory.totalmem = os.totalmem();
  38. stats.memory.freemem = os.freemem();
  39. stats.cpu = process.cpuUsage() || {};
  40. stats.cpu.loadavg = os.loadavg();
  41. stats.peers = {};
  42. stats.peers.count = Object.keys(root.opt.peers||{}).length;
  43. stats.node = {};
  44. stats.node.count = Object.keys(root.graph||{}).length;
  45. stats.all = all;
  46. var dam = root.opt.mesh;
  47. if(dam){
  48. stats.dam = {'in': {count: dam.hear.c, done: dam.hear.d}, 'out': {count: dam.say.c, done: dam.say.d}};
  49. dam.hear.c = dam.hear.d = dam.say.c = dam.say.d = 0; // reset
  50. stats.peers.time = dam.bye.time || 0;
  51. }
  52. var rad = root.opt.store; rad = rad && rad.stats;
  53. if(rad){
  54. stats.rad = rad;
  55. root.opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // reset
  56. }
  57. console.STAT && console.STAT(S, +new Date - S, 'stats');
  58. S = +new Date;
  59. fs.writeFile(__dirname+'/../stats.'+root.opt.file, JSON.stringify(stats, null, 2), function(err){ console.STAT && console.STAT(S, +new Date - S, 'stats stash') });
  60. stats.over = S;
  61. stats.gap = {};
  62. }, 1000 * 15);
  63. Object.keys = Object.keys || function(o){ return Gun.obj.map(o, function(v,k,t){t(k)}) }
  64. });
  65. var log = Gun.log, all = {}, max = 1000;
  66. Gun.log = console.STAT = function(a,b,c,d){
  67. if('number' == typeof a && 'number' == typeof b && 'string' == typeof c){
  68. var tmp = (all[c] || (all[c] = []));
  69. if(max < tmp.push([a,b])){ all[c] = [] } // reset
  70. //return;
  71. }
  72. if(!console.LOG || log.off){ return a }
  73. return log.apply(Gun, arguments);
  74. }