app.js 19 KB

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