-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- remove the region column and triggers from the cache table - add the indexer with the schema changes and generator - make the removeDuplicates function generic to be used with all indexers
- Loading branch information
1 parent
90d3591
commit 0c0246a
Showing
17 changed files
with
236 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package indexer | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/internal/explorer/db" | ||
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types" | ||
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer" | ||
"github.com/threefoldtech/zos/pkg/geoip" | ||
) | ||
|
||
const locationCmd = "zos.location.get" | ||
|
||
var _ Work[types.NodeLocation] = (*LocationWork)(nil) | ||
|
||
type LocationWork struct { | ||
finders map[string]time.Duration | ||
} | ||
|
||
func NewLocationWork(interval uint) *LocationWork { | ||
return &LocationWork{ | ||
finders: map[string]time.Duration{ | ||
"up": time.Duration(interval) * time.Minute, | ||
}, | ||
} | ||
} | ||
|
||
func (w *LocationWork) Finders() map[string]time.Duration { | ||
return w.finders | ||
} | ||
|
||
func (w *LocationWork) Get(ctx context.Context, rmb *peer.RpcClient, id uint32) ([]types.NodeLocation, error) { | ||
var loc geoip.Location | ||
if err := callNode(ctx, rmb, locationCmd, nil, id, &loc); err != nil { | ||
return []types.NodeLocation{}, nil | ||
} | ||
|
||
return []types.NodeLocation{ | ||
{ | ||
Country: loc.Country, | ||
Continent: loc.Continent, | ||
UpdatedAt: time.Now().Unix(), | ||
}, | ||
}, nil | ||
} | ||
|
||
func (w *LocationWork) Upsert(ctx context.Context, db db.Database, batch []types.NodeLocation) error { | ||
unique := removeDuplicates(batch, func(n types.NodeLocation) string { | ||
return n.Country | ||
}) | ||
|
||
return db.UpsertNodeLocation(ctx, unique) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package indexer | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types" | ||
) | ||
|
||
func TestRemoveDuplicates(t *testing.T) { | ||
t.Run("remove duplicate countries", func(t *testing.T) { | ||
locations := []types.NodeLocation{ | ||
{Country: "Egypt", Continent: "Africa"}, | ||
{Country: "Egypt", Continent: "Africa"}, | ||
{Country: "Belgium", Continent: "Europe"}, | ||
} | ||
|
||
uniqueLocations := []types.NodeLocation{ | ||
{Country: "Egypt", Continent: "Africa"}, | ||
{Country: "Belgium", Continent: "Europe"}, | ||
} | ||
|
||
gotLocations := removeDuplicates(locations, func(n types.NodeLocation) string { | ||
return n.Country | ||
}) | ||
|
||
if !reflect.DeepEqual(uniqueLocations, gotLocations) { | ||
t.Errorf("expected %v, but got %v", uniqueLocations, gotLocations) | ||
} | ||
}) | ||
|
||
t.Run("remove duplicate health reports", func(t *testing.T) { | ||
healthReports := []types.HealthReport{ | ||
{NodeTwinId: 1, Healthy: true}, | ||
{NodeTwinId: 1, Healthy: true}, | ||
{NodeTwinId: 2, Healthy: true}, | ||
} | ||
|
||
uniqueReports := []types.HealthReport{ | ||
{NodeTwinId: 1, Healthy: true}, | ||
{NodeTwinId: 2, Healthy: true}, | ||
} | ||
|
||
gotReports := removeDuplicates(healthReports, func(h types.HealthReport) uint32 { | ||
return h.NodeTwinId | ||
}) | ||
|
||
if !reflect.DeepEqual(gotReports, uniqueReports) { | ||
t.Errorf("expected %v, but got %v", uniqueReports, gotReports) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.