app.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 (nodeCfg.Name) {
  204. return nodeCfg.Name;
  205. }
  206. return nodeCfg.NodeID.substr(0, 6);
  207. };
  208. $scope.thisNodeName = function () {
  209. var node = $scope.thisNode();
  210. if (typeof node === 'undefined') {
  211. return "(unknown node)";
  212. }
  213. if (node.Name) {
  214. return node.Name;
  215. }
  216. return node.NodeID.substr(0, 6);
  217. };
  218. $scope.editSettings = function () {
  219. $('#settings').modal({backdrop: 'static', keyboard: true});
  220. }
  221. $scope.saveSettings = function () {
  222. $scope.configInSync = false;
  223. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  224. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  225. $('#settings').modal("hide");
  226. };
  227. $scope.restart = function () {
  228. restarting = true;
  229. $('#restarting').modal({backdrop: 'static', keyboard: false});
  230. $http.post(urlbase + '/restart');
  231. $scope.configInSync = true;
  232. };
  233. $scope.shutdown = function () {
  234. restarting = true;
  235. $http.post(urlbase + '/shutdown').success(function () {
  236. $('#shutdown').modal({backdrop: 'static', keyboard: false});
  237. });
  238. $scope.configInSync = true;
  239. };
  240. $scope.editNode = function (nodeCfg) {
  241. $scope.currentNode = $.extend({}, nodeCfg);
  242. $scope.editingExisting = true;
  243. $scope.editingSelf = (nodeCfg.NodeID == $scope.myID);
  244. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  245. $scope.nodeEditor.$setPristine();
  246. $('#editNode').modal({backdrop: 'static', keyboard: true});
  247. };
  248. $scope.idNode = function () {
  249. $('#idqr').modal('show');
  250. };
  251. $scope.addNode = function () {
  252. $scope.currentNode = {AddressesStr: 'dynamic'};
  253. $scope.editingExisting = false;
  254. $scope.editingSelf = false;
  255. $scope.nodeEditor.$setPristine();
  256. $('#editNode').modal({backdrop: 'static', keyboard: true});
  257. };
  258. $scope.deleteNode = function () {
  259. $('#editNode').modal('hide');
  260. if (!$scope.editingExisting) {
  261. return;
  262. }
  263. $scope.nodes = $scope.nodes.filter(function (n) {
  264. return n.NodeID !== $scope.currentNode.NodeID;
  265. });
  266. $scope.config.Nodes = $scope.nodes;
  267. for (var id in repos) {
  268. $scope.repos[id].Nodes = $scope.repos[id].Nodes.filter(function (n) {
  269. return n.NodeID !== $scope.currentNode.NodeID;
  270. });
  271. }
  272. $scope.configInSync = false;
  273. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  274. };
  275. $scope.saveNode = function () {
  276. var nodeCfg, done, i;
  277. $scope.configInSync = false;
  278. $('#editNode').modal('hide');
  279. nodeCfg = $scope.currentNode;
  280. nodeCfg.NodeID = nodeCfg.NodeID.replace(/ /g, '').replace(/-/g, '').trim();
  281. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  282. done = false;
  283. for (i = 0; i < $scope.nodes.length; i++) {
  284. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  285. $scope.nodes[i] = nodeCfg;
  286. done = true;
  287. break;
  288. }
  289. }
  290. if (!done) {
  291. $scope.nodes.push(nodeCfg);
  292. }
  293. $scope.nodes.sort(nodeCompare);
  294. $scope.config.Nodes = $scope.nodes;
  295. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  296. };
  297. $scope.otherNodes = function () {
  298. return $scope.nodes.filter(function (n){
  299. return n.NodeID !== $scope.myID;
  300. });
  301. };
  302. $scope.thisNode = function () {
  303. var i, n;
  304. for (i = 0; i < $scope.nodes.length; i++) {
  305. n = $scope.nodes[i];
  306. if (n.NodeID === $scope.myID) {
  307. return n;
  308. }
  309. }
  310. };
  311. $scope.allNodes = function () {
  312. var nodes = $scope.otherNodes();
  313. nodes.push($scope.thisNode());
  314. return nodes;
  315. };
  316. $scope.errorList = function () {
  317. return $scope.errors.filter(function (e) {
  318. return e.Time > $scope.seenError;
  319. });
  320. };
  321. $scope.clearErrors = function () {
  322. $scope.seenError = $scope.errors[$scope.errors.length - 1].Time;
  323. $http.post(urlbase + '/error/clear');
  324. };
  325. $scope.friendlyNodes = function (str) {
  326. for (var i = 0; i < $scope.nodes.length; i++) {
  327. var cfg = $scope.nodes[i];
  328. str = str.replace(cfg.NodeID, $scope.nodeName(cfg));
  329. }
  330. return str;
  331. };
  332. $scope.repoList = function () {
  333. return repoList($scope.repos);
  334. }
  335. $scope.editRepo = function (nodeCfg) {
  336. $scope.currentRepo = $.extend({selectedNodes: {}}, nodeCfg);
  337. $scope.currentRepo.Nodes.forEach(function (n) {
  338. $scope.currentRepo.selectedNodes[n.NodeID] = true;
  339. });
  340. $scope.editingExisting = true;
  341. $scope.repoEditor.$setPristine();
  342. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  343. };
  344. $scope.addRepo = function () {
  345. $scope.currentRepo = {selectedNodes: {}};
  346. $scope.editingExisting = false;
  347. $scope.repoEditor.$setPristine();
  348. $('#editRepo').modal({backdrop: 'static', keyboard: true});
  349. };
  350. $scope.saveRepo = function () {
  351. var repoCfg, done, i;
  352. $scope.configInSync = false;
  353. $('#editRepo').modal('hide');
  354. repoCfg = $scope.currentRepo;
  355. repoCfg.Nodes = [];
  356. repoCfg.selectedNodes[$scope.myID] = true;
  357. for (var nodeID in repoCfg.selectedNodes) {
  358. if (repoCfg.selectedNodes[nodeID] === true) {
  359. repoCfg.Nodes.push({NodeID: nodeID});
  360. }
  361. }
  362. delete repoCfg.selectedNodes;
  363. $scope.repos[repoCfg.ID] = repoCfg;
  364. $scope.config.Repositories = repoList($scope.repos);
  365. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  366. };
  367. $scope.deleteRepo = function () {
  368. $('#editRepo').modal('hide');
  369. if (!$scope.editingExisting) {
  370. return;
  371. }
  372. delete $scope.repos[$scope.currentRepo.ID];
  373. $scope.config.Repositories = repoList($scope.repos);
  374. $scope.configInSync = false;
  375. $http.post(urlbase + '/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  376. };
  377. $scope.init = function() {
  378. $http.get(urlbase + '/version').success(function (data) {
  379. $scope.version = data;
  380. });
  381. $http.get(urlbase + '/system').success(function (data) {
  382. $scope.system = data;
  383. $scope.myID = data.myID;
  384. });
  385. $http.get(urlbase + '/config').success(function (data) {
  386. $scope.config = data;
  387. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  388. $scope.nodes = $scope.config.Nodes;
  389. $scope.nodes.sort(nodeCompare);
  390. $scope.repos = repoMap($scope.config.Repositories);
  391. $scope.refresh();
  392. });
  393. $http.get(urlbase + '/config/sync').success(function (data) {
  394. $scope.configInSync = data.configInSync;
  395. });
  396. };
  397. $scope.init();
  398. setInterval($scope.refresh, 10000);
  399. });
  400. function nodeCompare(a, b) {
  401. if (typeof a.Name !== 'undefined' && typeof b.Name !== 'undefined') {
  402. if (a.Name < b.Name)
  403. return -1;
  404. return a.Name > b.Name;
  405. }
  406. if (a.NodeID < b.NodeID) {
  407. return -1;
  408. }
  409. return a.NodeID > b.NodeID;
  410. }
  411. function repoCompare(a, b) {
  412. if (a.Directory < b.Directory) {
  413. return -1;
  414. }
  415. return a.Directory > b.Directory;
  416. }
  417. function repoMap(l) {
  418. var m = {};
  419. l.forEach(function (r) {
  420. m[r.ID] = r;
  421. });
  422. return m;
  423. }
  424. function repoList(m) {
  425. var l = [];
  426. for (var id in m) {
  427. l.push(m[id])
  428. }
  429. l.sort(repoCompare);
  430. return l;
  431. }
  432. function decimals(val, num) {
  433. var digits, decs;
  434. if (val === 0) {
  435. return 0;
  436. }
  437. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  438. decs = Math.max(0, num - digits);
  439. return decs;
  440. }
  441. syncthing.filter('natural', function () {
  442. return function (input, valid) {
  443. return input.toFixed(decimals(input, valid));
  444. };
  445. });
  446. syncthing.filter('binary', function () {
  447. return function (input) {
  448. if (input === undefined) {
  449. return '0 ';
  450. }
  451. if (input > 1024 * 1024 * 1024) {
  452. input /= 1024 * 1024 * 1024;
  453. return input.toFixed(decimals(input, 2)) + ' Gi';
  454. }
  455. if (input > 1024 * 1024) {
  456. input /= 1024 * 1024;
  457. return input.toFixed(decimals(input, 2)) + ' Mi';
  458. }
  459. if (input > 1024) {
  460. input /= 1024;
  461. return input.toFixed(decimals(input, 2)) + ' Ki';
  462. }
  463. return Math.round(input) + ' ';
  464. };
  465. });
  466. syncthing.filter('metric', function () {
  467. return function (input) {
  468. if (input === undefined) {
  469. return '0 ';
  470. }
  471. if (input > 1000 * 1000 * 1000) {
  472. input /= 1000 * 1000 * 1000;
  473. return input.toFixed(decimals(input, 2)) + ' G';
  474. }
  475. if (input > 1000 * 1000) {
  476. input /= 1000 * 1000;
  477. return input.toFixed(decimals(input, 2)) + ' M';
  478. }
  479. if (input > 1000) {
  480. input /= 1000;
  481. return input.toFixed(decimals(input, 2)) + ' k';
  482. }
  483. return Math.round(input) + ' ';
  484. };
  485. });
  486. syncthing.filter('short', function () {
  487. return function (input) {
  488. return input.substr(0, 6);
  489. };
  490. });
  491. syncthing.filter('alwaysNumber', function () {
  492. return function (input) {
  493. if (input === undefined) {
  494. return 0;
  495. }
  496. return input;
  497. };
  498. });
  499. syncthing.filter('chunkID', function () {
  500. return function (input) {
  501. if (input === undefined)
  502. return "";
  503. var parts = input.match(/.{1,6}/g);
  504. if (!parts)
  505. return "";
  506. return parts.join('-');
  507. }
  508. });
  509. syncthing.filter('shortPath', function () {
  510. return function (input) {
  511. if (input === undefined)
  512. return "";
  513. var parts = input.split(/[\/\\]/);
  514. if (!parts || parts.length <= 3) {
  515. return input;
  516. }
  517. return ".../" + parts.slice(parts.length-2).join("/");
  518. }
  519. });
  520. syncthing.directive('optionEditor', function () {
  521. return {
  522. restrict: 'C',
  523. replace: true,
  524. transclude: true,
  525. scope: {
  526. setting: '=setting',
  527. },
  528. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  529. };
  530. });
  531. syncthing.directive('uniqueRepo', function() {
  532. return {
  533. require: 'ngModel',
  534. link: function(scope, elm, attrs, ctrl) {
  535. ctrl.$parsers.unshift(function(viewValue) {
  536. if (scope.editingExisting) {
  537. // we shouldn't validate
  538. ctrl.$setValidity('uniqueRepo', true);
  539. } else if (scope.repos[viewValue]) {
  540. // the repo exists already
  541. ctrl.$setValidity('uniqueRepo', false);
  542. } else {
  543. // the repo is unique
  544. ctrl.$setValidity('uniqueRepo', true);
  545. }
  546. return viewValue;
  547. });
  548. }
  549. };
  550. });