2017-06-20 11 views
0

サービスで特定のアプリケーションに関連するサーバー/ホストの一覧を返す関数を設定しました。フロントエンドの目的のために、私はそのホスト上のいくつのサービスが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; 
      }; 

すべてのヘルプは大歓迎または任意の簡素以上のエレガントな方法を提案していることは素晴らしいだろう。

答えて

2

使用の$ q.allと約束チェーンを見ます

service.getHosts = function (AppName) { 
    //returns a http promise 
    return $http.get('http://localhost:9000/App/' + AppName) 
     .then(function (response) { 
      var Data = response.data.recordset; 

      //makes an array of $http promises 
      var promises = Data.map(function (dt) { 
       return $http.get('http://localhost:9000/Service/' + dt.HostName) 
      }); 
      //executes all the promises in the array simultaneously and returns a single promise object 
      //whose result(response.data) will be an array of all the responses from the promises in the array 
      return $q.all(promises); 
     }) 
}; 
//Call the service method 

service.getHosts("app_name_here") 
.then(function(response){ 
    //response.data will have an array of responses for all the inner $http.get calls 
    //you wont still be able to return the data because everything is asynchronous 
    //Populate your data in the $scope here after data massaging 
}) 
+0

ありがとうございました!!!!!! –

関連する問題