From 2e44b47d311431a6ed76462319ab65649a6802df Mon Sep 17 00:00:00 2001 From: MaysWind Date: Wed, 25 May 2016 00:20:32 +0800 Subject: [PATCH] support displaying health percent of bt task in task detail page --- app/scripts/controllers/task-detail.js | 14 ++++-- app/scripts/core/utils.js | 60 ++++++++++++++++++++++++++ app/scripts/langs/en-US.js | 1 + app/scripts/langs/zh-CN.js | 1 + app/views/task-detail.html | 26 +++++++---- 5 files changed, 90 insertions(+), 12 deletions(-) diff --git a/app/scripts/controllers/task-detail.js b/app/scripts/controllers/task-detail.js index 8ee9c6c..67b9595 100644 --- a/app/scripts/controllers/task-detail.js +++ b/app/scripts/controllers/task-detail.js @@ -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) { diff --git a/app/scripts/core/utils.js b/app/scripts/core/utils.js index 274a888..c49f5a1 100644 --- a/app/scripts/core/utils.js +++ b/app/scripts/core/utils.js @@ -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; } }; }]); diff --git a/app/scripts/langs/en-US.js b/app/scripts/langs/en-US.js index 8181a59..4edf458 100644 --- a/app/scripts/langs/en-US.js +++ b/app/scripts/langs/en-US.js @@ -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', diff --git a/app/scripts/langs/zh-CN.js b/app/scripts/langs/zh-CN.js index 1965288..c895c3b 100644 --- a/app/scripts/langs/zh-CN.js +++ b/app/scripts/langs/zh-CN.js @@ -57,6 +57,7 @@ 'File List': '文件列表', 'Peers': '连接状态', 'Task Status': '任务状态', + 'Health Percent': '健康度', 'Info Hash': '特征值', 'Seeders': '种子数', 'Connections': '连接数', diff --git a/app/views/task-detail.html b/app/views/task-detail.html index b3a8d1d..2447d46 100644 --- a/app/views/task-detail.html +++ b/app/views/task-detail.html @@ -51,6 +51,14 @@ +
+
+ Health Percent +
+
+ +
+
Download @@ -149,16 +157,16 @@
Address
-
+
Status
-
+
Percent
-
+
Download Speed
-
+
Upload Speed
@@ -168,17 +176,17 @@
-
+
-
- +
+
-
+
-
+