Fix PingService when loading results from cache
This commit is contained in:
parent
ff878c2a28
commit
07c7cdd51d
1 changed files with 30 additions and 23 deletions
|
@ -441,6 +441,29 @@ quayApp = angular.module('quay', quayDependencies, function($provide, cfpLoading
|
||||||
var pingService = {};
|
var pingService = {};
|
||||||
var pingCache = {};
|
var pingCache = {};
|
||||||
|
|
||||||
|
var invokeCallback = function($scope, pings, callback) {
|
||||||
|
if (pings[0] == -1) {
|
||||||
|
setTimeout(function() {
|
||||||
|
$scope.$apply(function() {
|
||||||
|
callback(-1, false, -1);
|
||||||
|
});
|
||||||
|
}, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum = 0;
|
||||||
|
for (var i = 0; i < pings.length; ++i) {
|
||||||
|
sum += pings[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report the average ping.
|
||||||
|
setTimeout(function() {
|
||||||
|
$scope.$apply(function() {
|
||||||
|
callback(Math.floor(sum / pings.length), true, pings.length);
|
||||||
|
});
|
||||||
|
}, 0);
|
||||||
|
};
|
||||||
|
|
||||||
var reportPingResult = function($scope, url, ping, callback) {
|
var reportPingResult = function($scope, url, ping, callback) {
|
||||||
// Lookup the cached ping data, if any.
|
// Lookup the cached ping data, if any.
|
||||||
var cached = pingCache[url];
|
var cached = pingCache[url];
|
||||||
|
@ -453,28 +476,15 @@ quayApp = angular.module('quay', quayDependencies, function($provide, cfpLoading
|
||||||
// If an error occurred, report it and done.
|
// If an error occurred, report it and done.
|
||||||
if (ping < 0) {
|
if (ping < 0) {
|
||||||
cached['pings'] = [-1];
|
cached['pings'] = [-1];
|
||||||
setTimeout(function() {
|
invokeCallback($scope, pings, callback);
|
||||||
$scope.$apply(function() {
|
|
||||||
callback(-1, false, -1);
|
|
||||||
});
|
|
||||||
}, 0);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, add the current ping and determine the average.
|
// Otherwise, add the current ping and determine the average.
|
||||||
cached['pings'].push(ping);
|
cached['pings'].push(ping);
|
||||||
|
|
||||||
var sum = 0;
|
// Invoke the callback.
|
||||||
for (var i = 0; i < cached['pings'].length; ++i) {
|
invokeCallback($scope, cached['pings'], callback);
|
||||||
sum += cached['pings'][i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Report the average ping.
|
|
||||||
setTimeout(function() {
|
|
||||||
$scope.$apply(function() {
|
|
||||||
callback(Math.floor(sum / cached['pings'].length), true, cached['pings'].length);
|
|
||||||
});
|
|
||||||
}, 0);
|
|
||||||
|
|
||||||
// Schedule another check if we've done less than three.
|
// Schedule another check if we've done less than three.
|
||||||
if (cached['pings'].length < 3) {
|
if (cached['pings'].length < 3) {
|
||||||
|
@ -510,12 +520,7 @@ quayApp = angular.module('quay', quayDependencies, function($provide, cfpLoading
|
||||||
|
|
||||||
pingService.pingUrl = function($scope, url, callback) {
|
pingService.pingUrl = function($scope, url, callback) {
|
||||||
if (pingCache[url]) {
|
if (pingCache[url]) {
|
||||||
cached = pingCache[url];
|
invokeCallback($scope, pingCache[url]['pings'], callback);
|
||||||
setTimeout(function() {
|
|
||||||
$scope.$apply(function() {
|
|
||||||
callback(cached.result, cached.success);
|
|
||||||
});
|
|
||||||
}, 0);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5401,7 +5406,9 @@ quayApp.directive('locationView', function () {
|
||||||
|
|
||||||
$scope.getLocationTooltip = function(location, ping) {
|
$scope.getLocationTooltip = function(location, ping) {
|
||||||
var tip = $scope.getLocationTitle(location) + '<br>';
|
var tip = $scope.getLocationTitle(location) + '<br>';
|
||||||
if (ping < 0) {
|
if (ping == null) {
|
||||||
|
tip += '(Loading)';
|
||||||
|
} else if (ping < 0) {
|
||||||
tip += '<br><b>Note: Could not contact server</b>';
|
tip += '<br><b>Note: Could not contact server</b>';
|
||||||
} else {
|
} else {
|
||||||
tip += 'Estimated Ping: ' + (ping ? ping + 'ms' : '(Loading)');
|
tip += 'Estimated Ping: ' + (ping ? ping + 'ms' : '(Loading)');
|
||||||
|
|
Reference in a new issue