2014-01-05 22:54:57 +00:00
|
|
|
var syncthing = angular.module('syncthing', []);
|
|
|
|
|
|
|
|
syncthing.controller('SyncthingCtrl', function ($scope, $http) {
|
2014-01-09 09:31:27 +00:00
|
|
|
var prevDate = 0;
|
|
|
|
var modelGetOK = true;
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
$scope.connections = {};
|
|
|
|
$scope.config = {};
|
|
|
|
$scope.myID = "";
|
|
|
|
$scope.nodes = [];
|
|
|
|
|
|
|
|
// Strings before bools look better
|
|
|
|
$scope.settings = [
|
|
|
|
{id: 'ListenAddress', descr:"Sync Protocol Listen Address", type: 'string', restart: true},
|
|
|
|
{id: 'GUIAddress', descr: "GUI Listen Address", type: 'string', restart: true},
|
|
|
|
{id: 'MaxSendKbps', descr: "Outgoing Rate Limit (KBps)", type: 'string', restart: true},
|
|
|
|
{id: 'RescanIntervalS', descr: "Rescan Interval (s)", type: 'string', restart: true},
|
|
|
|
{id: 'ReconnectIntervalS', descr: "Reconnect Interval (s)", type: 'string', restart: true},
|
|
|
|
{id: 'ParallelRequests', descr: "Max Outstanding Requests", type: 'string', restart: true},
|
|
|
|
{id: 'MaxChangeKbps', descr: "Max File Change Rate (KBps)", type: 'string', restart: true},
|
|
|
|
|
|
|
|
{id: 'ReadOnly', descr: "Read Only", type: 'bool', restart: true},
|
|
|
|
{id: 'AllowDelete', descr: "Allow Delete", type: 'bool', restart: true},
|
|
|
|
{id: 'FollowSymlinks', descr: "Follow Symlinks", type: 'bool', restart: true},
|
|
|
|
{id: 'GlobalAnnEnabled', descr: "Global Announce", type: 'bool', restart: true},
|
|
|
|
{id: 'LocalAnnEnabled', descr: "Local Announce", type: 'bool', restart: true},
|
|
|
|
];
|
|
|
|
|
2014-01-09 09:31:27 +00:00
|
|
|
function modelGetSucceeded() {
|
|
|
|
if (!modelGetOK) {
|
|
|
|
$('#networkError').modal('hide');
|
|
|
|
modelGetOK = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function modelGetFailed() {
|
|
|
|
if (modelGetOK) {
|
|
|
|
$('#networkError').modal({backdrop: 'static', keyboard: false});
|
|
|
|
modelGetOK = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 22:54:57 +00:00
|
|
|
$http.get("/rest/version").success(function (data) {
|
|
|
|
$scope.version = data;
|
|
|
|
});
|
2014-02-01 19:23:19 +00:00
|
|
|
$http.get("/rest/system").success(function (data) {
|
|
|
|
$scope.system = data;
|
|
|
|
$scope.myID = data.myID;
|
|
|
|
|
|
|
|
$http.get("/rest/config").success(function (data) {
|
|
|
|
$scope.config = data;
|
|
|
|
|
|
|
|
var nodes = $scope.config.Repositories[0].Nodes;
|
2014-02-05 20:30:04 +00:00
|
|
|
nodes = nodes.filter(function (x) { return x.NodeID != $scope.myID; });
|
2014-02-01 19:23:19 +00:00
|
|
|
nodes.sort(function (a, b) {
|
|
|
|
if (a.NodeID < b.NodeID)
|
|
|
|
return -1;
|
|
|
|
return a.NodeID > b.NodeID;
|
|
|
|
})
|
|
|
|
$scope.nodes = nodes;
|
|
|
|
});
|
2014-01-05 22:54:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$scope.refresh = function () {
|
2014-01-09 23:09:27 +00:00
|
|
|
$http.get("/rest/system").success(function (data) {
|
|
|
|
$scope.system = data;
|
|
|
|
});
|
2014-01-05 22:54:57 +00:00
|
|
|
$http.get("/rest/model").success(function (data) {
|
|
|
|
$scope.model = data;
|
2014-01-09 09:31:27 +00:00
|
|
|
modelGetSucceeded();
|
|
|
|
}).error(function () {
|
|
|
|
modelGetFailed();
|
2014-01-05 22:54:57 +00:00
|
|
|
});
|
|
|
|
$http.get("/rest/connections").success(function (data) {
|
|
|
|
var now = Date.now();
|
|
|
|
var td = (now - prevDate) / 1000;
|
|
|
|
prevDate = now;
|
|
|
|
|
2014-02-05 20:30:04 +00:00
|
|
|
$scope.inbps = 0
|
|
|
|
$scope.outbps = 0
|
2014-01-05 22:54:57 +00:00
|
|
|
for (var id in data) {
|
|
|
|
try {
|
2014-01-09 09:31:27 +00:00
|
|
|
data[id].inbps = Math.max(0, 8 * (data[id].InBytesTotal - $scope.connections[id].InBytesTotal) / td);
|
|
|
|
data[id].outbps = Math.max(0, 8 * (data[id].OutBytesTotal - $scope.connections[id].OutBytesTotal) / td);
|
2014-01-05 22:54:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
data[id].inbps = 0;
|
|
|
|
data[id].outbps = 0;
|
|
|
|
}
|
2014-02-05 20:30:04 +00:00
|
|
|
$scope.inbps += data[id].outbps;
|
|
|
|
$scope.outbps += data[id].inbps;
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
$scope.connections = data;
|
|
|
|
});
|
|
|
|
$http.get("/rest/need").success(function (data) {
|
2014-01-06 05:10:53 +00:00
|
|
|
var i, name;
|
|
|
|
for (i = 0; i < data.length; i++) {
|
|
|
|
name = data[i].Name.split("/");
|
|
|
|
data[i].ShortName = name[name.length-1];
|
|
|
|
}
|
|
|
|
data.sort(function (a, b) {
|
|
|
|
if (a.ShortName < b.ShortName) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.ShortName > b.ShortName) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
2014-01-05 22:54:57 +00:00
|
|
|
$scope.need = data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-01 19:23:19 +00:00
|
|
|
$scope.nodeIcon = function (nodeCfg) {
|
|
|
|
if ($scope.connections[nodeCfg.NodeID]) {
|
|
|
|
return "ok";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "minus";
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.nodeClass = function (nodeCfg) {
|
|
|
|
var conn = $scope.connections[nodeCfg.NodeID];
|
|
|
|
if (conn) {
|
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "info";
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.nodeAddr = function (nodeCfg) {
|
|
|
|
var conn = $scope.connections[nodeCfg.NodeID];
|
|
|
|
if (conn) {
|
|
|
|
return conn.Address;
|
|
|
|
}
|
|
|
|
return nodeCfg.Addresses.join(", ");
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.nodeVer = function (nodeCfg) {
|
|
|
|
if (nodeCfg.NodeID === $scope.myID) {
|
|
|
|
return $scope.version;
|
|
|
|
}
|
|
|
|
var conn = $scope.connections[nodeCfg.NodeID];
|
|
|
|
if (conn) {
|
|
|
|
return conn.ClientVersion;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.saveSettings = function () {
|
|
|
|
$http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}});
|
|
|
|
$('#settingsTable').collapse('hide');
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.editNode = function (nodeCfg) {
|
|
|
|
$scope.currentNode = nodeCfg;
|
|
|
|
$scope.editingExisting = true;
|
|
|
|
$scope.currentNode.AddressesStr = nodeCfg.Addresses.join(", ")
|
|
|
|
$('#editNode').modal({backdrop: 'static', keyboard: false});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addNode = function () {
|
|
|
|
$scope.currentNode = {NodeID: "", AddressesStr: "dynamic"};
|
|
|
|
$scope.editingExisting = false;
|
|
|
|
$('#editNode').modal({backdrop: 'static', keyboard: false});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.deleteNode = function () {
|
|
|
|
$('#editNode').modal('hide');
|
|
|
|
if (!$scope.editingExisting)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var newNodes = [];
|
|
|
|
for (var i = 0; i < $scope.nodes.length; i++) {
|
|
|
|
if ($scope.nodes[i].NodeID !== $scope.currentNode.NodeID) {
|
|
|
|
newNodes.push($scope.nodes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.nodes = newNodes;
|
|
|
|
$scope.config.Repositories[0].Nodes = newNodes;
|
|
|
|
|
|
|
|
$http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}})
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.saveNode = function () {
|
|
|
|
$('#editNode').modal('hide');
|
|
|
|
nodeCfg = $scope.currentNode;
|
|
|
|
nodeCfg.Addresses = nodeCfg.AddressesStr.split(',').map(function (x) { return x.trim(); });
|
|
|
|
|
|
|
|
var done = false;
|
|
|
|
for (var i = 0; i < $scope.nodes.length; i++) {
|
|
|
|
if ($scope.nodes[i].NodeID === nodeCfg.NodeID) {
|
|
|
|
$scope.nodes[i] = nodeCfg;
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
$scope.nodes.push(nodeCfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.nodes.sort(function (a, b) {
|
|
|
|
if (a.NodeID < b.NodeID)
|
|
|
|
return -1;
|
|
|
|
return a.NodeID > b.NodeID;
|
|
|
|
})
|
|
|
|
|
|
|
|
$scope.config.Repositories[0].Nodes = $scope.nodes;
|
|
|
|
|
|
|
|
$http.post('/rest/config', JSON.stringify($scope.config), {headers: {'Content-Type': 'application/json'}})
|
|
|
|
};
|
|
|
|
|
2014-01-05 22:54:57 +00:00
|
|
|
$scope.refresh();
|
|
|
|
setInterval($scope.refresh, 10000);
|
|
|
|
});
|
|
|
|
|
2014-01-09 23:09:27 +00:00
|
|
|
function decimals(val, num) {
|
|
|
|
if (val === 0) { return 0; }
|
|
|
|
var digits = Math.floor(Math.log(Math.abs(val))/Math.log(10));
|
|
|
|
var decimals = Math.max(0, num - digits);
|
|
|
|
return decimals;
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 23:09:27 +00:00
|
|
|
syncthing.filter('natural', function() {
|
|
|
|
return function(input, valid) {
|
|
|
|
return input.toFixed(decimals(input, valid));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-05 22:54:57 +00:00
|
|
|
syncthing.filter('binary', function() {
|
|
|
|
return function(input) {
|
|
|
|
if (input === undefined) {
|
2014-02-01 19:23:19 +00:00
|
|
|
return '0 '
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
if (input > 1024 * 1024 * 1024) {
|
|
|
|
input /= 1024 * 1024 * 1024;
|
2014-01-09 23:09:27 +00:00
|
|
|
return input.toFixed(decimals(input, 2)) + ' Gi';
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
if (input > 1024 * 1024) {
|
|
|
|
input /= 1024 * 1024;
|
2014-01-09 23:09:27 +00:00
|
|
|
return input.toFixed(decimals(input, 2)) + ' Mi';
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
if (input > 1024) {
|
|
|
|
input /= 1024;
|
2014-01-09 23:09:27 +00:00
|
|
|
return input.toFixed(decimals(input, 2)) + ' Ki';
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
return Math.round(input) + ' ';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
syncthing.filter('metric', function() {
|
|
|
|
return function(input) {
|
|
|
|
if (input === undefined) {
|
2014-02-01 19:23:19 +00:00
|
|
|
return '0 '
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
if (input > 1000 * 1000 * 1000) {
|
|
|
|
input /= 1000 * 1000 * 1000;
|
2014-01-09 23:09:27 +00:00
|
|
|
return input.toFixed(decimals(input, 2)) + ' G';
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
if (input > 1000 * 1000) {
|
|
|
|
input /= 1000 * 1000;
|
2014-01-09 23:09:27 +00:00
|
|
|
return input.toFixed(decimals(input, 2)) + ' M';
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
if (input > 1000) {
|
|
|
|
input /= 1000;
|
2014-01-09 23:09:27 +00:00
|
|
|
return input.toFixed(decimals(input, 2)) + ' k';
|
2014-01-05 22:54:57 +00:00
|
|
|
}
|
|
|
|
return Math.round(input) + ' ';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
syncthing.filter('short', function() {
|
|
|
|
return function(input) {
|
|
|
|
return input.substr(0, 6);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
syncthing.filter('alwaysNumber', function() {
|
|
|
|
return function(input) {
|
|
|
|
if (input === undefined) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
});
|
2014-02-01 19:23:19 +00:00
|
|
|
|
|
|
|
syncthing.directive('optionEditor', function() {
|
|
|
|
return {
|
|
|
|
restrict: 'C',
|
|
|
|
replace: true,
|
|
|
|
transclude: true,
|
|
|
|
scope: {
|
|
|
|
setting: '=setting',
|
|
|
|
},
|
|
|
|
template: '<input type="text" ng-model="config.Options[setting.id]"></input>',
|
|
|
|
};
|
|
|
|
})
|