app.js 15 KB

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