Skip to content

Commit

Permalink
Merge pull request #211 from threefoldtech/development_num_gpus
Browse files Browse the repository at this point in the history
replace has gpu with num gpu
  • Loading branch information
alichaddad authored Jun 21, 2023
2 parents 89149cd + 3a7e555 commit 0f4324a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
12 changes: 10 additions & 2 deletions grid-proxy/internal/explorer/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions grid-proxy/internal/explorer/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions grid-proxy/internal/explorer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
5 changes: 3 additions & 2 deletions grid-proxy/pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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"`
}

Expand Down
15 changes: 14 additions & 1 deletion grid-proxy/tests/queries/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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++
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion grid-proxy/tests/queries/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})
}
Expand Down

0 comments on commit 0f4324a

Please sign in to comment.