فهرست منبع

feat(go): only report online hosts.

Vishal Dalwadi 6 ماه پیش
والد
کامیت
5eef3d5931
2فایلهای تغییر یافته به همراه29 افزوده شده و 1 حذف شده
  1. 27 0
      logic/hosts.go
  2. 2 1
      pro/util.go

+ 27 - 0
logic/hosts.go

@@ -106,6 +106,33 @@ func GetAllHosts() ([]models.Host, error) {
 	return currHosts, nil
 }
 
+// GetAllHostsWithStatus - returns all hosts with at least one
+// node with given status.
+func GetAllHostsWithStatus(status models.NodeStatus) ([]models.Host, error) {
+	hosts, err := GetAllHosts()
+	if err != nil {
+		return nil, err
+	}
+
+	var validHosts []models.Host
+	for _, host := range hosts {
+		if len(host.Nodes) == 0 {
+			continue
+		}
+
+		nodes := AddStatusToNodes(GetHostNodes(&host))
+
+		for _, node := range nodes {
+			if node.Status == status {
+				validHosts = append(validHosts, host)
+				break
+			}
+		}
+	}
+
+	return validHosts, nil
+}
+
 // GetAllHostsAPI - get's all the hosts in an API usable format
 func GetAllHostsAPI(hosts []models.Host) []models.ApiHost {
 	apiHosts := []models.ApiHost{}

+ 2 - 1
pro/util.go

@@ -5,6 +5,7 @@ package pro
 
 import (
 	"encoding/base64"
+	"github.com/gravitl/netmaker/models"
 
 	"github.com/gravitl/netmaker/logic"
 )
@@ -26,7 +27,7 @@ func base64decode(input string) []byte {
 
 func getCurrentServerUsage() (limits Usage) {
 	limits.SetDefaults()
-	hosts, hErr := logic.GetAllHosts()
+	hosts, hErr := logic.GetAllHostsWithStatus(models.OnlineSt)
 	if hErr == nil {
 		limits.Hosts = len(hosts)
 	}