From 3a7e555f3e43e76f1166d9b5999d089993df4494 Mon Sep 17 00:00:00 2001 From: Ali Chaddad Date: Wed, 21 Jun 2023 14:20:25 +0300 Subject: [PATCH] replace has gpu with num gpu --- grid-proxy/internal/explorer/converters.go | 12 ++++++++++-- grid-proxy/internal/explorer/db/postgres.go | 3 +++ grid-proxy/internal/explorer/server.go | 4 ---- grid-proxy/pkg/types/types.go | 5 +++-- grid-proxy/tests/queries/local_client.go | 15 ++++++++++++++- grid-proxy/tests/queries/node_test.go | 2 +- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/grid-proxy/internal/explorer/converters.go b/grid-proxy/internal/explorer/converters.go index ecc3d377..b4abd19d 100644 --- a/grid-proxy/internal/explorer/converters.go +++ b/grid-proxy/internal/explorer/converters.go @@ -26,6 +26,14 @@ func decideNodeStatus(power types.NodePower, updatedAt int64) string { } } +// getNumGPUs should be deleted after removing hasGPU +func getNumGPUs(hasGPU bool) int { + if hasGPU { + return 1 + } + return 0 +} + func nodeFromDBNode(info db.Node) types.Node { node := types.Node{ ID: info.ID, @@ -70,7 +78,7 @@ func nodeFromDBNode(info db.Node) types.Node { RentedByTwinID: uint(info.RentedByTwinID), SerialNumber: info.SerialNumber, Power: types.NodePower(info.Power), - HasGPU: info.HasGPU, + NumGPU: getNumGPUs(info.HasGPU), ExtraFee: info.ExtraFee, } node.Status = decideNodeStatus(node.Power, node.UpdatedAt) @@ -141,7 +149,7 @@ func nodeWithNestedCapacityFromDBNode(info db.Node) types.NodeWithNestedCapacity RentedByTwinID: uint(info.RentedByTwinID), SerialNumber: info.SerialNumber, Power: types.NodePower(info.Power), - HasGPU: info.HasGPU, + NumGPU: getNumGPUs(info.HasGPU), ExtraFee: info.ExtraFee, } node.Status = decideNodeStatus(node.Power, node.UpdatedAt) diff --git a/grid-proxy/internal/explorer/db/postgres.go b/grid-proxy/internal/explorer/db/postgres.go index 5f0f80f8..291e650f 100644 --- a/grid-proxy/internal/explorer/db/postgres.go +++ b/grid-proxy/internal/explorer/db/postgres.go @@ -230,6 +230,9 @@ func (d *PostgresDatabase) GetCounters(filter types.StatsFilter) (types.Counters Select("country, count(node_id) as nodes").Where(condition).Group("country").Scan(&distribution); res.Error != nil { return counters, errors.Wrap(res.Error, "couldn't get nodes distribution") } + if res := d.gormDB.Table("node").Where(condition).Where("node.has_gpu = true").Count(&counters.GPUs); res.Error != nil { + return counters, errors.Wrap(res.Error, "couldn't get node with GPU count") + } nodesDistribution := map[string]int64{} for _, d := range distribution { nodesDistribution[d.Country] = d.Nodes diff --git a/grid-proxy/internal/explorer/server.go b/grid-proxy/internal/explorer/server.go index 26d48e96..a0555707 100644 --- a/grid-proxy/internal/explorer/server.go +++ b/grid-proxy/internal/explorer/server.go @@ -440,10 +440,6 @@ func (a *App) getNodeGpus(r *http.Request) (interface{}, mw.Response) { return nil, errorReply(err) } - if !node.HasGPU { - return nil, mw.Error(fmt.Errorf("node %d has no GPU support", node.NodeID)) - } - if node.Status == "down" || node.Status == "standby" { return nil, mw.Error(fmt.Errorf("cannot fetch GPU information from node %d with status: %s", node.NodeID, node.Status)) } diff --git a/grid-proxy/pkg/types/types.go b/grid-proxy/pkg/types/types.go index 94ab7848..3d230762 100644 --- a/grid-proxy/pkg/types/types.go +++ b/grid-proxy/pkg/types/types.go @@ -29,6 +29,7 @@ type Counters struct { Twins int64 `json:"twins"` Contracts int64 `json:"contracts"` NodesDistribution map[string]int64 `json:"nodesDistribution" gorm:"-:all"` + GPUs int64 `json:"gpus"` } // PublicConfig node public config @@ -187,7 +188,7 @@ type Node struct { RentedByTwinID uint `json:"rentedByTwinId"` SerialNumber string `json:"serialNumber"` Power NodePower `json:"power"` - HasGPU bool `json:"hasGpu"` + NumGPU int `json:"num_gpu"` ExtraFee uint64 `json:"extraFee"` } @@ -220,7 +221,7 @@ type NodeWithNestedCapacity struct { RentedByTwinID uint `json:"rentedByTwinId"` SerialNumber string `json:"serialNumber"` Power NodePower `json:"power"` - HasGPU bool `json:"hasGpu"` + NumGPU int `json:"num_gpu"` ExtraFee uint64 `json:"extraFee"` } diff --git a/grid-proxy/tests/queries/local_client.go b/grid-proxy/tests/queries/local_client.go index c1318d0a..0b0665b0 100644 --- a/grid-proxy/tests/queries/local_client.go +++ b/grid-proxy/tests/queries/local_client.go @@ -343,12 +343,20 @@ func (g *GridProxyClientimpl) Node(nodeID uint32) (res proxytypes.NodeWithNested State: node.power.State, Target: node.power.Target, }, - HasGPU: node.HasGPU, + NumGPU: getNumGPUs(node.HasGPU), ExtraFee: node.ExtraFee, } return } +// getNumGPUs should be deleted after removing hasGPU +func getNumGPUs(hasGPU bool) int { + if hasGPU { + return 1 + } + return 0 +} + func (g *GridProxyClientimpl) NodeStatus(nodeID uint32) (res proxytypes.NodeStatus, err error) { node := g.data.nodes[uint64(nodeID)] res.Status = decideNodeStatus(node.power, node.updated_at) @@ -363,6 +371,7 @@ func (g *GridProxyClientimpl) Counters(filter proxytypes.StatsFilter) (res proxy res.Contracts += int64(len(g.data.nodeContracts)) res.Contracts += int64(len(g.data.nameContracts)) distribution := map[string]int64{} + var gpus int64 for _, node := range g.data.nodes { if filter.Status == nil || (*filter.Status == STATUS_UP && isUp(node.updated_at)) { res.Nodes++ @@ -377,10 +386,14 @@ func (g *GridProxyClientimpl) Counters(filter proxytypes.StatsFilter) (res proxy res.Gateways++ } } + if node.HasGPU { + gpus++ + } } } res.Countries = int64(len(distribution)) res.NodesDistribution = distribution + res.GPUs = gpus return } diff --git a/grid-proxy/tests/queries/node_test.go b/grid-proxy/tests/queries/node_test.go index 60503dc6..a849b499 100644 --- a/grid-proxy/tests/queries/node_test.go +++ b/grid-proxy/tests/queries/node_test.go @@ -157,7 +157,7 @@ func TestNode(t *testing.T) { assert.NoError(t, err) for _, node := range nodes { - assert.Equal(t, node.HasGPU, hasGPU, "has_gpu filter did not work") + assert.Equal(t, node.NumGPU, 1, "has_gpu filter did not work") } }) }