app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. modelGetOK = true;
  8. $scope.connections = {};
  9. $scope.config = {};
  10. $scope.myID = '';
  11. $scope.nodes = [];
  12. $scope.configInSync = true;
  13. $scope.errors = [];
  14. $scope.seenError = '';
  15. // Strings before bools look better
  16. $scope.settings = [
  17. {id: 'ListenStr', descr: 'Sync Protocol Listen Addresses', type: 'text', restart: true},
  18. {id: 'GUIAddress', descr: 'GUI Listen Address', type: 'text', restart: true},
  19. {id: 'MaxSendKbps', descr: 'Outgoing Rate Limit (KBps)', type: 'number', restart: true},
  20. {id: 'RescanIntervalS', descr: 'Rescan Interval (s)', type: 'number', restart: true},
  21. {id: 'ReconnectIntervalS', descr: 'Reconnect Interval (s)', type: 'number', restart: true},
  22. {id: 'ParallelRequests', descr: 'Max Outstanding Requests', type: 'number', restart: true},
  23. {id: 'MaxChangeKbps', descr: 'Max File Change Rate (KBps)', type: 'number', restart: true},
  24. {id: 'ReadOnly', descr: 'Read Only', type: 'bool', restart: true},
  25. {id: 'AllowDelete', descr: 'Allow Delete', type: 'bool', restart: true},
  26. {id: 'FollowSymlinks', descr: 'Follow Symlinks', type: 'bool', restart: true},
  27. {id: 'GlobalAnnEnabled', descr: 'Global Announce', type: 'bool', restart: true},
  28. {id: 'LocalAnnEnabled', descr: 'Local Announce', type: 'bool', restart: true},
  29. ];
  30. function modelGetSucceeded() {
  31. if (!modelGetOK) {
  32. $('#networkError').modal('hide');
  33. modelGetOK = true;
  34. }
  35. }
  36. function modelGetFailed() {
  37. if (modelGetOK) {
  38. $('#networkError').modal({backdrop: 'static', keyboard: false});
  39. modelGetOK = false;
  40. }
  41. }
  42. function nodeCompare(a, b) {
  43. if (a.NodeID === $scope.myID) {
  44. return -1;
  45. }
  46. if (b.NodeID === $scope.myID) {
  47. return 1;
  48. }
  49. if (a.NodeID < b.NodeID) {
  50. return -1;
  51. }
  52. return a.NodeID > b.NodeID;
  53. }
  54. $http.get('/rest/version').success(function (data) {
  55. $scope.version = data;
  56. });
  57. $http.get('/rest/system').success(function (data) {
  58. $scope.system = data;
  59. $scope.myID = data.myID;
  60. $http.get('/rest/config').success(function (data) {
  61. $scope.config = data;
  62. $scope.config.Options.ListenStr = $scope.config.Options.ListenAddress.join(', ');
  63. var nodes = $scope.config.Repositories[0].Nodes;
  64. nodes.sort(nodeCompare);
  65. $scope.nodes = nodes;
  66. });
  67. $http.get('/rest/config/sync').success(function (data) {
  68. $scope.configInSync = data.configInSync;
  69. });
  70. });
  71. $scope.refresh = function () {
  72. $http.get('/rest/system').success(function (data) {
  73. $scope.system = data;
  74. });
  75. $http.get('/rest/model').success(function (data) {
  76. $scope.model = data;
  77. modelGetSucceeded();
  78. }).error(function () {
  79. modelGetFailed();
  80. });
  81. $http.get('/rest/connections').success(function (data) {
  82. var now = Date.now(),
  83. td = (now - prevDate) / 1000,
  84. id;
  85. prevDate = now;
  86. $scope.inbps = 0;
  87. $scope.outbps = 0;
  88. for (id in data) {
  89. if (!data.hasOwnProperty(id)) {
  90. continue;
  91. }
  92. try {
  93. data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
  94. data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
  95. } catch (e) {
  96. data[id].inbps = 0;
  97. data[id].outbps = 0;
  98. }
  99. $scope.inbps += data[id].inbps;
  100. $scope.outbps += data[id].outbps;
  101. }
  102. $scope.connections = data;
  103. });
  104. $http.get('/rest/need').success(function (data) {
  105. var i, name;
  106. for (i = 0; i < data.length; i++) {
  107. name = data[i].Name.split('/');
  108. data[i].ShortName = name[name.length - 1];
  109. }
  110. data.sort(function (a, b) {
  111. if (a.ShortName < b.ShortName) {
  112. return -1;
  113. }
  114. if (a.ShortName > b.ShortName) {
  115. return 1;
  116. }
  117. return 0;
  118. });
  119. $scope.need = data;
  120. });
  121. $http.get('/rest/errors').success(function (data) {
  122. $scope.errors = data;
  123. });
  124. };
  125. $scope.nodeStatus = function (nodeCfg) {
  126. var conn = $scope.connections[nodeCfg.NodeID];
  127. if (conn) {
  128. if (conn.Completion === 100) {
  129. return 'In Sync';
  130. } else {
  131. return 'Syncing (' + conn.Completion + '%)';
  132. }
  133. }
  134. return 'Disconnected';
  135. };
  136. $scope.nodeIcon = function (nodeCfg) {
  137. var conn = $scope.connections[nodeCfg.NodeID];
  138. if (conn) {
  139. if (conn.Completion === 100) {
  140. return 'ok';
  141. } else {
  142. return 'refresh';
  143. }
  144. }
  145. return 'minus';
  146. };
  147. $scope.nodeClass = function (nodeCfg) {
  148. var conn = $scope.connections[nodeCfg.NodeID];
  149. if (conn) {
  150. if (conn.Completion === 100) {
  151. return 'success';
  152. } else {
  153. return 'primary';
  154. }
  155. }
  156. return 'info';
  157. };
  158. $scope.nodeAddr = function (nodeCfg) {
  159. var conn = $scope.connections[nodeCfg.NodeID];
  160. if (conn) {
  161. return conn.Address;
  162. }
  163. return '(unknown address)';
  164. };
  165. $scope.nodeCompletion = function (nodeCfg) {
  166. var conn = $scope.connections[nodeCfg.NodeID];
  167. if (conn) {
  168. return conn.Completion + '%';
  169. }
  170. return '';
  171. };
  172. $scope.nodeVer = function (nodeCfg) {
  173. if (nodeCfg.NodeID === $scope.myID) {
  174. return $scope.version;
  175. }
  176. var conn = $scope.connections[nodeCfg.NodeID];
  177. if (conn) {
  178. return conn.ClientVersion;
  179. }
  180. return '(unknown version)';
  181. };
  182. $scope.nodeName = function (nodeCfg) {
  183. if (nodeCfg.Name) {
  184. return nodeCfg.Name;
  185. }
  186. return nodeCfg.NodeID.substr(0, 6);
  187. };
  188. $scope.saveSettings = function () {
  189. $scope.configInSync = false;
  190. $scope.config.Options.ListenAddress = $scope.config.Options.ListenStr.split(',').map(function (x) { return x.trim(); });
  191. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  192. $('#settingsTable').collapse('hide');
  193. };
  194. $scope.restart = function () {
  195. $http.post('/rest/restart');
  196. $scope.configInSync = true;
  197. };
  198. $scope.editNode = function (nodeCfg) {
  199. $scope.currentNode = nodeCfg;
  200. $scope.editingExisting = true;
  201. $scope.currentNode.AddressesStr = nodeCfg.Addresses.join(', ');
  202. $('#editNode').modal({backdrop: 'static', keyboard: false});
  203. };
  204. $scope.addNode = function () {
  205. $scope.currentNode = {NodeID: '', AddressesStr: 'dynamic'};
  206. $scope.editingExisting = false;
  207. $('#editNode').modal({backdrop: 'static', keyboard: false});
  208. };
  209. $scope.deleteNode = function () {
  210. var newNodes = [], i;
  211. $('#editNode').modal('hide');
  212. if (!$scope.editingExisting) {
  213. return;
  214. }
  215. for (i = 0; i < $scope.nodes.length; i++) {
  216. if ($scope.nodes[i].NodeID !== $scope.currentNode.NodeID) {
  217. newNodes.push($scope.nodes[i]);
  218. }
  219. }
  220. $scope.nodes = newNodes;
  221. $scope.config.Repositories[0].Nodes = newNodes;
  222. $scope.configInSync = false;
  223. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  224. };
  225. $scope.saveNode = function () {
  226. var nodeCfg, done, i;
  227. $scope.configInSync = false;
  228. $('#editNode').modal('hide');
  229. nodeCfg = $scope.currentNode;
  230. nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
  231. done = false;
  232. for (i = 0; i < $scope.nodes.length; i++) {
  233. if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
  234. $scope.nodes[i] = nodeCfg;
  235. done = true;
  236. break;
  237. }
  238. }
  239. if (!done) {
  240. $scope.nodes.push(nodeCfg);
  241. }
  242. $scope.nodes.sort(nodeCompare);
  243. $scope.config.Repositories[0].Nodes = $scope.nodes;
  244. $http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
  245. };
  246. $scope.otherNodes = function () {
  247. var nodes = [], i, n;
  248. for (i = 0; i < $scope.nodes.length; i++) {
  249. n = $scope.nodes[i];
  250. if (n.NodeID !== $scope.myID) {
  251. nodes.push(n);
  252. }
  253. }
  254. return nodes;
  255. };
  256. $scope.thisNode = function () {
  257. var i, n;
  258. for (i = 0; i < $scope.nodes.length; i++) {
  259. n = $scope.nodes[i];
  260. if (n.NodeID === $scope.myID) {
  261. return [n];
  262. }
  263. }
  264. };
  265. $scope.errorList = function () {
  266. var errors = [];
  267. for (var i = 0; i < $scope.errors.length; i++) {
  268. var e = $scope.errors[i];
  269. if (e.Time > $scope.seenError) {
  270. errors.push(e);
  271. }
  272. }
  273. return errors;
  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.refresh();
  286. setInterval($scope.refresh, 10000);
  287. });
  288. function decimals(val, num) {
  289. var digits, decs;
  290. if (val === 0) {
  291. return 0;
  292. }
  293. digits = Math.floor(Math.log(Math.abs(val)) / Math.log(10));
  294. decs = Math.max(0, num - digits);
  295. return decs;
  296. }
  297. syncthing.filter('natural', function () {
  298. return function (input, valid) {
  299. return input.toFixed(decimals(input, valid));
  300. };
  301. });
  302. syncthing.filter('binary', function () {
  303. return function (input) {
  304. if (input === undefined) {
  305. return '0 ';
  306. }
  307. if (input > 1024 * 1024 * 1024) {
  308. input /= 1024 * 1024 * 1024;
  309. return input.toFixed(decimals(input, 2)) + ' Gi';
  310. }
  311. if (input > 1024 * 1024) {
  312. input /= 1024 * 1024;
  313. return input.toFixed(decimals(input, 2)) + ' Mi';
  314. }
  315. if (input > 1024) {
  316. input /= 1024;
  317. return input.toFixed(decimals(input, 2)) + ' Ki';
  318. }
  319. return Math.round(input) + ' ';
  320. };
  321. });
  322. syncthing.filter('metric', function () {
  323. return function (input) {
  324. if (input === undefined) {
  325. return '0 ';
  326. }
  327. if (input > 1000 * 1000 * 1000) {
  328. input /= 1000 * 1000 * 1000;
  329. return input.toFixed(decimals(input, 2)) + ' G';
  330. }
  331. if (input > 1000 * 1000) {
  332. input /= 1000 * 1000;
  333. return input.toFixed(decimals(input, 2)) + ' M';
  334. }
  335. if (input > 1000) {
  336. input /= 1000;
  337. return input.toFixed(decimals(input, 2)) + ' k';
  338. }
  339. return Math.round(input) + ' ';
  340. };
  341. });
  342. syncthing.filter('short', function () {
  343. return function (input) {
  344. return input.substr(0, 6);
  345. };
  346. });
  347. syncthing.filter('alwaysNumber', function () {
  348. return function (input) {
  349. if (input === undefined) {
  350. return 0;
  351. }
  352. return input;
  353. };
  354. });
  355. syncthing.directive('optionEditor', function () {
  356. return {
  357. restrict: 'C',
  358. replace: true,
  359. transclude: true,
  360. scope: {
  361. setting: '=setting',
  362. },
  363. template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
  364. };
  365. });