app.js 12 KB

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