not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB

  1. // Interface to stats
  2. define(["settings","sugar-web/datastore"], function(preferences, datastore) {
  3. var stats = {};
  4. var source = 'Sugarizer';
  5. // Compute stat
  6. function computeStat(object, action, label, value) {
  7. var stat = {};
  8. stat.user_id = preferences.getNetworkId();
  9. stat.user_agent = navigator.userAgent;
  10. stat.timestamp = new Date().getTime();
  11. stat.client_type = util.getClientName();
  12. stat.event_source = source;
  13. stat.event_object = object;
  14. stat.event_action = action;
  15. stat.event_label = label;
  16. stat.event_value = value;
  17. return stat;
  18. }
  19. // Send log to the API
  20. function sendStats(statslist, ok, notOk) {
  21. myserver.postStats(statslist, function() {
  22. if (ok) ok();
  23. }, function(response, error) {
  24. if (response.code != 20) {
  25. console.log("Error sending log");
  26. }
  27. if (notOk) notOk();
  28. });
  29. }
  30. // Test is stat is active
  31. stats.isActive = function() {
  32. // No logging privacy
  33. if (!preferences.getOptions("stats")) {
  34. return false;
  35. }
  36. // Not connected
  37. if (!preferences.isConnected()) {
  38. return false;
  39. }
  40. // Statistics not active on server
  41. var info = preferences.getServer();
  42. if (!info || !info.options || !info.options.statistics) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. // Trace an action
  48. stats.trace = function(object, action, label, value) {
  49. // Stat is active ?
  50. if (!stats.isActive()) {
  51. return;
  52. }
  53. // Compute stat
  54. console.log(source+": "+action+"("+object+", "+label+", "+value+")");
  55. var stat = computeStat(object, action, label, value);
  56. // Add stat to localStorage array
  57. var statslist = datastore.localStorage.getValue('sugar_stats');
  58. if (!statslist) {
  59. statslist = [];
  60. }
  61. statslist.push(stat);
  62. // Send stats to server when package size reached
  63. datastore.localStorage.setValue('sugar_stats', statslist);
  64. if (statslist.length >= constant.statPackageSize) {
  65. console.log(source+": "+"Send stat to server");
  66. sendStats(statslist, function() {
  67. datastore.localStorage.setValue('sugar_stats', []);
  68. }, function() {
  69. // Ensure local array don't grow for ever
  70. if (statslist.length > constant.statMaxLocalSize) {
  71. statslist.shift();
  72. datastore.localStorage.setValue('sugar_stats', statslist);
  73. }
  74. });
  75. }
  76. }
  77. return stats;
  78. });