Skip to content

refactor region filtering #1352

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

Merged
merged 2 commits into from
Apr 15, 2025
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
4 changes: 2 additions & 2 deletions grid-proxy/internal/explorer/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func (d *PostgresDatabase) GetFarms(ctx context.Context, filter types.FarmFilter
}

if filter.Region != nil {
nodeQuery = nodeQuery.Where("LOWER(node_location.continent) = LOWER(?)", *filter.Region)
nodeQuery = nodeQuery.Where("node_location.continent ILIKE '%' || ? || '%'", *filter.Region)
}

if len(filter.NodeStatus) != 0 {
Expand Down Expand Up @@ -681,7 +681,7 @@ func (d *PostgresDatabase) GetNodes(ctx context.Context, filter types.NodeFilter
q = q.Where("node.city ILIKE '%' || ? || '%'", *filter.CityContains)
}
if filter.Region != nil {
q = q.Where("LOWER(node_location.continent) = LOWER(?)", *filter.Region)
q = q.Where("node_location.continent ILIKE '%' || ? || '%'", *filter.Region)
}
if filter.NodeID != nil {
q = q.Where("node.node_id = ?", *filter.NodeID)
Expand Down
10 changes: 10 additions & 0 deletions grid-proxy/tests/queries/farm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ var farmFilterRandomValueGenerator = map[string]func(agg FarmsAggregate) interfa
return nil
}

runesList := []rune(c)
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this block of code do?

a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList))
if a > b {
a, b = b, a
}
runesList = runesList[a : b+1]
c = string(runesList)
if len(c) == 0 {
return nil
}
return &c
},
"NameContains": func(agg FarmsAggregate) interface{} {
Expand Down
2 changes: 1 addition & 1 deletion grid-proxy/tests/queries/mock_client/farms.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (f *Farm) satisfyFarmNodesFilter(data *DBData, filter types.FarmFilter) boo
continue
}

if filter.Region != nil && !strings.EqualFold(*filter.Region, data.Regions[node.Country]) {
if filter.Region != nil && !stringMatch(data.Regions[node.Country], *filter.Region) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion grid-proxy/tests/queries/mock_client/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (n *Node) satisfies(f types.NodeFilter, data *DBData) bool {
return false
}

if f.Region != nil && !strings.EqualFold(*f.Region, data.Regions[n.Country]) {
if f.Region != nil && !stringMatch(data.Regions[n.Country], *f.Region) {
return false
}

Expand Down
10 changes: 10 additions & 0 deletions grid-proxy/tests/queries/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa
return nil
}

runesList := []rune(region)
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like that's also duplicated from the code above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it is duplicated same as CityContains and CountryContains filters for both nodes/farms.

basically this snippet tries to get a substring of a random city/country/region from the aggregated values from the database, to pass it the filter and make sure it can search with any part of the value

a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList))
if a > b {
a, b = b, a
}
runesList = runesList[a : b+1]
region = string(runesList)
if len(region) == 0 {
return nil
}
return &region
},
"CountryContains": func(agg NodesAggregate) interface{} {
Expand Down
Loading