app.js 15 KB

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