我在我的服务中设置了一个函数,以返回与特定应用程序相关的服务器/主机列表。对于前端的目的,我一直试图给主机分配一个颜色,这取决于主机上有多少服务正在运行okay/warning/critical。为了实现这一点,我首先进行一次api调用,以获得与该应用程序相关的所有主机,然后循环访问返回的主机列表,并执行另一个api调用来获取服务。http呼叫中的角度http呼叫
我的问题是,他们正在解决正确的顺序,所以我的Data2变量返回“未定义”。如何在第一个for循环中解决它,以便我可以为每个主机分配一个状态颜色?
有没有更好的实现方法?
这是我在我的服务中定义的功能。
// Function to get Servers and all their information **********************************
service.getHosts = function(AppName){
var HostList = [];
//intial http call to get the correct hostlist associated with the selected application ************
var promise = $http.get('http://localhost:9000/App/' + AppName);
promise.then(function(response){
var Data = response.data.recordset;
//Looping through each host is the recordset to push them into the HostList[] **********************
for (i = 0; i <= Data.length -1; i++){
//variables for the loop
var StatusColor = '';
var StatusTextColor = '';
var count = 0;
//another http call to get the services for each host in the Hostlist ******************************
$http.get('http://localhost:9000/Service/' + Data[i].HostName)
.then(function(response){
var Data2 = response.recordset;
//looping through the services to see if any of the services have status other than ok (shortstatus != 0) ********
for(i = 0; i<= Data2.length-1; i++){
if(Data2[i].ShortStatus != 0){
count = count + 1;
}
}
//Assigning the status color for each host depending on how many services are not ok (either warning or critical) *************
if (count == 0){
StatusColor ='rgb(255,152,0)';
StatusTextColor = 'black';
}else if (count == 1){
StatusColor ='rgb(255,152,0)';
StatusTextColor = 'white';
}else{
StatusColor = 'rgb(244,67,54)';
StatusTextColor = 'white';
}
//Pushing host information and status color to the HostList **********************
HostList.push({
"address":Data[i].Address,
"hostname":Data[i].HostName.split('.')[0],
"fullhostname":Data[i].HostName,
"statuscolor":StatusColor,
// "textcolor":'black'
})
});
}
})
return HostList;
};
任何帮助非常感谢或任何建议更简单或更优雅的方式将是真棒。
谢谢!!!!!! –