app.js 4.1 KB

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