app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*jslint browser: true, continue: true, plusplus: true */
  2. /*global $: false, angular: false */
  3. 'use strict';
  4. var syncthing = angular.module('syncthing', []);
  5. syncthing.controller('SyncthingCtrl', function ($scope, $http) {
  6. var prevDate = 0,
  7. modelGetOK = true;
  8. $scope.connections = {};
  9. $scope.config = {};
  10. $scope.myID = '';
  11. $scope.nodes = [];
  12. $scope.configInSync = true;
  13. $scope.errors = [];
  14. $scope.seenError = '';
  15. // Strings before bools look better
  16. $scope.settings = [
  17. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  18. {id: 'GUIAddress', descr: 'GUI Listen Address', type: 'text', restart: true},
  19. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KBps)', type: 'number', restart: true},
  20. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  21. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  22. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  23. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KBps)', type: 'number', restart: true},
  24. {id: 'ReadOnly', descr: 'Read Only', type: 'bool', restart: true},
  25. {id: 'AllowDelete', descr: 'Allow Delete', type: 'bool', restart: true},
  26. {id: 'FollowSymlinks', descr: 'Follow Symlinks', type: 'bool', restart: true},
  27. {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
  28. {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
  29. ];
  30. function modelGetSucceeded() {
  31. if (!modelGetOK) {
  32. $('#networkError').modal('hide');
  33. modelGetOK = true;
  34. }
  35. }
  36. function modelGetFailed() {
  37. if (modelGetOK) {
  38. $('#networkError').modal({backdrop: 'static', keyboard: false});
  39. modelGetOK = false;
  40. }
  41. }
  42. function nodeCompare(a, b) {
  43. if (a.NodeID === $scope.myID) {
  44. return -1;
  45. }
  46. if (b.NodeID === $scope.myID) {
  47. return 1;
  48. }
  49. if (a.NodeID < b.NodeID) {
  50. return -1;
  51. }
  52. return a.NodeID > b.NodeID;
  53. }
  54. $http.get('/rest/version').success(function (data) {
  55. $scope.version = data;
  56. });
  57. $http.get('/rest/system').success(function (data) {
  58. $scope.system = data;
  59. $scope.myID = data.myID;
  60. $http.get('/rest/config').success(function (data) {
  61. $scope.config = data;
  62. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  63. var nodes = $scope.config.Repositories[0].Nodes;
  64. nodes.sort(nodeCompare);
  65. $scope.nodes = nodes;
  66. });
  67. $http.get('/rest/config/sync').success(function (data) {
  68. $scope.configInSync = data.configInSync;
  69. });
  70. });
  71. $scope.refresh = function () {
  72. $http.get('/rest/system').success(function (data) {
  73. $scope.system = data;
  74. });
  75. $http.get('/rest/model').success(function (data) {
  76. $scope.model = data;
  77. modelGetSucceeded();
  78. }).error(function () {
  79. modelGetFailed();
  80. });
  81. $http.get('/rest/connections').success(function (data) {
  82. var now = Date.now(),
  83. td = (now - prevDate) / 1000,
  84. id;
  85. prevDate = now;
  86. $scope.inbps = 0;
  87. $scope.outbps = 0;
  88. for (id in data) {
  89. if (!data.hasOwnProperty(id)) {
  90. continue;
  91. }
  92. try {
  93. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  94. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  95. } catch (e) {
  96. data[id].inbps = 0;
  97. data[id].outbps = 0;
  98. }
  99. $scope.inbps += data[id].inbps;
  100. $scope.outbps += data[id].outbps;
  101. }
  102. $scope.connections = data;
  103. });
  104. $http.get('/rest/need').success(function (data) {
  105. var i, name;
  106. for (i = 0; i < data.length; i++) {
  107. name = data[i].Name.split('/');
  108. data[i].ShortName = name[name.length - 1];
  109. }
  110. data.sort(function (a, b) {
  111. if (a.ShortName < b.ShortName) {
  112. return -1;
  113. }
  114. if (a.ShortName > b.ShortName) {
  115. return 1;
  116. }
  117. return 0;
  118. });
  119. $scope.need = data;
  120. });
  121. $http.get('/rest/errors').success(function (data) {
  122. $scope.errors = data;
  123. });
  124. };
  125. $scope.nodeIcon = function (nodeCfg) {
  126. if ($scope.connections[nodeCfg.NodeID]) {
  127. return 'ok';
  128. }
  129. return 'minus';
  130. };
  131. $scope.nodeStatus = function (nodeCfg) {
  132. if ($scope.connections[nodeCfg.NodeID]) {
  133. return 'Connected';
  134. }
  135. return 'Disconnected';
  136. };
  137. $scope.nodeIcon = function (nodeCfg) {
  138. if ($scope.connections[nodeCfg.NodeID]) {
  139. return 'ok';
  140. }
  141. return 'minus';
  142. };
  143. $scope.nodeClass = function (nodeCfg) {
  144. var conn = $scope.connections[nodeCfg.NodeID];
  145. if (conn) {
  146. return 'success';
  147. }
  148. return 'info';
  149. };
  150. $scope.nodeAddr = function (nodeCfg) {
  151. var conn = $scope.connections[nodeCfg.NodeID];
  152. if (conn) {
  153. return conn.Address;
  154. }
  155. return '(unknown address)';
  156. };
  157. $scope.nodeVer = function (nodeCfg) {
  158. if (nodeCfg.NodeID === $scope.myID) {
  159. return $scope.version;
  160. }
  161. var conn = $scope.connections[nodeCfg.NodeID];
  162. if (conn) {
  163. return conn.ClientVersion;
  164. }
  165. return '(unknown version)';
  166. };
  167. $scope.nodeName = function (nodeCfg) {
  168. if (nodeCfg.Name) {
  169. return nodeCfg.Name;
  170. }
  171. return nodeCfg.NodeID.substr(0, 6);
  172. };
  173. $scope.saveSettings = function () {
  174. $scope.configInSync = false;
  175. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  176. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  177. $('#settingsTable').collapse('hide');
  178. };
  179. $scope.restart = function () {
  180. $http.post('/rest/restart');
  181. $scope.configInSync = true;
  182. };
  183. $scope.editNode = function (nodeCfg) {
  184. $scope.currentNode = nodeCfg;
  185. $scope.editingExisting = true;
  186. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  187. $('#editNode').modal({backdrop: 'static', keyboard: false});
  188. };
  189. $scope.addNode = function () {
  190. $scope.currentNode = {NodeID: '', AddressesStr: 'dynamic'};
  191. $scope.editingExisting = false;
  192. $('#editNode').modal({backdrop: 'static', keyboard: false});
  193. };
  194. $scope.deleteNode = function () {
  195. var newNodes = [], i;
  196. $('#editNode').modal('hide');
  197. if (!$scope.editingExisting) {
  198. return;
  199. }
  200. for (i = 0; i < $scope.nodes.length; i++) {
  201. if ($scope.nodes[i].NodeID !== $scope.currentNode.NodeID) {
  202. newNodes.push($scope.nodes[i]);
  203. }
  204. }
  205. $scope.nodes = newNodes;
  206. $scope.config.Repositories[0].Nodes = newNodes;
  207. $scope.configInSync = false;
  208. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  209. };
  210. $scope.saveNode = function () {
  211. var nodeCfg, done, i;
  212. $scope.configInSync = false;
  213. $('#editNode').modal('hide');
  214. nodeCfg = $scope.currentNode;
  215. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  216. done = false;
  217. for (i = 0; i < $scope.nodes.length; i++) {
  218. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  219. $scope.nodes[i] = nodeCfg;
  220. done = true;
  221. break;
  222. }
  223. }
  224. if (!done) {
  225. $scope.nodes.push(nodeCfg);
  226. }
  227. $scope.nodes.sort(nodeCompare);
  228. $scope.config.Repositories[0].Nodes = $scope.nodes;
  229. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  230. };
  231. $scope.otherNodes = function () {
  232. var nodes = [], i, n;
  233. for (i = 0; i < $scope.nodes.length; i++) {
  234. n = $scope.nodes[i];
  235. if (n.NodeID !== $scope.myID) {
  236. nodes.push(n);
  237. }
  238. }
  239. return nodes;
  240. };
  241. $scope.thisNode = function () {
  242. var i, n;
  243. for (i = 0; i < $scope.nodes.length; i++) {
  244. n = $scope.nodes[i];
  245. if (n.NodeID === $scope.myID) {
  246. return [n];
  247. }
  248. }
  249. };
  250. $scope.errorList = function () {
  251. var errors = [];
  252. for (var i = 0; i < $scope.errors.length; i++) {
  253. var e = $scope.errors[i];
  254. if (e.Time > $scope.seenError) {
  255. errors.push(e);
  256. }
  257. }
  258. return errors;
  259. };
  260. $scope.clearErrors = function () {
  261. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  262. };
  263. $scope.friendlyNodes = function (str) {
  264. for (var i = 0; i < $scope.nodes.length; i++) {
  265. var cfg = $scope.nodes[i];
  266. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  267. }
  268. return str;
  269. };
  270. $scope.refresh();
  271. setInterval($scope.refresh, 10000);
  272. });
  273. function decimals(val, num) {
  274. var digits, decs;
  275. if (val === 0) {
  276. return 0;
  277. }
  278. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  279. decs = Math.max(0, num - digits);
  280. return decs;
  281. }
  282. syncthing.filter('natural', function () {
  283. return function (input, valid) {
  284. return input.toFixed(decimals(input, valid));
  285. };
  286. });
  287. syncthing.filter('binary', function () {
  288. return function (input) {
  289. if (input === undefined) {
  290. return '0 ';
  291. }
  292. if (input > 1024 * 1024 * 1024) {
  293. input /= 1024 * 1024 * 1024;
  294. return input.toFixed(decimals(input, 2)) + ' Gi';
  295. }
  296. if (input > 1024 * 1024) {
  297. input /= 1024 * 1024;
  298. return input.toFixed(decimals(input, 2)) + ' Mi';
  299. }
  300. if (input > 1024) {
  301. input /= 1024;
  302. return input.toFixed(decimals(input, 2)) + ' Ki';
  303. }
  304. return Math.round(input) + ' ';
  305. };
  306. });
  307. syncthing.filter('metric', function () {
  308. return function (input) {
  309. if (input === undefined) {
  310. return '0 ';
  311. }
  312. if (input > 1000 * 1000 * 1000) {
  313. input /= 1000 * 1000 * 1000;
  314. return input.toFixed(decimals(input, 2)) + ' G';
  315. }
  316. if (input > 1000 * 1000) {
  317. input /= 1000 * 1000;
  318. return input.toFixed(decimals(input, 2)) + ' M';
  319. }
  320. if (input > 1000) {
  321. input /= 1000;
  322. return input.toFixed(decimals(input, 2)) + ' k';
  323. }
  324. return Math.round(input) + ' ';
  325. };
  326. });
  327. syncthing.filter('short', function () {
  328. return function (input) {
  329. return input.substr(0, 6);
  330. };
  331. });
  332. syncthing.filter('alwaysNumber', function () {
  333. return function (input) {
  334. if (input === undefined) {
  335. return 0;
  336. }
  337. return input;
  338. };
  339. });
  340. syncthing.directive('optionEditor', function () {
  341. return {
  342. restrict: 'C',
  343. replace: true,
  344. transclude: true,
  345. scope: {
  346. setting: '=setting',
  347. },
  348. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  349. };
  350. });