stat.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. const request = require('request');
  7. const version = require('../configs').version_full;
  8. const url = 'http://lab.oldj.net/s.gif';
  9. const session_id = (new Date()).getTime() + ':' + Math.random();
  10. const queue = [];
  11. let is_initialized = false;
  12. function log(action) {
  13. let u = url + '?' + [
  14. 'app=sh3',
  15. 'action=' + encodeURIComponent(action),
  16. 'v=' + encodeURIComponent(version),
  17. 'os=' + process.platform,
  18. 'sid=' + encodeURIComponent(session_id),
  19. '_r=' + Math.random()
  20. ].join('&');
  21. console.log('stat: ' + action);
  22. request.get(u)
  23. .on('response', function(response) {
  24. // console.log('log ' + response.statusCode); // 200
  25. })
  26. .on('error', function(err) {
  27. console.log(err);
  28. });
  29. }
  30. function record(action) {
  31. queue.push(action);
  32. }
  33. function send() {
  34. if (queue.length === 0) return;
  35. let action = queue.splice(0).join(',');
  36. log(action);
  37. }
  38. function init() {
  39. if (is_initialized) return;
  40. is_initialized = true;
  41. record('launch');
  42. SH_event.on('toggle_host', () => {
  43. record('switch');
  44. });
  45. setInterval(function () {
  46. // 每一段时间自动打点
  47. record('tick');
  48. }, 60 * 1000 * 42);
  49. setInterval(() => {
  50. send();
  51. }, 5000);
  52. }
  53. exports.init = init;