app.js 17 KB

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