app.js 16 KB

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