app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var syncthing = angular.module('syncthing', []);
  2. syncthing.controller('SyncthingCtrl', function ($scope, $http) {
  3. $http.get("/rest/version").success(function (data) {
  4. $scope.version = data;
  5. });
  6. $http.get("/rest/config").success(function (data) {
  7. $scope.config = data;
  8. });
  9. var prevDate = 0;
  10. $scope.refresh = function () {
  11. $http.get("/rest/model").success(function (data) {
  12. $scope.model = data;
  13. });
  14. $http.get("/rest/connections").success(function (data) {
  15. var now = Date.now();
  16. var td = (now - prevDate) / 1000;
  17. prevDate = now;
  18. for (var id in data) {
  19. try {
  20. data[id].inbps = 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td;
  21. data[id].outbps = 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td;
  22. } catch (e) {
  23. data[id].inbps = 0;
  24. data[id].outbps = 0;
  25. }
  26. }
  27. $scope.connections = data;
  28. });
  29. $http.get("/rest/need").success(function (data) {
  30. var i, name;
  31. for (i = 0; i < data.length; i++) {
  32. name = data[i].Name.split("/");
  33. data[i].ShortName = name[name.length-1];
  34. }
  35. data.sort(function (a, b) {
  36. if (a.ShortName < b.ShortName) {
  37. return -1;
  38. }
  39. if (a.ShortName > b.ShortName) {
  40. return 1;
  41. }
  42. return 0;
  43. });
  44. $scope.need = data;
  45. });
  46. };
  47. $scope.refresh();
  48. setInterval($scope.refresh, 10000);
  49. });
  50. function decimals(num) {
  51. if (num > 100) {
  52. return 0;
  53. }
  54. if (num > 10) {
  55. return 1;
  56. }
  57. return 2;
  58. }
  59. syncthing.filter('binary', function() {
  60. return function(input) {
  61. if (input === undefined) {
  62. return '- '
  63. }
  64. if (input > 1024 * 1024 * 1024) {
  65. input /= 1024 * 1024 * 1024;
  66. return input.toFixed(decimals(input)) + ' Gi';
  67. }
  68. if (input > 1024 * 1024) {
  69. input /= 1024 * 1024;
  70. return input.toFixed(decimals(input)) + ' Mi';
  71. }
  72. if (input > 1024) {
  73. input /= 1024;
  74. return input.toFixed(decimals(input)) + ' Ki';
  75. }
  76. return Math.round(input) + ' ';
  77. }
  78. });
  79. syncthing.filter('metric', function() {
  80. return function(input) {
  81. if (input === undefined) {
  82. return '- '
  83. }
  84. if (input > 1000 * 1000 * 1000) {
  85. input /= 1000 * 1000 * 1000;
  86. return input.toFixed(decimals(input)) + ' G';
  87. }
  88. if (input > 1000 * 1000) {
  89. input /= 1000 * 1000;
  90. return input.toFixed(decimals(input)) + ' M';
  91. }
  92. if (input > 1000) {
  93. input /= 1000;
  94. return input.toFixed(decimals(input)) + ' k';
  95. }
  96. return Math.round(input) + ' ';
  97. }
  98. });
  99. syncthing.filter('short', function() {
  100. return function(input) {
  101. return input.substr(0, 6);
  102. }
  103. });
  104. syncthing.filter('alwaysNumber', function() {
  105. return function(input) {
  106. if (input === undefined) {
  107. return 0;
  108. }
  109. return input;
  110. }
  111. });