reduce request in task detail page

This commit is contained in:
MaysWind 2016-07-03 18:07:07 +08:00
parent 8a67807fc9
commit 0a8429cf2c
3 changed files with 159 additions and 126 deletions

View file

@ -16,28 +16,12 @@
return aria2SettingService.getSpecifiedOptions(keys); return aria2SettingService.getSpecifiedOptions(keys);
}; };
var refreshBtPeers = function (task, silent) {
return aria2TaskService.getBtTaskPeers(task, function (response) {
if (!response.success) {
return;
}
var peers = response.data;
if (!ariaNgCommonService.extendArray(peers, $scope.context.btPeers, 'peerId')) {
$scope.context.btPeers = peers;
}
$scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.context.btPeers);
}, silent, true);
};
var refreshDownloadTask = function (silent) { var refreshDownloadTask = function (silent) {
if (pauseDownloadTaskRefresh) { if (pauseDownloadTaskRefresh) {
return; return;
} }
return aria2TaskService.getTaskStatus($routeParams.gid, function (response) { return aria2TaskService.getTaskStatusAndBtPeers($routeParams.gid, function (response) {
if (!response.success) { if (!response.success) {
if (response.data.message == aria2RpcErrors.Unauthorized.message) { if (response.data.message == aria2RpcErrors.Unauthorized.message) {
$interval.cancel(downloadTaskRefreshPromise); $interval.cancel(downloadTaskRefreshPromise);
@ -46,11 +30,9 @@
return; return;
} }
var task = response.data; var task = response.task;
if (task.status == 'active' && task.bittorrent) { if (!task.bittorrent) {
refreshBtPeers(task, true);
} else {
if (tabOrders.indexOf('btpeers') >= 0) { if (tabOrders.indexOf('btpeers') >= 0) {
tabOrders.splice(tabOrders.indexOf('btpeers'), 1); tabOrders.splice(tabOrders.indexOf('btpeers'), 1);
} }
@ -62,12 +44,22 @@
$scope.task = ariaNgCommonService.copyObjectTo(task, $scope.task); $scope.task = ariaNgCommonService.copyObjectTo(task, $scope.task);
if (response.peers) {
var peers = response.peers;
if (!ariaNgCommonService.extendArray(peers, $scope.context.btPeers, 'peerId')) {
$scope.context.btPeers = peers;
}
$scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.context.btPeers);
}
$rootScope.taskContext.list = [$scope.task]; $rootScope.taskContext.list = [$scope.task];
$rootScope.taskContext.selected = {}; $rootScope.taskContext.selected = {};
$rootScope.taskContext.selected[$scope.task.gid] = true; $rootScope.taskContext.selected[$scope.task.gid] = true;
ariaNgMonitorService.recordStat(task.gid, task); ariaNgMonitorService.recordStat(task.gid, task);
}, silent); }, silent, true);
}; };
var setSelectFiles = function (silent) { var setSelectFiles = function (silent) {

View file

@ -24,24 +24,28 @@
return getAria2MethodFullName(eventName); return getAria2MethodFullName(eventName);
}; };
var invoke = function (context) { var invoke = function (requestContext, returnContextOnly) {
if (returnContextOnly) {
return requestContext;
}
var uniqueId = ariaNgCommonService.generateUniqueId(); var uniqueId = ariaNgCommonService.generateUniqueId();
var requestBody = { var requestBody = {
jsonrpc: aria2RpcConstants.rpcServiceVersion, jsonrpc: aria2RpcConstants.rpcServiceVersion,
method: context.methodName, method: requestContext.methodName,
id: uniqueId, id: uniqueId,
params: context.params params: requestContext.params
}; };
var requestContext = { var invokeContext = {
uniqueId: uniqueId, uniqueId: uniqueId,
requestBody: requestBody, requestBody: requestBody,
successCallback: context.successCallback, successCallback: requestContext.successCallback,
errorCallback: context.errorCallback errorCallback: requestContext.errorCallback
}; };
return rpcImplementService.request(requestContext); return rpcImplementService.request(invokeContext);
}; };
var registerEvent = function (eventName, callbacks) { var registerEvent = function (eventName, callbacks) {
@ -116,15 +120,15 @@
} }
if (arguments.length > 1) { if (arguments.length > 1) {
var invokeContext = arguments[1]; var innerContext = arguments[1];
context.successCallback = function (id, result) { context.successCallback = function (id, result) {
if (invokeContext.callback) { if (innerContext.callback) {
invokeContext.callback({ innerContext.callback({
id: id, id: id,
success: true, success: true,
data: result, data: result,
context: invokeContext context: innerContext
}); });
} }
}; };
@ -132,17 +136,17 @@
context.errorCallback = function (id, error) { context.errorCallback = function (id, error) {
var errorProcessed = false; var errorProcessed = false;
if (!invokeContext.silent) { if (!innerContext.silent) {
errorProcessed = processError(error); errorProcessed = processError(error);
} }
if (invokeContext.callback) { if (innerContext.callback) {
invokeContext.callback({ innerContext.callback({
id: id, id: id,
success: false, success: false,
data: error, data: error,
errorProcessed: errorProcessed, errorProcessed: errorProcessed,
context: invokeContext context: innerContext
}); });
} }
}; };
@ -194,7 +198,7 @@
return requestParams; return requestParams;
}, },
addUri: function (context) { addUri: function (context, returnContextOnly) {
var urls = context.task.urls; var urls = context.task.urls;
var options = angular.copy(context.task.options); var options = angular.copy(context.task.options);
@ -202,7 +206,7 @@
options.pause = 'true'; options.pause = 'true';
} }
return invoke(buildRequestContext('addUri', context, urls, options)); return invoke(buildRequestContext('addUri', context, urls, options), !!returnContextOnly);
}, },
addUriMulti: function (context) { addUriMulti: function (context) {
var contexts = []; var contexts = [];
@ -219,7 +223,7 @@
return invokeMulti(this.addUri, contexts, context.callback); return invokeMulti(this.addUri, contexts, context.callback);
}, },
addTorrent: function (context) { addTorrent: function (context, returnContextOnly) {
var content = context.task.content; var content = context.task.content;
var options = angular.copy(context.task.options); var options = angular.copy(context.task.options);
@ -227,9 +231,9 @@
options.pause = 'true'; options.pause = 'true';
} }
return invoke(buildRequestContext('addTorrent', context, content, [], options)); return invoke(buildRequestContext('addTorrent', context, content, [], options), !!returnContextOnly);
}, },
addMetalink: function (context) { addMetalink: function (context, returnContextOnly) {
var content = context.task.content; var content = context.task.content;
var options = angular.copy(context.task.options); var options = angular.copy(context.task.options);
@ -237,13 +241,13 @@
options.pause = 'true'; options.pause = 'true';
} }
return invoke(buildRequestContext('addMetalink', context, content, [], options)); return invoke(buildRequestContext('addMetalink', context, content, [], options), !!returnContextOnly);
}, },
remove: function (context) { remove: function (context, returnContextOnly) {
return invoke(buildRequestContext('remove', context, context.gid)); return invoke(buildRequestContext('remove', context, context.gid), !!returnContextOnly);
}, },
forceRemove: function (context) { forceRemove: function (context, returnContextOnly) {
return invoke(buildRequestContext('forceRemove', context, context.gid)); return invoke(buildRequestContext('forceRemove', context, context.gid), !!returnContextOnly);
}, },
forceRemoveMulti: function (context) { forceRemoveMulti: function (context) {
var contexts = []; var contexts = [];
@ -257,14 +261,14 @@
return invokeMulti(this.forceRemove, contexts, context.callback); return invokeMulti(this.forceRemove, contexts, context.callback);
}, },
pause: function (context) { pause: function (context, returnContextOnly) {
return invoke(buildRequestContext('pause', context, context.gid)); return invoke(buildRequestContext('pause', context, context.gid), !!returnContextOnly);
}, },
pauseAll: function (context) { pauseAll: function (context, returnContextOnly) {
return invoke(buildRequestContext('pauseAll', context)); return invoke(buildRequestContext('pauseAll', context), !!returnContextOnly);
}, },
forcePause: function (context) { forcePause: function (context, returnContextOnly) {
return invoke(buildRequestContext('forcePause', context, context.gid)); return invoke(buildRequestContext('forcePause', context, context.gid), !!returnContextOnly);
}, },
forcePauseMulti: function (context) { forcePauseMulti: function (context) {
var contexts = []; var contexts = [];
@ -278,11 +282,11 @@
return invokeMulti(this.forcePause, contexts, context.callback); return invokeMulti(this.forcePause, contexts, context.callback);
}, },
forcePauseAll: function (context) { forcePauseAll: function (context, returnContextOnly) {
return invoke(buildRequestContext('forcePauseAll', context)); return invoke(buildRequestContext('forcePauseAll', context), !!returnContextOnly);
}, },
unpause: function (context) { unpause: function (context, returnContextOnly) {
return invoke(buildRequestContext('unpause', context, context.gid)); return invoke(buildRequestContext('unpause', context, context.gid), !!returnContextOnly);
}, },
unpauseMulti: function (context) { unpauseMulti: function (context) {
var contexts = []; var contexts = [];
@ -296,69 +300,69 @@
return invokeMulti(this.unpause, contexts, context.callback); return invokeMulti(this.unpause, contexts, context.callback);
}, },
unpauseAll: function (context) { unpauseAll: function (context, returnContextOnly) {
return invoke(buildRequestContext('unpauseAll', context)); return invoke(buildRequestContext('unpauseAll', context), !!returnContextOnly);
}, },
tellStatus: function (context) { tellStatus: function (context, returnContextOnly) {
return invoke(buildRequestContext('tellStatus', context, context.gid)); return invoke(buildRequestContext('tellStatus', context, context.gid), !!returnContextOnly);
}, },
getUris: function (context) { getUris: function (context, returnContextOnly) {
return invoke(buildRequestContext('getUris', context, context.gid)); return invoke(buildRequestContext('getUris', context, context.gid), !!returnContextOnly);
}, },
getFiles: function (context) { getFiles: function (context, returnContextOnly) {
return invoke(buildRequestContext('getFiles', context, context.gid)); return invoke(buildRequestContext('getFiles', context, context.gid), !!returnContextOnly);
}, },
getPeers: function (context) { getPeers: function (context, returnContextOnly) {
return invoke(buildRequestContext('getPeers', context, context.gid)); return invoke(buildRequestContext('getPeers', context, context.gid), !!returnContextOnly);
}, },
getServers: function (context) { getServers: function (context, returnContextOnly) {
return invoke(buildRequestContext('getServers', context, context.gid)); return invoke(buildRequestContext('getServers', context, context.gid), !!returnContextOnly);
}, },
tellActive: function (context) { tellActive: function (context, returnContextOnly) {
return invoke(buildRequestContext('tellActive', context, return invoke(buildRequestContext('tellActive', context,
angular.isDefined(context.requestParams) ? context.requestParams: null angular.isDefined(context.requestParams) ? context.requestParams: null
)); ), !!returnContextOnly);
}, },
tellWaiting: function (context) { tellWaiting: function (context, returnContextOnly) {
return invoke(buildRequestContext('tellWaiting', context, return invoke(buildRequestContext('tellWaiting', context,
angular.isDefined(context.offset) ? context.offset : 0, angular.isDefined(context.offset) ? context.offset : 0,
angular.isDefined(context.num) ? context.num : 1000, angular.isDefined(context.num) ? context.num : 1000,
angular.isDefined(context.requestParams) ? context.requestParams : null angular.isDefined(context.requestParams) ? context.requestParams : null
)); ), !!returnContextOnly);
}, },
tellStopped: function (context) { tellStopped: function (context, returnContextOnly) {
return invoke(buildRequestContext('tellStopped', context, return invoke(buildRequestContext('tellStopped', context,
angular.isDefined(context.offset) ? context.offset : 0, angular.isDefined(context.offset) ? context.offset : 0,
angular.isDefined(context.num) ? context.num : 1000, angular.isDefined(context.num) ? context.num : 1000,
angular.isDefined(context.requestParams) ? context.requestParams: null angular.isDefined(context.requestParams) ? context.requestParams: null
)); ), !!returnContextOnly);
}, },
changePosition: function (context) { changePosition: function (context, returnContextOnly) {
return invoke(buildRequestContext('changePosition', context, context.gid, context.pos, context.how)); return invoke(buildRequestContext('changePosition', context, context.gid, context.pos, context.how), !!returnContextOnly);
}, },
// changeUri: function (context) { changeUri: function (context, returnContextOnly) {
// return invoke('changeUri', context); return invoke(buildRequestContext('changeUri', context, context.gid, context.fileIndex, context.delUris, context.addUris), !!returnContextOnly);
// },
getOption: function (context) {
return invoke(buildRequestContext('getOption', context, context.gid));
}, },
changeOption: function (context) { getOption: function (context, returnContextOnly) {
return invoke(buildRequestContext('changeOption', context, context.gid, context.options)); return invoke(buildRequestContext('getOption', context, context.gid), !!returnContextOnly);
}, },
getGlobalOption: function (context) { changeOption: function (context, returnContextOnly) {
return invoke(buildRequestContext('getGlobalOption', context)); return invoke(buildRequestContext('changeOption', context, context.gid, context.options), !!returnContextOnly);
}, },
changeGlobalOption: function (context) { getGlobalOption: function (context, returnContextOnly) {
return invoke(buildRequestContext('changeGlobalOption', context, context.options)); return invoke(buildRequestContext('getGlobalOption', context), !!returnContextOnly);
}, },
getGlobalStat: function (context) { changeGlobalOption: function (context, returnContextOnly) {
return invoke(buildRequestContext('getGlobalStat', context)); return invoke(buildRequestContext('changeGlobalOption', context, context.options), !!returnContextOnly);
}, },
purgeDownloadResult: function (context) { getGlobalStat: function (context, returnContextOnly) {
return invoke(buildRequestContext('purgeDownloadResult', context)); return invoke(buildRequestContext('getGlobalStat', context), !!returnContextOnly);
}, },
removeDownloadResult: function (context) { purgeDownloadResult: function (context, returnContextOnly) {
return invoke(buildRequestContext('removeDownloadResult', context, context.gid)); return invoke(buildRequestContext('purgeDownloadResult', context), !!returnContextOnly);
},
removeDownloadResult: function (context, returnContextOnly) {
return invoke(buildRequestContext('removeDownloadResult', context, context.gid), !!returnContextOnly);
}, },
removeDownloadResultMulti: function (context) { removeDownloadResultMulti: function (context) {
var contexts = []; var contexts = [];
@ -372,20 +376,20 @@
return invokeMulti(this.removeDownloadResult, contexts, context.callback); return invokeMulti(this.removeDownloadResult, contexts, context.callback);
}, },
getVersion: function (context) { getVersion: function (context, returnContextOnly) {
return invoke(buildRequestContext('getVersion', context)); return invoke(buildRequestContext('getVersion', context), !!returnContextOnly);
}, },
getSessionInfo: function (context) { getSessionInfo: function (context, returnContextOnly) {
return invoke(buildRequestContext('getSessionInfo', context)); return invoke(buildRequestContext('getSessionInfo', context), !!returnContextOnly);
}, },
shutdown: function (context) { shutdown: function (context, returnContextOnly) {
return invoke(buildRequestContext('shutdown', context)); return invoke(buildRequestContext('shutdown', context), !!returnContextOnly);
}, },
forceShutdown: function (context) { forceShutdown: function (context, returnContextOnly) {
return invoke(buildRequestContext('forceShutdown', context)); return invoke(buildRequestContext('forceShutdown', context), !!returnContextOnly);
}, },
saveSession: function (context) { saveSession: function (context, returnContextOnly) {
return invoke(buildRequestContext('saveSession', context)); return invoke(buildRequestContext('saveSession', context), !!returnContextOnly);
}, },
multicall: function (context) { multicall: function (context) {
return invoke(buildRequestContext('system.multicall', context, context.methods)); return invoke(buildRequestContext('system.multicall', context, context.methods));

View file

@ -96,6 +96,39 @@
return task; return task;
}; };
var processBtPeers = function (peers, task, includeLocalPeer) {
if (!peers) {
return peers;
}
var localTaskCompletedPieces = getPieceStatus(task.bitfield, task.numPieces);
var localTaskCompletedPieceCount = ariaNgCommonService.countArray(localTaskCompletedPieces, true);
var localTaskCompletedPercent = task.completePercent;
for (var i = 0; i < peers.length; i++) {
var peer = peers[i];
var upstreamToSpeed = peer.uploadSpeed;
var downstreamFromSpeed = peer.downloadSpeed;
var completedPieces = getPieceStatus(peer.bitfield, task.numPieces);
var completedPieceCount = ariaNgCommonService.countArray(completedPieces, true);
peer.name = peer.ip + ':' + peer.port;
peer.completePercent = completedPieceCount / task.numPieces * 100;
peer.downloadSpeed = upstreamToSpeed;
peer.uploadSpeed = downstreamFromSpeed;
if (completedPieceCount == localTaskCompletedPieceCount && peer.completePercent != localTaskCompletedPercent) {
peer.completePercent = localTaskCompletedPercent;
}
}
if (includeLocalPeer) {
peers.push(createLocalPeerFromTask(task));
}
return peers;
};
var getPieceStatus = function (bitField, pieceCount) { var getPieceStatus = function (bitField, pieceCount) {
var pieces = []; var pieces = [];
@ -249,7 +282,7 @@
return this.setTaskOption(gid, 'select-file', selectedFileIndex, callback, silent); return this.setTaskOption(gid, 'select-file', selectedFileIndex, callback, silent);
}, },
getBtTaskPeers: function (task, callback, silent, includeLocal) { getBtTaskPeers: function (task, callback, silent, includeLocalPeer) {
return aria2RpcService.getPeers({ return aria2RpcService.getPeers({
gid: task.gid, gid: task.gid,
silent: !!silent, silent: !!silent,
@ -259,31 +292,35 @@
} }
if (response.success) { if (response.success) {
var peers = response.data; processBtPeers(response.data, task, includeLocalPeer);
var localTaskCompletedPieces = getPieceStatus(task.bitfield, task.numPieces); }
var localTaskCompletedPieceCount = ariaNgCommonService.countArray(localTaskCompletedPieces, true);
var localTaskCompletedPercent = task.completePercent;
for (var i = 0; i < peers.length; i++) { callback(response);
var peer = peers[i]; }
var upstreamToSpeed = peer.uploadSpeed; });
var downstreamFromSpeed = peer.downloadSpeed; },
var completedPieces = getPieceStatus(peer.bitfield, task.numPieces); getTaskStatusAndBtPeers: function (gid, callback, silent, includeLocalPeer) {
var completedPieceCount = ariaNgCommonService.countArray(completedPieces, true); return aria2RpcService.multicall({
methods: [
aria2RpcService.tellStatus({ gid: gid }, true),
aria2RpcService.getPeers({ gid: gid }, true)
],
silent: !!silent,
callback: function (response) {
if (!callback) {
return;
}
peer.name = peer.ip + ':' + peer.port; response.task = {};
peer.completePercent = completedPieceCount / task.numPieces * 100;
peer.downloadSpeed = upstreamToSpeed;
peer.uploadSpeed = downstreamFromSpeed;
if (completedPieceCount == localTaskCompletedPieceCount && peer.completePercent != localTaskCompletedPercent) { if (response.success) {
peer.completePercent = localTaskCompletedPercent; response.task = response.data[0][0];
} processDownloadTask(response.task);
} }
if (includeLocal) { if (response.success && response.task.bittorrent) {
peers.push(createLocalPeerFromTask(task)); response.peers = response.data[1][0];
} processBtPeers(response.peers, response.task, includeLocalPeer);
} }
callback(response); callback(response);