From 346f09ce3975a6a6c092875fdf3278edda4932d2 Mon Sep 17 00:00:00 2001 From: Vishal Dalwadi <51291657+VishalDalwadi@users.noreply.github.com> Date: Fri, 14 Mar 2025 06:16:31 -0700 Subject: [PATCH] NET-1986: Only report online hosts. (#3370) * feat(go): only report online hosts. * feat(go): only report online external clients. --- logic/extpeers.go | 21 +++++++++++++++++++++ logic/hosts.go | 27 +++++++++++++++++++++++++++ pro/util.go | 5 +++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/logic/extpeers.go b/logic/extpeers.go index 89934e56..b8e660ca 100644 --- a/logic/extpeers.go +++ b/logic/extpeers.go @@ -416,6 +416,27 @@ func GetAllExtClients() ([]models.ExtClient, error) { return clients, nil } +// GetAllExtClientsWithStatus - returns all external clients with +// given status. +func GetAllExtClientsWithStatus(status models.NodeStatus) ([]models.ExtClient, error) { + extClients, err := GetAllExtClients() + if err != nil { + return nil, err + } + + var validExtClients []models.ExtClient + for _, extClient := range extClients { + nodes := []models.Node{extClient.ConvertToStaticNode()} + AddStatusToNodes(nodes) + + if nodes[0].Status == status { + validExtClients = append(validExtClients, extClient) + } + } + + return validExtClients, nil +} + // ToggleExtClientConnectivity - enables or disables an ext client func ToggleExtClientConnectivity(client *models.ExtClient, enable bool) (models.ExtClient, error) { update := models.CustomExtClient{ diff --git a/logic/hosts.go b/logic/hosts.go index 8fd125c5..670acd09 100644 --- a/logic/hosts.go +++ b/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{} diff --git a/pro/util.go b/pro/util.go index e4ed2bdf..be3b4d9f 100644 --- a/pro/util.go +++ b/pro/util.go @@ -5,6 +5,7 @@ package pro import ( "encoding/base64" + "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/logic" ) @@ -26,11 +27,11 @@ 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) } - clients, cErr := logic.GetAllExtClients() + clients, cErr := logic.GetAllExtClientsWithStatus(models.OnlineSt) if cErr == nil { limits.Clients = len(clients) }