support displaying health percent of bt task in task detail page

This commit is contained in:
MaysWind 2016-05-25 00:20:32 +08:00
parent bee3e2af94
commit 2e44b47d31
5 changed files with 90 additions and 12 deletions

View file

@ -4,13 +4,20 @@
angular.module('ariaNg').controller('TaskDetailController', ['$scope', '$routeParams', '$interval', 'aria2RpcService', 'ariaNgSettingService', 'utils', function ($scope, $routeParams, $interval, aria2RpcService, ariaNgSettingService, utils) {
var downloadTaskRefreshPromise = null;
var refreshPeers = function () {
var refreshPeers = function (task) {
return aria2RpcService.getPeers({
params: [$routeParams.gid],
params: [task.gid],
callback: function (result) {
if (!utils.extendArray(result, $scope.peers, 'peerId')) {
$scope.peers = result;
}
for (var i = 0; i < $scope.peers.length; i++) {
var peer = $scope.peers[i];
peer.completePercent = utils.estimateCompletedPercentFromBitField(peer.bitfield) * 100;
}
$scope.healthPercent = utils.estimateHealthPercentFromPeers(task, $scope.peers);
}
})
};
@ -22,7 +29,7 @@
var task = utils.processDownloadTask(result);
if (task.status == 'active' && task.bittorrent) {
refreshPeers();
refreshPeers(task);
}
$scope.task = utils.copyObjectTo(task, $scope.task);
@ -34,6 +41,7 @@
currentTab: 'overview'
};
$scope.healthPercent = 0;
$scope.loadPromise = refreshDownloadTask();
if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) {

View file

@ -142,6 +142,66 @@
return this.type + ":" + this.reverse.toString();
}
}
},
estimateCompletedPercentFromBitField: function (bitfield) {
var totalLength = bitfield.length * 16;
var completedLength = 0;
if (totalLength == 0) {
return 0;
}
for (var i = 0; i < bitfield.length; i++) {
var num = parseInt(bitfield[i], 16);
completedLength += num;
}
return completedLength / totalLength;
},
estimateHealthPercentFromPeers: function (task, peers) {
if (peers.length < 1) {
return task.completePercent;
}
var bitfieldArr = new Array(task.bitfield.length);
var totalLength = task.bitfield.length * 16;
var healthBitCount = 0;
for (var i = 0; i < task.bitfield.length; i++) {
var num = parseInt(task.bitfield[i], 16);
bitfieldArr[i] = num;
}
for (var i = 0; i < peers.length; i++) {
var peer = peers[i];
var bitfield = peer.bitfield;
for (var j = 0; j < bitfield.length; j++) {
var num = parseInt(bitfield[j], 16);
bitfieldArr[j] += num;
}
}
while (true) {
var completed = true;
for (var i = 0; i < bitfieldArr.length; i++) {
if (bitfieldArr[i] > 16) {
healthBitCount += 16;
bitfieldArr[i] -= 16;
} else {
healthBitCount += bitfieldArr[i];
bitfieldArr[i] = 0;
completed = false;
}
}
if (!completed) {
break;
}
}
return healthBitCount / totalLength * 100;
}
};
}]);

View file

@ -57,6 +57,7 @@
'File List': 'File List',
'Peers': 'Peers',
'Task Status': 'Task Status',
'Health Percent': 'Health Percent',
'Info Hash': 'Info Hash',
'Seeders': 'Seeders',
'Connections': 'Connections',

View file

@ -57,6 +57,7 @@
'File List': '文件列表',
'Peers': '连接状态',
'Task Status': '任务状态',
'Health Percent': '健康度',
'Info Hash': '特征值',
'Seeders': '种子数',
'Connections': '连接数',

View file

@ -51,6 +51,14 @@
<span ng-bind="(task.completePercent | percent: 2) + '%'"></span>
</div>
</div>
<div class="row" ng-if="task.bittorrent">
<div class="setting-key col-sm-4">
<span translate>Health Percent</span>
</div>
<div class="setting-value col-sm-8">
<span ng-bind="(healthPercent | percent: 2) + '%'"></span>
</div>
</div>
<div class="row">
<div class="setting-key col-sm-4">
<span translate>Download</span>
@ -149,16 +157,16 @@
<div class="col-sm-3">
<span translate>Address</span>
</div>
<div class="col-sm-6">
<div class="col-sm-3">
<span translate>Status</span>
</div>
<div class="col-sm-1 col-xs-4">
<div class="col-sm-2 col-xs-4">
<span translate>Percent</span>
</div>
<div class="col-sm-1 col-xs-4">
<div class="col-sm-2 col-xs-4">
<span translate>Download Speed</span>
</div>
<div class="col-sm-1 col-xs-4">
<div class="col-sm-2 col-xs-4">
<span translate>Upload Speed</span>
</div>
</div>
@ -168,17 +176,17 @@
<div class="col-sm-3">
<span ng-bind="peer.ip + ':' + peer.port"></span>
</div>
<div class="col-sm-6">
<div class="col-sm-3">
</div>
<div class="col-sm-1 col-xs-4">
<!--<span ng-bind="(peer.completePercent | percent: 2) + '%'"></span>-->
<div class="col-sm-2 col-xs-4">
<span ng-bind="(peer.completePercent | percent: 2) + '%'"></span>
</div>
<div class="col-sm-1 col-xs-4">
<div class="col-sm-2 col-xs-4">
<i class="fa fa-arrow-down visible-xs-inline-block"></i>
<span class="task-download-speed" ng-bind="(peer.downloadSpeed | readableVolumn) + '/s'"></span>
</div>
<div class="col-sm-1 col-xs-4">
<div class="col-sm-2 col-xs-4">
<i class="fa fa-arrow-up visible-xs-inline-block"></i>
<span class="task-download-speed" ng-bind="(peer.uploadSpeed | readableVolumn) + '/s'"></span>
</div>