app.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*jslint browser: true, continue: true, plusplus: true */
  2. /*global $: false, angular: false */
  3. 'use strict';
  4. var syncthing = angular.module('syncthing', []);
  5. var urlbase = 'rest';
  6. syncthing.controller('SyncthingCtrl', function ($scope, $http) {
  7. var prevDate = 0;
  8. var getOK = true;
  9. var restarting = false;
  10. $scope.connections = {};
  11. $scope.config = {};
  12. $scope.myID = '';
  13. $scope.nodes = [];
  14. $scope.configInSync = true;
  15. $scope.errors = [];
  16. $scope.seenError = '';
  17. $scope.model = {};
  18. $scope.repos = {};
  19. // Strings before bools look better
  20. $scope.settings = [
  21. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  22. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KiB/s)', type: 'number', restart: true},
  23. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  24. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  25. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  26. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KiB/s)', type: 'number', 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. {id: 'UPnPEnabled', descr: 'Enable UPnP', type: 'bool'},
  31. ];
  32. $scope.guiSettings = [
  33. {id: 'Address', descr: 'GUI Listen Addresses', type: 'text', restart: true},
  34. {id: 'User', descr: 'GUI Authentication User', type: 'text', restart: true},
  35. {id: 'Password', descr: 'GUI Authentication Password', type: 'password', restart: true},
  36. {id: 'UseTLS', descr: 'Use HTTPS for GUI', type: 'bool', restart: true},
  37. ];
  38. function getSucceeded() {
  39. if (!getOK) {
  40. $scope.init();
  41. $('#networkError').modal('hide');
  42. getOK = true;
  43. }
  44. if (restarting) {
  45. $scope.init();
  46. $('#restarting').modal('hide');
  47. restarting = false;
  48. }
  49. }
  50. function getFailed() {
  51. if (restarting) {
  52. return;
  53. }
  54. if (getOK) {
  55. $('#networkError').modal({backdrop: 'static', keyboard: false});
  56. getOK = false;
  57. }
  58. }
  59. $scope.refresh = function () {
  60. $http.get(urlbase + '/system').success(function (data) {
  61. getSucceeded();
  62. $scope.system = data;
  63. }).error(function () {
  64. getFailed();
  65. });
  66. Object.keys($scope.repos).forEach(function (id) {
  67. $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
  68. $scope.model[id] = data;
  69. });
  70. });
  71. $http.get(urlbase + '/connections').success(function (data) {
  72. var now = Date.now(),
  73. td = (now - prevDate) / 1000,
  74. id;
  75. prevDate = now;
  76. $scope.inbps = 0;
  77. $scope.outbps = 0;
  78. for (id in data) {
  79. if (!data.hasOwnProperty(id)) {
  80. continue;
  81. }
  82. try {
  83. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  84. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  85. } catch (e) {
  86. data[id].inbps = 0;
  87. data[id].outbps = 0;
  88. }
  89. $scope.inbps += data[id].inbps;
  90. $scope.outbps += data[id].outbps;
  91. }
  92. $scope.connections = data;
  93. });
  94. $http.get(urlbase + '/errors').success(function (data) {
  95. $scope.errors = data;
  96. });
  97. };
  98. $scope.repoStatus = function (repo) {
  99. if (typeof $scope.model[repo] === 'undefined') {
  100. return 'Unknown';
  101. }
  102. if ($scope.model[repo].invalid !== '') {
  103. return 'Stopped';
  104. }
  105. var state = '' + $scope.model[repo].state;
  106. state = state[0].toUpperCase() + state.substr(1);
  107. if (state == "Syncing" || state == "Idle") {
  108. state += " (" + $scope.syncPercentage(repo) + "%)";
  109. }
  110. return state;
  111. }
  112. $scope.repoClass = function (repo) {
  113. if (typeof $scope.model[repo] === 'undefined') {
  114. return 'info';
  115. }
  116. if ($scope.model[repo].invalid !== '') {
  117. return 'danger';
  118. }
  119. var state = '' + $scope.model[repo].state;
  120. if (state == 'idle') {
  121. return 'success';
  122. }
  123. if (state == 'syncing') {
  124. return 'primary';
  125. }
  126. return 'info';
  127. }
  128. $scope.syncPercentage = function (repo) {
  129. if (typeof $scope.model[repo] === 'undefined') {
  130. return 100;
  131. }
  132. if ($scope.model[repo].globalBytes === 0) {
  133. return 100;
  134. }
  135. var pct = 100 * $scope.model[repo].inSyncBytes / $scope.model[repo].globalBytes;
  136. return Math.floor(pct);
  137. };
  138. $scope.nodeStatus = function (nodeCfg) {
  139. var conn = $scope.connections[nodeCfg.NodeID];
  140. if (conn) {
  141. if (conn.Completion === 100) {
  142. return 'In Sync';
  143. } else {
  144. return 'Syncing (' + conn.Completion + '%)';
  145. }
  146. }
  147. return 'Disconnected';
  148. };
  149. $scope.nodeIcon = function (nodeCfg) {
  150. var conn = $scope.connections[nodeCfg.NodeID];
  151. if (conn) {
  152. if (conn.Completion === 100) {
  153. return 'ok';
  154. } else {
  155. return 'refresh';
  156. }
  157. }
  158. return 'minus';
  159. };
  160. $scope.nodeClass = function (nodeCfg) {
  161. var conn = $scope.connections[nodeCfg.NodeID];
  162. if (conn) {
  163. if (conn.Completion === 100) {
  164. return 'success';
  165. } else {
  166. return 'primary';
  167. }
  168. }
  169. return 'info';
  170. };
  171. $scope.nodeAddr = function (nodeCfg) {
  172. var conn = $scope.connections[nodeCfg.NodeID];
  173. if (conn) {
  174. return conn.Address;
  175. }
  176. return '?';
  177. };
  178. $scope.nodeCompletion = function (nodeCfg) {
  179. var conn = $scope.connections[nodeCfg.NodeID];
  180. if (conn) {
  181. return conn.Completion + '%';
  182. }
  183. return '';
  184. };
  185. $scope.nodeVer = function (nodeCfg) {
  186. if (nodeCfg.NodeID === $scope.myID) {
  187. return $scope.version;
  188. }
  189. var conn = $scope.connections[nodeCfg.NodeID];
  190. if (conn) {
  191. return conn.ClientVersion;
  192. }
  193. return '?';
  194. };
  195. $scope.findNode = function (nodeID) {
  196. var matches = $scope.nodes.filter(function (n) { return n.NodeID == nodeID; });
  197. if (matches.length != 1) {
  198. return undefined;
  199. }
  200. return matches[0];
  201. };
  202. $scope.nodeName = function (nodeCfg) {
  203. if (typeof nodeCfg === 'undefined') {
  204. return "";
  205. }
  206. if (nodeCfg.Name) {
  207. return nodeCfg.Name;
  208. }
  209. return nodeCfg.NodeID.substr(0, 6);
  210. };
  211. $scope.thisNodeName = function () {
  212. var node = $scope.thisNode();
  213. if (typeof node === 'undefined') {
  214. return "(unknown node)";
  215. }
  216. if (node.Name) {
  217. return node.Name;
  218. }
  219. return node.NodeID.substr(0, 6);
  220. };
  221. $scope.editSettings = function () {
  222. // Make a working copy
  223. $scope.config.workingOptions = angular.copy($scope.config.Options);
  224. $scope.config.workingGUI = angular.copy($scope.config.GUI);
  225. $('#settings').modal({backdrop: 'static', keyboard: true});
  226. }
  227. $scope.saveSettings = function () {
  228. // Make sure something changed
  229. var changed = ! angular.equals($scope.config.Options, $scope.config.workingOptions) ||
  230. ! angular.equals($scope.config.GUI, $scope.config.workingGUI);
  231. if(changed){
  232. $scope.config.Options = angular.copy($scope.config.workingOptions);
  233. $scope.config.GUI = angular.copy($scope.config.workingGUI);
  234. $scope.configInSync = false;
  235. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  236. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  237. }
  238. $('#settings').modal("hide");
  239. };
  240. $scope.restart = function () {
  241. restarting = true;
  242. $('#restarting').modal({backdrop: 'static', keyboard: false});
  243. $http.post(urlbase + '/restart');
  244. $scope.configInSync = true;
  245. };
  246. $scope.shutdown = function () {
  247. restarting = true;
  248. $http.post(urlbase + '/shutdown').success(function () {
  249. $('#shutdown').modal({backdrop: 'static', keyboard: false});
  250. });
  251. $scope.configInSync = true;
  252. };
  253. $scope.editNode = function (nodeCfg) {
  254. $scope.currentNode = $.extend({}, nodeCfg);
  255. $scope.editingExisting = true;
  256. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  257. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  258. $scope.nodeEditor.$setPristine();
  259. $('#editNode').modal({backdrop: 'static', keyboard: true});
  260. };
  261. $scope.idNode = function () {
  262. $('#idqr').modal('show');
  263. };
  264. $scope.addNode = function () {
  265. $scope.currentNode = {AddressesStr: 'dynamic'};
  266. $scope.editingExisting = false;
  267. $scope.editingSelf = false;
  268. $scope.nodeEditor.$setPristine();
  269. $('#editNode').modal({backdrop: 'static', keyboard: true});
  270. };
  271. $scope.deleteNode = function () {
  272. $('#editNode').modal('hide');
  273. if (!$scope.editingExisting) {
  274. return;
  275. }
  276. $scope.nodes = $scope.nodes.filter(function (n) {
  277. return n.NodeID !== $scope.currentNode.NodeID;
  278. });
  279. $scope.config.Nodes = $scope.nodes;
  280. for (var id in $scope.repos) {
  281. $scope.repos[id].Nodes = $scope.repos[id].Nodes.filter(function (n) {
  282. return n.NodeID !== $scope.currentNode.NodeID;
  283. });
  284. }
  285. $scope.configInSync = false;
  286. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  287. };
  288. $scope.saveNode = function () {
  289. var nodeCfg, done, i;
  290. $scope.configInSync = false;
  291. $('#editNode').modal('hide');
  292. nodeCfg = $scope.currentNode;
  293. nodeCfg.NodeID = nodeCfg.NodeID.replace(/ /g, '').replace(/-/g, '').trim();
  294. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  295. done = false;
  296. for (i = 0; i < $scope.nodes.length; i++) {
  297. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  298. $scope.nodes[i] = nodeCfg;
  299. done = true;
  300. break;
  301. }
  302. }
  303. if (!done) {
  304. $scope.nodes.push(nodeCfg);
  305. }
  306. $scope.nodes.sort(nodeCompare);
  307. $scope.config.Nodes = $scope.nodes;
  308. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  309. };
  310. $scope.otherNodes = function () {
  311. return $scope.nodes.filter(function (n){
  312. return n.NodeID !== $scope.myID;
  313. });
  314. };
  315. $scope.thisNode = function () {
  316. var i, n;
  317. for (i = 0; i < $scope.nodes.length; i++) {
  318. n = $scope.nodes[i];
  319. if (n.NodeID === $scope.myID) {
  320. return n;
  321. }
  322. }
  323. };
  324. $scope.allNodes = function () {
  325. var nodes = $scope.otherNodes();
  326. nodes.push($scope.thisNode());
  327. return nodes;
  328. };
  329. $scope.errorList = function () {
  330. return $scope.errors.filter(function (e) {
  331. return e.Time > $scope.seenError;
  332. });
  333. };
  334. $scope.clearErrors = function () {
  335. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  336. $http.post(urlbase + '/error/clear');
  337. };
  338. $scope.friendlyNodes = function (str) {
  339. for (var i = 0; i < $scope.nodes.length; i++) {
  340. var cfg = $scope.nodes[i];
  341. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  342. }
  343. return str;
  344. };
  345. $scope.repoList = function () {
  346. return repoList($scope.repos);
  347. }
  348. $scope.editRepo = function (nodeCfg) {
  349. $scope.currentRepo = $.extend({selectedNodes: {}}, nodeCfg);
  350. $scope.currentRepo.Nodes.forEach(function (n) {
  351. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  352. });
  353. $scope.editingExisting = true;
  354. $scope.repoEditor.$setPristine();
  355. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  356. };
  357. $scope.addRepo = function () {
  358. $scope.currentRepo = {selectedNodes: {}};
  359. $scope.editingExisting = false;
  360. $scope.repoEditor.$setPristine();
  361. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  362. };
  363. $scope.saveRepo = function () {
  364. var repoCfg, done, i;
  365. $scope.configInSync = false;
  366. $('#editRepo').modal('hide');
  367. repoCfg = $scope.currentRepo;
  368. repoCfg.Nodes = [];
  369. repoCfg.selectedNodes[$scope.myID] = true;
  370. for (var nodeID in repoCfg.selectedNodes) {
  371. if (repoCfg.selectedNodes[nodeID] === true) {
  372. repoCfg.Nodes.push({NodeID: nodeID});
  373. }
  374. }
  375. delete repoCfg.selectedNodes;
  376. $scope.repos[repoCfg.ID] = repoCfg;
  377. $scope.config.Repositories = repoList($scope.repos);
  378. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  379. };
  380. $scope.deleteRepo = function () {
  381. $('#editRepo').modal('hide');
  382. if (!$scope.editingExisting) {
  383. return;
  384. }
  385. delete $scope.repos[$scope.currentRepo.ID];
  386. $scope.config.Repositories = repoList($scope.repos);
  387. $scope.configInSync = false;
  388. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  389. };
  390. $scope.init = function() {
  391. $http.get(urlbase + '/version').success(function (data) {
  392. $scope.version = data;
  393. });
  394. $http.get(urlbase + '/system').success(function (data) {
  395. $scope.system = data;
  396. $scope.myID = data.myID;
  397. });
  398. $http.get(urlbase + '/config').success(function (data) {
  399. $scope.config = data;
  400. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  401. $scope.nodes = $scope.config.Nodes;
  402. $scope.nodes.sort(nodeCompare);
  403. $scope.repos = repoMap($scope.config.Repositories);
  404. $scope.refresh();
  405. });
  406. $http.get(urlbase + '/config/sync').success(function (data) {
  407. $scope.configInSync = data.configInSync;
  408. });
  409. };
  410. $scope.init();
  411. setInterval($scope.refresh, 10000);
  412. });
  413. function nodeCompare(a, b) {
  414. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  415. if (a.Name < b.Name)
  416. return -1;
  417. return a.Name > b.Name;
  418. }
  419. if (a.NodeID < b.NodeID) {
  420. return -1;
  421. }
  422. return a.NodeID > b.NodeID;
  423. }
  424. function repoCompare(a, b) {
  425. if (a.Directory < b.Directory) {
  426. return -1;
  427. }
  428. return a.Directory > b.Directory;
  429. }
  430. function repoMap(l) {
  431. var m = {};
  432. l.forEach(function (r) {
  433. m[r.ID] = r;
  434. });
  435. return m;
  436. }
  437. function repoList(m) {
  438. var l = [];
  439. for (var id in m) {
  440. l.push(m[id])
  441. }
  442. l.sort(repoCompare);
  443. return l;
  444. }
  445. function decimals(val, num) {
  446. var digits, decs;
  447. if (val === 0) {
  448. return 0;
  449. }
  450. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  451. decs = Math.max(0, num - digits);
  452. return decs;
  453. }
  454. syncthing.filter('natural', function () {
  455. return function (input, valid) {
  456. return input.toFixed(decimals(input, valid));
  457. };
  458. });
  459. syncthing.filter('binary', function () {
  460. return function (input) {
  461. if (input === undefined) {
  462. return '0 ';
  463. }
  464. if (input > 1024 * 1024 * 1024) {
  465. input /= 1024 * 1024 * 1024;
  466. return input.toFixed(decimals(input, 2)) + ' Gi';
  467. }
  468. if (input > 1024 * 1024) {
  469. input /= 1024 * 1024;
  470. return input.toFixed(decimals(input, 2)) + ' Mi';
  471. }
  472. if (input > 1024) {
  473. input /= 1024;
  474. return input.toFixed(decimals(input, 2)) + ' Ki';
  475. }
  476. return Math.round(input) + ' ';
  477. };
  478. });
  479. syncthing.filter('metric', function () {
  480. return function (input) {
  481. if (input === undefined) {
  482. return '0 ';
  483. }
  484. if (input > 1000 * 1000 * 1000) {
  485. input /= 1000 * 1000 * 1000;
  486. return input.toFixed(decimals(input, 2)) + ' G';
  487. }
  488. if (input > 1000 * 1000) {
  489. input /= 1000 * 1000;
  490. return input.toFixed(decimals(input, 2)) + ' M';
  491. }
  492. if (input > 1000) {
  493. input /= 1000;
  494. return input.toFixed(decimals(input, 2)) + ' k';
  495. }
  496. return Math.round(input) + ' ';
  497. };
  498. });
  499. syncthing.filter('short', function () {
  500. return function (input) {
  501. return input.substr(0, 6);
  502. };
  503. });
  504. syncthing.filter('alwaysNumber', function () {
  505. return function (input) {
  506. if (input === undefined) {
  507. return 0;
  508. }
  509. return input;
  510. };
  511. });
  512. syncthing.filter('chunkID', function () {
  513. return function (input) {
  514. if (input === undefined)
  515. return "";
  516. var parts = input.match(/.{1,6}/g);
  517. if (!parts)
  518. return "";
  519. return parts.join('-');
  520. }
  521. });
  522. syncthing.filter('shortPath', function () {
  523. return function (input) {
  524. if (input === undefined)
  525. return "";
  526. var parts = input.split(/[\/\\]/);
  527. if (!parts || parts.length <= 3) {
  528. return input;
  529. }
  530. return ".../" + parts.slice(parts.length-2).join("/");
  531. }
  532. });
  533. syncthing.directive('optionEditor', function () {
  534. return {
  535. restrict: 'C',
  536. replace: true,
  537. transclude: true,
  538. scope: {
  539. setting: '=setting',
  540. },
  541. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  542. };
  543. });
  544. syncthing.directive('uniqueRepo', function() {
  545. return {
  546. require: 'ngModel',
  547. link: function(scope, elm, attrs, ctrl) {
  548. ctrl.$parsers.unshift(function(viewValue) {
  549. if (scope.editingExisting) {
  550. // we shouldn't validate
  551. ctrl.$setValidity('uniqueRepo', true);
  552. } else if (scope.repos[viewValue]) {
  553. // the repo exists already
  554. ctrl.$setValidity('uniqueRepo', false);
  555. } else {
  556. // the repo is unique
  557. ctrl.$setValidity('uniqueRepo', true);
  558. }
  559. return viewValue;
  560. });
  561. }
  562. };
  563. });