fix incorrect completed percent in connection status tab

This commit is contained in:
MaysWind 2016-06-12 22:50:49 +08:00
parent 322b3c2fa0
commit a9281f707b
3 changed files with 80 additions and 70 deletions

View file

@ -21,20 +21,19 @@
}; };
var refreshBtPeers = function (task, silent) { var refreshBtPeers = function (task, silent) {
return aria2TaskService.getBtTaskPeers(task.gid, function (response) { return aria2TaskService.getBtTaskPeers(task, function (response) {
if (!response.success) { if (!response.success) {
return; return;
} }
var peers = response.data; var peers = response.data;
peers.push(aria2TaskService.createLocalPeerFromTask(task));
if (!ariaNgCommonService.extendArray(peers, $scope.peers, 'peerId')) { if (!ariaNgCommonService.extendArray(peers, $scope.peers, 'peerId')) {
$scope.peers = peers; $scope.peers = peers;
} }
$scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.peers); $scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.peers);
}, silent); }, silent, true);
}; };
var refreshDownloadTask = function (silent) { var refreshDownloadTask = function (silent) {

View file

@ -81,20 +81,64 @@
return task; return task;
}; };
var estimateCompletedPercentFromBitField = function (bitfield) { var getPieceStatus = function (bitField, pieceCount) {
var totalLength = bitfield.length * 0xf; var pieces = [];
var completedLength = 0;
if (totalLength == 0) { if (!bitField) {
return 0; return pieces;
} }
for (var i = 0; i < bitfield.length; i++) { var pieceIndex = 0;
var num = parseInt(bitfield[i], 16);
completedLength += num; for (var i = 0; i < bitField.length; i++) {
var bitSet = parseInt(bitField[i], 16);
for (var j = 1; j <= 4; j++) {
var bit = (1 << (4 - j));
var isCompleted = (bitSet & bit) == bit;
pieces.push(isCompleted);
pieceIndex++;
if (pieceIndex >= pieceCount) {
return pieces;
}
}
} }
return completedLength / totalLength; return pieces;
};
var getCombinedPieces = function (bitField, pieceCount) {
var pieces = getPieceStatus(bitField, pieceCount);
var combinedPieces = [];
for (var i = 0; i < pieces.length; i++) {
var isCompleted = pieces[i];
if (combinedPieces.length > 0 && combinedPieces[combinedPieces.length - 1].isCompleted == isCompleted) {
combinedPieces[combinedPieces.length - 1].count++;
} else {
combinedPieces.push({
isCompleted: isCompleted,
count: 1
});
}
}
return combinedPieces;
};
var createLocalPeerFromTask = function (task) {
return {
local: true,
bitfield: task.bitfield,
completePercent: task.completePercent,
downloadSpeed: task.downloadSpeed,
name: '(local)',
seeder: task.seeder,
uploadSpeed: task.uploadSpeed
};
}; };
return { return {
@ -171,9 +215,9 @@
return this.setTaskOption(gid, 'select-file', selectedFileIndex, callback, silent); return this.setTaskOption(gid, 'select-file', selectedFileIndex, callback, silent);
}, },
getBtTaskPeers: function (gid, callback, silent) { getBtTaskPeers: function (task, callback, silent, includeLocal) {
return aria2RpcService.getPeers({ return aria2RpcService.getPeers({
gid: gid, gid: task.gid,
silent: !!silent, silent: !!silent,
callback: function (response) { callback: function (response) {
if (!callback) { if (!callback) {
@ -187,12 +231,17 @@
var peer = peers[i]; var peer = peers[i];
var upstreamToSpeed = peer.uploadSpeed; var upstreamToSpeed = peer.uploadSpeed;
var downstreamFromSpeed = peer.downloadSpeed; var downstreamFromSpeed = peer.downloadSpeed;
var completedPieces = getPieceStatus(peer.bitfield, task.numPieces);
peer.name = peer.ip + ':' + peer.port; peer.name = peer.ip + ':' + peer.port;
peer.completePercent = estimateCompletedPercentFromBitField(peer.bitfield) * 100; peer.completePercent = ariaNgCommonService.countArray(completedPieces, true) / task.numPieces * 100;
peer.downloadSpeed = upstreamToSpeed; peer.downloadSpeed = upstreamToSpeed;
peer.uploadSpeed = downstreamFromSpeed; peer.uploadSpeed = downstreamFromSpeed;
} }
if (includeLocal) {
peers.push(createLocalPeerFromTask(task));
}
} }
callback(response); callback(response);
@ -289,62 +338,11 @@
processDownloadTask(tasks[i]); processDownloadTask(tasks[i]);
} }
}, },
createLocalPeerFromTask: function (task) {
return {
local: true,
bitfield: task.bitfield,
completePercent: task.completePercent,
downloadSpeed: task.downloadSpeed,
name: '(local)',
seeder: task.seeder,
uploadSpeed: task.uploadSpeed
};
},
getPieceStatus: function (bitField, pieceCount) { getPieceStatus: function (bitField, pieceCount) {
var pieces = []; return getPieceStatus(bitField, pieceCount);
if (!bitField) {
return pieces;
}
var pieceIndex = 0;
for (var i = 0; i < bitField.length; i++) {
var bitSet = parseInt(bitField[i], 16);
for (var j = 1; j <= 4; j++) {
var bit = (1 << (4 - j));
var isCompleted = (bitSet & bit) == bit;
pieces.push(isCompleted);
pieceIndex++;
if (pieceIndex >= pieceCount) {
return pieces;
}
}
}
return pieces;
}, },
getCombinedPieces: function (bitField, pieceCount) { getCombinedPieces: function (bitField, pieceCount) {
var pieces = this.getPieceStatus(bitField, pieceCount); return getCombinedPieces(bitField, pieceCount);
var combinedPieces = [];
for (var i = 0; i < pieces.length; i++) {
var isCompleted = pieces[i];
if (combinedPieces.length > 0 && combinedPieces[combinedPieces.length - 1].isCompleted == isCompleted) {
combinedPieces[combinedPieces.length - 1].count++;
} else {
combinedPieces.push({
isCompleted: isCompleted,
count: 1
});
}
}
return combinedPieces;
}, },
estimateHealthPercentFromPeers: function (task, peers) { estimateHealthPercentFromPeers: function (task, peers) {
if (peers.length < 1) { if (peers.length < 1) {
@ -352,7 +350,7 @@
} }
var totalPieces = []; var totalPieces = [];
var currentPieces = this.getPieceStatus(task.bitfield, task.numPieces); var currentPieces = getPieceStatus(task.bitfield, task.numPieces);
var maxCompletedPieceCount = 0; var maxCompletedPieceCount = 0;
var maxCompletedPercent = task.completePercent; var maxCompletedPercent = task.completePercent;
@ -364,7 +362,7 @@
for (var i = 0; i < peers.length; i++) { for (var i = 0; i < peers.length; i++) {
var peer = peers[i]; var peer = peers[i];
var peerPieces = this.getPieceStatus(peer.bitfield, task.numPieces); var peerPieces = getPieceStatus(peer.bitfield, task.numPieces);
var completedPieceCount = 0; var completedPieceCount = 0;
for (var j = 0; j < peerPieces.length; j++) { for (var j = 0; j < peerPieces.length; j++) {

View file

@ -118,6 +118,19 @@
return result; return result;
}, },
countArray: function (array, value) {
if (!angular.isArray(array) || array.length < 1) {
return 0;
}
var count = 0;
for (var i = 0; i < array.length; i++) {
count += (array[i] == value ? 1 : 0);
}
return count;
},
parseOrderType: function (value) { parseOrderType: function (value) {
var values = value.split(':'); var values = value.split(':');