Reputation: 406
I am retrieving multiple http.get requests from a server from different json sets.
At the end of each "physical" data set I perform a calculation which requires the value from the "virtual" call, this is set up in a function called "Calculate" (see code below)
Often, this works completely fine and the correct values are returned, however occassionally the $http.get requests do not complete in time and thus the values are still set at 0.
How do I ensure both http.get requests are complete for the physical and virtual calls on each "cluster"?
function($scope, $http) {
$http.get('http:///search?idc=LH5&type=Virtual&cluster=1').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
//console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tcores1 = parseInt(tcores1) + parseInt(item.cores);
tmemory1 = parseInt(tmemory1) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 1 Total Cores: " + tcores1);
console.log("Hall 5. Cluster 1 Total Memory: " + tmemory1);
}).then(function() {
$http.get('http:///search?idc=LH5&type=Virtual&cluster=2').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tcores2 = parseInt(tcores2) + parseInt(item.cores);
tmemory2 = parseInt(tmemory2) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 2 Total Cores: " + tcores2);
console.log("Hall 5. Cluster 2 Total Memory: " + tmemory2);
});
}).then(function() {
$http.get('http:///search?idc=LH5&type=Virtual&cluster=3').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tcores3 = parseInt(tcores3) + parseInt(item.cores);
tmemory3 = parseInt(tmemory3) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 3 Total Cores: " + tcores3);
console.log("Hall 5. Cluster 3 Total memory: " + tmemory3);
});
}).then(function() {
$http.get('http:///search?idc=LH5&type=Virtual&cluster=4').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tcores4 = parseInt(tcores4) + parseInt(item.cores);
tmemory4 = parseInt(tmemory4) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 4 Total Cores: " + tcores4);
console.log("Hall 5. Cluster 4 Total memory: " + tmemory4);
});
}).then(function() {
$http.get('http:///search?idc=LH5&type=Physical&cluster=1').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tpcores1 = parseInt(tpcores1) + parseInt(item.cores);
tpmemory1 = parseInt(tpmemory1) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 1 Total Physical Cores: " + tpcores1);
console.log("Hall 5. Cluster 1 Total Physical memory: " + tpmemory1);
calculate("Cluster 1", "Core", tcores1, tpcores1);
calculate("Cluster 1", "Memory", tmemory1, tpmemory1);
});
}).then(function() {
$http.get('http:///search?idc=LH5&type=Physical&cluster=2').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tpcores2 = parseInt(tpcores2) + parseInt(item.cores);
tpcores2 = parseInt(tpcores2) + parseInt(item.cores);
}
}
})
console.log("Hall 5. Cluster 2 Total Physical Cores: " + tpcores2);
calculate("Cluster 2", "Core", tcores2, tpcores2);
calculate("Cluster 2", "Memory", tmemory2, tpmemory2);
});
}).then(function() {
$http.get('http:///search?idc=LH5&type=Physical&cluster=3').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tpcores3 = parseInt(tpcores3) + parseInt(item.cores);
tpmemory3 = parseInt(tpmemory3) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 3 Total Physical Cores: " + tpcores3);
console.log("Hall 5. Cluster 3 Total Physical memory: " + tpmemory3);
calculate("Cluster 3", "Core", tcores3, tpcores3);
calculate("Cluster 3", "Memory", tmemory3, tpmemory3);
});
}).then(function() {
$http.get('http:///search?idc=LH5&type=Physical&cluster=4').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tpcores4 = parseInt(tpcores4) + parseInt(item.cores);
tpmemory4 = parseInt(tpmemory4) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 4 Total Physical Cores: " + tpcores4);
console.log("Hall 5. Cluster 4 Total Physical Cores: " + tpcores4);
console.log("Hall 5. Cluster 4 Total Physical memory: " + tpmemory4);
calculate("Cluster 4", "Core", tcores4, tpcores4);
calculate("Cluster 4", "Memory", tmemory4, tpmemory4);
});
});
function calculate(string, type, virtual, physical) {
console.log(type + " capacity " + string + ": " + (virtual / physical).toFixed(2) * 100 + "%");
}
}]);
Upvotes: 1
Views: 84
Reputation: 34288
You are not tailing the calls correctly. It looks like the calls can be made in parallel and update independent states tcores[1..5]
and tmemory[1..5]
. To do that, you can make calls independently and then create a large promise which resolves when all the calls have resolved using $q.all.
var core1 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=1').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
//console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tcores1 = parseInt(tcores1) + parseInt(item.cores);
tmemory1 = parseInt(tmemory1) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 1 Total Cores: " + tcores1);
console.log("Hall 5. Cluster 1 Total Memory: " + tmemory1);
});
var core2 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=2').success(function(data) {
$scope.servers = data; // get data from json
angular.forEach($scope.servers, function(item) {
// console.log(item.cores);
if (parseInt(item.cores) != "NaN" && item.cores != "") {
if (angular.isNumber(parseInt(item.cores))) {
tcores2 = parseInt(tcores2) + parseInt(item.cores);
tmemory2 = parseInt(tmemory2) + parseInt(item.memory);
}
}
})
console.log("Hall 5. Cluster 2 Total Cores: " + tcores2);
console.log("Hall 5. Cluster 2 Total Memory: " + tmemory2);
});
// ...
var allRequestsComplete = false;
$q.all([core1, core2, core3, core4, core5]).then(function () {
allRequestsComplete = true;
});
function calculate() {
if (!allRequestsComplete) {
console.log(type + " capacity " + string + ": " + (virtual / physical).toFixed(2) * 100 + "%");
} else {
console.log('Calculation in progress.');
}
}
Upvotes: 1
Reputation: 6229
When your http requests can be run in parallel, you can inject $q and use $q.all()
like this:
var https = [
$http.get(/* .... */),
$http.get(/* .... */),
// as many as you wish
];
$q.all(https).then(function(results) {
console.log('All finished, results:', results);
});
Upvotes: 4
Reputation: 9597
If they can be run concurrently, then fire them all off at once and then use a $q.all
which will resolve when all promises are resolved:
var promiseObj = {};
promiseObj.virtual1 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=1');
promiseObj.virtual2 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=2');
promiseObj.physical1 = $http.get('http:///search?idc=LH5&type=Physical&cluster=1');
$q.all(promiseObj).then(function (results) {
// results.virtual1 will be the results of virtual / cluster 1 promise
// results.virtual2 will be the results of virtual / cluster 2
});
Upvotes: 4