app.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. var getOK = true;
  8. var restarting = false;
  9. $scope.connections = {};
  10. $scope.config = {};
  11. $scope.myID = '';
  12. $scope.nodes = [];
  13. $scope.configInSync = true;
  14. $scope.errors = [];
  15. $scope.seenError = '';
  16. $scope.model = {};
  17. $scope.repos = [];
  18. // Strings before bools look better
  19. $scope.settings = [
  20. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  21. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KBps)', type: 'number', restart: true},
  22. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  23. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  24. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  25. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KBps)', type: 'number', restart: true},
  26. {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
  27. {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
  28. {id: 'StartBrowser', descr: 'Start Browser', type: 'bool'},
  29. {id: 'UPnPEnabled', descr: 'Enable UPnP', type: 'bool'},
  30. ];
  31. $scope.guiSettings = [
  32. {id: 'Address', descr: 'GUI Listen Addresses', type: 'text', restart: true},
  33. {id: 'User', descr: 'GUI Authentication User', type: 'text', restart: true},
  34. {id: 'Password', descr: 'GUI Authentication Password', type: 'password', restart: true},
  35. ];
  36. function getSucceeded() {
  37. if (!getOK) {
  38. $('#networkError').modal('hide');
  39. getOK = true;
  40. }
  41. if (restarting) {
  42. $('#restarting').modal('hide');
  43. restarting = false;
  44. }
  45. }
  46. function getFailed() {
  47. if (restarting) {
  48. return;
  49. }
  50. if (getOK) {
  51. $('#networkError').modal({backdrop: 'static', keyboard: false});
  52. getOK = false;
  53. }
  54. }
  55. function nodeCompare(a, b) {
  56. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  57. if (a.Name < b.Name)
  58. return -1;
  59. return a.Name > b.Name;
  60. }
  61. if (a.NodeID < b.NodeID) {
  62. return -1;
  63. }
  64. return a.NodeID > b.NodeID;
  65. }
  66. $scope.refresh = function () {
  67. $http.get('/rest/system').success(function (data) {
  68. getSucceeded();
  69. $scope.system = data;
  70. }).error(function () {
  71. getFailed();
  72. });
  73. $scope.repos.forEach(function (repo) {
  74. $http.get('/rest/model/' + repo.ID).success(function (data) {
  75. $scope.model[repo.ID] = data;
  76. });
  77. });
  78. $http.get('/rest/connections').success(function (data) {
  79. var now = Date.now(),
  80. td = (now - prevDate) / 1000,
  81. id;
  82. prevDate = now;
  83. $scope.inbps = 0;
  84. $scope.outbps = 0;
  85. for (id in data) {
  86. if (!data.hasOwnProperty(id)) {
  87. continue;
  88. }
  89. try {
  90. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  91. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  92. } catch (e) {
  93. data[id].inbps = 0;
  94. data[id].outbps = 0;
  95. }
  96. $scope.inbps += data[id].inbps;
  97. $scope.outbps += data[id].outbps;
  98. }
  99. $scope.connections = data;
  100. });
  101. $http.get('/rest/errors').success(function (data) {
  102. $scope.errors = data;
  103. });
  104. };
  105. $scope.repoStatus = function (repo) {
  106. if (typeof $scope.model[repo] === 'undefined') {
  107. return 'Unknown';
  108. }
  109. var state = '' + $scope.model[repo].state;
  110. state = state[0].toUpperCase() + state.substr(1);
  111. if (state == "Syncing" || state == "Idle") {
  112. state += " (" + $scope.syncPercentage(repo) + "%)";
  113. }
  114. return state;
  115. }
  116. $scope.repoClass = function (repo) {
  117. if (typeof $scope.model[repo] === 'undefined') {
  118. return 'text-info';
  119. }
  120. var state = '' + $scope.model[repo].state;
  121. if (state == 'idle') {
  122. return 'text-success';
  123. }
  124. if (state == 'syncing') {
  125. return 'text-primary';
  126. }
  127. return 'text-info';
  128. }
  129. $scope.syncPercentage = function (repo) {
  130. if (typeof $scope.model[repo] === 'undefined') {
  131. return 100;
  132. }
  133. if ($scope.model[repo].globalBytes === 0) {
  134. return 100;
  135. }
  136. var pct = 100 * $scope.model[repo].inSyncBytes / $scope.model[repo].globalBytes;
  137. return Math.ceil(pct);
  138. };
  139. $scope.nodeStatus = function (nodeCfg) {
  140. var conn = $scope.connections[nodeCfg.NodeID];
  141. if (conn) {
  142. if (conn.Completion === 100) {
  143. return 'In Sync';
  144. } else {
  145. return 'Syncing (' + conn.Completion + '%)';
  146. }
  147. }
  148. return 'Disconnected';
  149. };
  150. $scope.nodeIcon = function (nodeCfg) {
  151. var conn = $scope.connections[nodeCfg.NodeID];
  152. if (conn) {
  153. if (conn.Completion === 100) {
  154. return 'ok';
  155. } else {
  156. return 'refresh';
  157. }
  158. }
  159. return 'minus';
  160. };
  161. $scope.nodeClass = function (nodeCfg) {
  162. var conn = $scope.connections[nodeCfg.NodeID];
  163. if (conn) {
  164. if (conn.Completion === 100) {
  165. return 'success';
  166. } else {
  167. return 'primary';
  168. }
  169. }
  170. return 'info';
  171. };
  172. $scope.nodeAddr = function (nodeCfg) {
  173. var conn = $scope.connections[nodeCfg.NodeID];
  174. if (conn) {
  175. return conn.Address;
  176. }
  177. return '?';
  178. };
  179. $scope.nodeCompletion = function (nodeCfg) {
  180. var conn = $scope.connections[nodeCfg.NodeID];
  181. if (conn) {
  182. return conn.Completion + '%';
  183. }
  184. return '';
  185. };
  186. $scope.nodeVer = function (nodeCfg) {
  187. if (nodeCfg.NodeID === $scope.myID) {
  188. return $scope.version;
  189. }
  190. var conn = $scope.connections[nodeCfg.NodeID];
  191. if (conn) {
  192. return conn.ClientVersion;
  193. }
  194. return '?';
  195. };
  196. $scope.nodeName = function (nodeCfg) {
  197. if (nodeCfg.Name) {
  198. return nodeCfg.Name;
  199. }
  200. return nodeCfg.NodeID.substr(0, 6);
  201. };
  202. $scope.editSettings = function () {
  203. $('#settings').modal({backdrop: 'static', keyboard: true});
  204. }
  205. $scope.saveSettings = function () {
  206. $scope.configInSync = false;
  207. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  208. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  209. $('#settings').modal("hide");
  210. };
  211. $scope.restart = function () {
  212. restarting = true;
  213. $('#restarting').modal('show');
  214. $http.post('/rest/restart');
  215. $scope.configInSync = true;
  216. };
  217. $scope.editNode = function (nodeCfg) {
  218. $scope.currentNode = $.extend({}, nodeCfg);
  219. $scope.editingExisting = true;
  220. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  221. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  222. $('#editNode').modal({backdrop: 'static', keyboard: true});
  223. };
  224. $scope.addNode = function () {
  225. $scope.currentNode = {AddressesStr: 'dynamic'};
  226. $scope.editingExisting = false;
  227. $scope.editingSelf = false;
  228. $('#editNode').modal({backdrop: 'static', keyboard: true});
  229. };
  230. $scope.deleteNode = function () {
  231. $('#editNode').modal('hide');
  232. if (!$scope.editingExisting) {
  233. return;
  234. }
  235. $scope.nodes = $scope.nodes.filter(function (n) {
  236. return n.NodeID !== $scope.currentNode.NodeID;
  237. });
  238. $scope.config.Nodes = $scope.nodes;
  239. for (var i = 0; i < $scope.repos.length; i++) {
  240. $scope.repos[i].Nodes = $scope.repos[i].Nodes.filter(function (n) {
  241. return n.NodeID !== $scope.currentNode.NodeID;
  242. });
  243. }
  244. $scope.configInSync = false;
  245. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  246. };
  247. $scope.saveNode = function () {
  248. var nodeCfg, done, i;
  249. $scope.configInSync = false;
  250. $('#editNode').modal('hide');
  251. nodeCfg = $scope.currentNode;
  252. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  253. done = false;
  254. for (i = 0; i < $scope.nodes.length; i++) {
  255. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  256. $scope.nodes[i] = nodeCfg;
  257. done = true;
  258. break;
  259. }
  260. }
  261. if (!done) {
  262. $scope.nodes.push(nodeCfg);
  263. }
  264. $scope.nodes.sort(nodeCompare);
  265. $scope.config.Nodes = $scope.nodes;
  266. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  267. };
  268. $scope.otherNodes = function () {
  269. return $scope.nodes.filter(function (n){
  270. return n.NodeID !== $scope.myID;
  271. });
  272. };
  273. $scope.thisNode = function () {
  274. var i, n;
  275. for (i = 0; i < $scope.nodes.length; i++) {
  276. n = $scope.nodes[i];
  277. if (n.NodeID === $scope.myID) {
  278. return [n];
  279. }
  280. }
  281. };
  282. $scope.errorList = function () {
  283. return $scope.errors.filter(function (e) {
  284. return e.Time > $scope.seenError;
  285. });
  286. };
  287. $scope.clearErrors = function () {
  288. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  289. $http.post('/rest/error/clear');
  290. };
  291. $scope.friendlyNodes = function (str) {
  292. for (var i = 0; i < $scope.nodes.length; i++) {
  293. var cfg = $scope.nodes[i];
  294. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  295. }
  296. return str;
  297. };
  298. $scope.editRepo = function (nodeCfg) {
  299. $scope.currentRepo = $.extend({selectedNodes: {}}, nodeCfg);
  300. $scope.currentRepo.Nodes.forEach(function (n) {
  301. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  302. });
  303. $scope.editingExisting = true;
  304. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  305. };
  306. $scope.addRepo = function () {
  307. $scope.currentRepo = {selectedNodes: {}};
  308. $scope.editingExisting = false;
  309. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  310. };
  311. $scope.saveRepo = function () {
  312. var repoCfg, done, i;
  313. $scope.configInSync = false;
  314. $('#editRepo').modal('hide');
  315. repoCfg = $scope.currentRepo;
  316. repoCfg.Nodes = [];
  317. repoCfg.selectedNodes[$scope.myID] = true;
  318. for (var nodeID in repoCfg.selectedNodes) {
  319. if (repoCfg.selectedNodes[nodeID] === true) {
  320. repoCfg.Nodes.push({NodeID: nodeID});
  321. }
  322. }
  323. delete repoCfg.selectedNodes;
  324. done = false;
  325. for (i = 0; i < $scope.repos.length; i++) {
  326. if ($scope.repos[i].ID === repoCfg.ID) {
  327. $scope.repos[i] = repoCfg;
  328. done = true;
  329. break;
  330. }
  331. }
  332. if (!done) {
  333. $scope.repos.push(repoCfg);
  334. }
  335. $scope.config.Repositories = $scope.repos;
  336. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  337. };
  338. $scope.deleteRepo = function () {
  339. $('#editRepo').modal('hide');
  340. if (!$scope.editingExisting) {
  341. return;
  342. }
  343. $scope.repos = $scope.repos.filter(function (r) {
  344. return r.ID !== $scope.currentRepo.ID;
  345. });
  346. $scope.config.Repositories = $scope.repos;
  347. $scope.configInSync = false;
  348. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  349. };
  350. $http.get('/rest/version').success(function (data) {
  351. $scope.version = data;
  352. });
  353. $http.get('/rest/system').success(function (data) {
  354. $scope.system = data;
  355. $scope.myID = data.myID;
  356. });
  357. $http.get('/rest/config').success(function (data) {
  358. $scope.config = data;
  359. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  360. var nodes = $scope.config.Nodes;
  361. nodes.sort(nodeCompare);
  362. $scope.nodes = nodes;
  363. $scope.repos = $scope.config.Repositories;
  364. $scope.refresh();
  365. });
  366. $http.get('/rest/config/sync').success(function (data) {
  367. $scope.configInSync = data.configInSync;
  368. });
  369. setInterval($scope.refresh, 10000);
  370. });
  371. function decimals(val, num) {
  372. var digits, decs;
  373. if (val === 0) {
  374. return 0;
  375. }
  376. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  377. decs = Math.max(0, num - digits);
  378. return decs;
  379. }
  380. syncthing.filter('natural', function () {
  381. return function (input, valid) {
  382. return input.toFixed(decimals(input, valid));
  383. };
  384. });
  385. syncthing.filter('binary', function () {
  386. return function (input) {
  387. if (input === undefined) {
  388. return '0 ';
  389. }
  390. if (input > 1024 * 1024 * 1024) {
  391. input /= 1024 * 1024 * 1024;
  392. return input.toFixed(decimals(input, 2)) + ' Gi';
  393. }
  394. if (input > 1024 * 1024) {
  395. input /= 1024 * 1024;
  396. return input.toFixed(decimals(input, 2)) + ' Mi';
  397. }
  398. if (input > 1024) {
  399. input /= 1024;
  400. return input.toFixed(decimals(input, 2)) + ' Ki';
  401. }
  402. return Math.round(input) + ' ';
  403. };
  404. });
  405. syncthing.filter('metric', function () {
  406. return function (input) {
  407. if (input === undefined) {
  408. return '0 ';
  409. }
  410. if (input > 1000 * 1000 * 1000) {
  411. input /= 1000 * 1000 * 1000;
  412. return input.toFixed(decimals(input, 2)) + ' G';
  413. }
  414. if (input > 1000 * 1000) {
  415. input /= 1000 * 1000;
  416. return input.toFixed(decimals(input, 2)) + ' M';
  417. }
  418. if (input > 1000) {
  419. input /= 1000;
  420. return input.toFixed(decimals(input, 2)) + ' k';
  421. }
  422. return Math.round(input) + ' ';
  423. };
  424. });
  425. syncthing.filter('short', function () {
  426. return function (input) {
  427. return input.substr(0, 6);
  428. };
  429. });
  430. syncthing.filter('alwaysNumber', function () {
  431. return function (input) {
  432. if (input === undefined) {
  433. return 0;
  434. }
  435. return input;
  436. };
  437. });
  438. syncthing.directive('optionEditor', function () {
  439. return {
  440. restrict: 'C',
  441. replace: true,
  442. transclude: true,
  443. scope: {
  444. setting: '=setting',
  445. },
  446. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  447. };
  448. });