Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rentable_by filter #1204

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions grid-proxy/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions grid-proxy/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,12 @@
"name": "available_for",
"in": "query"
},
{
"type": "integer",
"description": "rented by a twin id or available to rent",
"name": "rentable_by",
"in": "query"
},
{
"type": "string",
"description": "List of farms separated by comma to fetch nodes from (e.g. '1,2,3')",
Expand Down Expand Up @@ -1610,6 +1616,9 @@
"items": {
"$ref": "#/definitions/types.Processor"
}
},
"updatedAt": {
"type": "integer"
}
}
},
Expand Down Expand Up @@ -1706,6 +1715,12 @@
"farmingPolicyId": {
"type": "integer"
},
"gpus": {
"type": "array",
"items": {
"$ref": "#/definitions/types.NodeGPU"
}
},
"gridVersion": {
"type": "integer"
},
Expand Down Expand Up @@ -1895,6 +1910,12 @@
"farmingPolicyId": {
"type": "integer"
},
"gpus": {
"type": "array",
"items": {
"$ref": "#/definitions/types.NodeGPU"
}
},
"gridVersion": {
"type": "integer"
},
Expand Down Expand Up @@ -2019,6 +2040,9 @@
"node_twin_id": {
"type": "integer"
},
"updatedAt": {
"type": "integer"
},
"upload": {
"description": "in bit/sec",
"type": "number"
Expand Down
16 changes: 16 additions & 0 deletions grid-proxy/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ definitions:
items:
$ref: '#/definitions/types.Processor'
type: array
updatedAt:
type: integer
type: object
types.Farm:
properties:
Expand Down Expand Up @@ -153,6 +155,10 @@ definitions:
type: string
farmingPolicyId:
type: integer
gpus:
items:
$ref: '#/definitions/types.NodeGPU'
type: array
gridVersion:
type: integer
healthy:
Expand Down Expand Up @@ -277,6 +283,10 @@ definitions:
type: string
farmingPolicyId:
type: integer
gpus:
items:
$ref: '#/definitions/types.NodeGPU'
type: array
gridVersion:
type: integer
healthy:
Expand Down Expand Up @@ -359,6 +369,8 @@ definitions:
type: number
node_twin_id:
type: integer
updatedAt:
type: integer
upload:
description: in bit/sec
type: number
Expand Down Expand Up @@ -1109,6 +1121,10 @@ paths:
in: query
name: available_for
type: integer
- description: rented by a twin id or available to rent
in: query
name: rentable_by
type: integer
- description: List of farms separated by comma to fetch nodes from (e.g. '1,2,3')
in: query
name: farm_ids
Expand Down
9 changes: 8 additions & 1 deletion grid-proxy/internal/explorer/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,12 @@ func (d *PostgresDatabase) GetNodes(ctx context.Context, filter types.NodeFilter
if filter.RentedBy != nil {
q = q.Where(`COALESCE(resources_cache.renter, 0) = ?`, *filter.RentedBy)
}
if filter.RentableBy != nil {
q = q.Where(`((farm.dedicated_farm = true OR resources_cache.node_contracts_count = 0)
AND resources_cache.renter is null)
OR COALESCE(resources_cache.renter, 0) = ?
`, *filter.RentableBy)
}
if filter.Rented != nil {
q = q.Where(`? = (resources_cache.renter is not null)`, *filter.Rented)
}
Expand All @@ -723,7 +729,8 @@ func (d *PostgresDatabase) GetNodes(ctx context.Context, filter types.NodeFilter
if limit.Randomize {
q = q.Order("random()")
} else {
if filter.AvailableFor != nil {
// prioritize the rented (by the twin) nodes
if filter.AvailableFor != nil || filter.RentableBy != nil {
q = q.Order("(case when resources_cache.renter is not null then 1 else 2 end)")
}

Expand Down
1 change: 1 addition & 0 deletions grid-proxy/internal/explorer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (a *App) getStats(r *http.Request) (interface{}, mw.Response) {
// @Param rented query bool false "Set to true to filter rented nodes"
// @Param rented_by query int false "rented by twin id"
// @Param available_for query int false "available for twin id"
// @Param rentable_by query int false "rented by a twin id or available to rent"
// @Param farm_ids query string false "List of farms separated by comma to fetch nodes from (e.g. '1,2,3')"
// @Param certification_type query string false "certificate type" Enums(Certified, DIY)
// @Param has_gpu query bool false "filter nodes on whether they have GPU support or not"
Expand Down
3 changes: 2 additions & 1 deletion grid-proxy/pkg/types/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ type NodeFilter struct {
OwnedBy *uint64 `schema:"owned_by,omitempty"`
Rented *bool `schema:"rented,omitempty"`
RentedBy *uint64 `schema:"rented_by,omitempty"`
AvailableFor *uint64 `schema:"available_for,omitempty"`
RentableBy *uint64 `schema:"rentable_by,omitempty"` // rented by twin or rentable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RentedBy instead of RentableBy no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rentedby is already a functional filter returns all the nodes rented by a specific twin.
rentableby is a new filter that should return all the nodes rented by a specific twin + all the nodes available to rent

Copy link
Contributor Author

@Omarabdul3ziz Omarabdul3ziz Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rentedby is already a functional filter returns all the nodes rented by a specific twin.
rentableby is a new filter that should return all the nodes rented by a specific twin + all the nodes available to rent. somehow combining rentable and rentedby

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that different from AvailableFor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

available_for returns the nodes rented by a twin id + the shared nodes. (available to deploy on)
rentable_for returns the nodes rented by a twin id + the rentable nodes. (rented or available to rent)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tb bos :D let's make it rentable_or_rented_by :D

AvailableFor *uint64 `schema:"available_for,omitempty"` // rented by twin or free
NodeID *uint64 `schema:"node_id,omitempty"`
NodeIDs []uint64 `schema:"node_ids,omitempty"`
TwinID *uint64 `schema:"twin_id,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions grid-proxy/tests/queries/mock_client/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ func (n *Node) satisfies(f types.NodeFilter, data *DBData) bool {
}

renter, ok := data.NodeRentedBy[n.NodeID]

if f.RentableBy != nil &&
((ok && renter != *f.RentableBy) ||
(!ok && !(data.Farms[n.FarmID].DedicatedFarm || len(data.NonDeletedContracts[n.NodeID]) == 0))) {
return false
}

if f.AvailableFor != nil &&
((ok && renter != *f.AvailableFor) ||
(!ok && (data.Farms[n.FarmID].DedicatedFarm || n.ExtraFee != 0))) {
Expand Down
7 changes: 7 additions & 0 deletions grid-proxy/tests/queries/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa
}
return &c
},
"RentableBy": func(agg NodesAggregate) interface{} {
c := agg.twins[rand.Intn(len(agg.twins))]
if flip(.2) && len(agg.nodeRenters) != 0 {
c = agg.nodeRenters[rand.Intn(len(agg.nodeRenters))]
}
return &c
},
"AvailableFor": func(agg NodesAggregate) interface{} {
c := agg.twins[rand.Intn(len(agg.twins))]
if flip(.1) && len(agg.nodeRenters) != 0 {
Expand Down
Loading