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 all commits
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
21 changes: 6 additions & 15 deletions grid-proxy/tests/queries/farm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,18 @@ var farmFilterRandomValueGenerator = map[string]func(agg FarmsAggregate) interfa
return &country
},
"Region": func(agg FarmsAggregate) interface{} {
c := changeCase(agg.regions[rand.Intn(len(agg.regions))])
if len(c) == 0 {
value := getRandomItemSubstring(agg.regions)
if len(value) == 0 {
return nil
}

return &c
return &value
},
"NameContains": func(agg FarmsAggregate) interface{} {
c := agg.farmNames[rand.Intn(len(agg.farmNames))]
runesList := []rune(c)
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 {
name := getRandomItemSubstring(agg.farmNames)
if len(name) == 0 {
return nil
}

return &c
return &name
},
"CertificationType": func(agg FarmsAggregate) interface{} {
return &agg.certifications[rand.Intn(len(agg.certifications))]
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
36 changes: 4 additions & 32 deletions grid-proxy/tests/queries/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,14 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa
return &country
},
"Region": func(agg NodesAggregate) interface{} {
region := changeCase(agg.regions[rand.Intn(len(agg.regions))])
region := getRandomItemSubstring(agg.regions)
if len(region) == 0 {
return nil
}

return &region
},
"CountryContains": func(agg NodesAggregate) interface{} {
c := agg.countries[rand.Intn(len(agg.countries))]
if len(c) == 0 {
return nil
}

runesList := []rune(c)
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)
c := getRandomItemSubstring(agg.countries)
if len(c) == 0 {
return nil
}
Expand All @@ -160,18 +148,10 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa
return &city
},
"CityContains": func(agg NodesAggregate) interface{} {
c := agg.cities[rand.Intn(len(agg.cities))]
c := getRandomItemSubstring(agg.cities)
if len(c) == 0 {
return nil
}

runesList := []rune(c)
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)
return &c
},
"FarmName": func(agg NodesAggregate) interface{} {
Expand All @@ -183,18 +163,10 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa
return &name
},
"FarmNameContains": func(agg NodesAggregate) interface{} {
c := agg.farmNames[rand.Intn(len(agg.farmNames))]
c := getRandomItemSubstring(agg.farmNames)
if len(c) == 0 {
return nil
}

runesList := []rune(c)
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)
return &c
},
"FarmIDs": func(agg NodesAggregate) interface{} {
Expand Down
23 changes: 23 additions & 0 deletions grid-proxy/tests/queries/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ func min(a, b uint64) uint64 {
return b
}

// getRandomItemSubstring
// - selects a random item from input slice
// - transform case the selected item
// - extracts a random substring from the selected item
func getRandomItemSubstring(items []string) string {
if len(items) == 0 {
return ""
}

item := items[rand.Intn(len(items))]
item = changeCase(item)
if len(item) == 0 {
return ""
}

runesList := []rune(item)
a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList))
if a > b {
a, b = b, a
}
return string(runesList[a : b+1])
}

func changeCase(s string) string {
if len(s) == 0 {
return s
Expand Down
34 changes: 33 additions & 1 deletion grid-proxy/tests/queries/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package test

import "testing"
import (
"strings"
"testing"
)

func TestGetRandomSliceLengthInts(t *testing.T) {
inputSlice := []int{1, 2, 3, 4, 5}
Expand All @@ -19,3 +22,32 @@ func TestGetRandomSliceLengthEmpty(t *testing.T) {
t.Errorf("expected length %d, got %d", length, len(result))
}
}

func TestGetRandomItemSubstring(t *testing.T) {
testItems := []string{"egypt", "belgium", "united states"}

for i := 0; i < 10; i++ {
result := getRandomItemSubstring(testItems)
if result == "" {
continue
}

// is result a substring of any item in testItems? check case insensitive
found := false
for _, item := range testItems {
if strings.Contains(strings.ToLower(item), strings.ToLower(result)) {
found = true
break
}
}

if !found {
t.Errorf("result '%s' is not a substring of any input item", result)
}
}

emptyResult := getRandomItemSubstring([]string{})
if emptyResult != "" {
t.Errorf("expected empty string, got '%s'", emptyResult)
}
}
Loading