Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #284 from threefoldtech/development_update_twin_sc…
Browse files Browse the repository at this point in the history
…hema

Update twin schema
  • Loading branch information
Omarabdul3ziz authored Feb 9, 2023
2 parents 2cabafb + 8ce5cac commit da02fc7
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 15 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.19

- name: Build
run: |
Expand All @@ -30,7 +30,11 @@ jobs:
uses: golangci/golangci-lint-action@v3

- name: staticcheck
uses: dominikh/[email protected]
uses: dominikh/[email protected]
with:
version: "2022.1.3"
env:
GO111MODULE: on

- name: Test version
run: ./main -v
9 changes: 8 additions & 1 deletion internal/explorer/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,21 @@ func (d *PostgresDatabase) GetTwins(filter types.TwinFilter, limit types.Limit)
Select(
"twin_id",
"account_id",
"ip",
"relay",
"public_key",
)
if filter.TwinID != nil {
q = q.Where("twin_id = ?", *filter.TwinID)
}
if filter.AccountID != nil {
q = q.Where("account_id = ?", *filter.AccountID)
}
if filter.Relay != nil {
q = q.Where("relay = ?", *filter.Relay)
}
if filter.PublicKey != nil {
q = q.Where("public_key = ?", *filter.PublicKey)
}
var count int64
if limit.Randomize || limit.RetCount {
if res := q.Count(&count); res.Error != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/explorer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func (a *App) handleTwinRequestsQueryParams(r *http.Request) (types.TwinFilter,
}
strs := map[string]**string{
"account_id": &filter.AccountID,
"relay": &filter.Relay,
"public_key": &filter.PublicKey,
}

if err := parseParams(r, ints, strs, nil, nil); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type FarmFilter struct {
type TwinFilter struct {
TwinID *uint64
AccountID *string
Relay *string
PublicKey *string
}

// ContractFilter contract filters
Expand Down Expand Up @@ -208,7 +210,8 @@ type NodeWithNestedCapacity struct {
type Twin struct {
TwinID uint `json:"twinId"`
AccountID string `json:"accountId"`
IP string `json:"ip"`
Relay string `json:"relay"`
PublicKey string `json:"publicKey"`
}

type NodeContractDetails struct {
Expand Down
6 changes: 4 additions & 2 deletions tests/queries/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func loadTwins(db *sql.DB, data *DBData) error {
COALESCE(grid_version, 0),
COALESCE(twin_id, 0),
COALESCE(account_id, ''),
COALESCE(ip, '')
COALESCE(relay, ''),
COALESCE(public_key, '')
FROM
twin;`)
if err != nil {
Expand All @@ -225,7 +226,8 @@ func loadTwins(db *sql.DB, data *DBData) error {
&twin.grid_version,
&twin.twin_id,
&twin.account_id,
&twin.ip,
&twin.relay,
&twin.public_key,
); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion tests/queries/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ func (g *GridProxyClientimpl) Twins(filter proxytypes.TwinFilter, limit proxytyp
res = append(res, proxytypes.Twin{
TwinID: uint(twin.twin_id),
AccountID: twin.account_id,
IP: twin.ip,
Relay: twin.relay,
PublicKey: twin.public_key,
})
}
}
Expand Down
31 changes: 27 additions & 4 deletions tests/queries/twin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
type TwinsAggregate struct {
twinIDs []uint64
accountIDs []string
ip []string
relays []string
publicKeys []string
twins map[uint64]twin
}

Expand Down Expand Up @@ -98,6 +99,24 @@ func randomTwinsFilter(agg *TwinsAggregate) proxytypes.TwinFilter {
f.AccountID = &c
}
}
if flip(.2) {
if f.TwinID != nil && flip(.4) {
relay := agg.twins[*f.TwinID].account_id
f.Relay = &relay
} else {
c := agg.relays[rand.Intn(len(agg.relays))]
f.Relay = &c
}
}
if flip(.2) {
if f.TwinID != nil && flip(.4) {
publicKey := agg.twins[*f.TwinID].account_id
f.PublicKey = &publicKey
} else {
c := agg.publicKeys[rand.Intn(len(agg.publicKeys))]
f.PublicKey = &c
}
}

return f
}
Expand Down Expand Up @@ -127,7 +146,8 @@ func calcTwinsAggregates(data *DBData) (res TwinsAggregate) {
for _, twin := range data.twins {
res.twinIDs = append(res.twinIDs, twin.twin_id)
res.accountIDs = append(res.accountIDs, twin.account_id)
res.ip = append(res.ip, twin.ip)
res.relays = append(res.relays, twin.relay)
res.publicKeys = append(res.publicKeys, twin.public_key)
}
res.twins = data.twins
sort.Slice(res.twinIDs, func(i, j int) bool {
Expand All @@ -136,8 +156,11 @@ func calcTwinsAggregates(data *DBData) (res TwinsAggregate) {
sort.Slice(res.accountIDs, func(i, j int) bool {
return res.accountIDs[i] < res.accountIDs[j]
})
sort.Slice(res.ip, func(i, j int) bool {
return res.ip[i] < res.ip[j]
sort.Slice(res.relays, func(i, j int) bool {
return res.relays[i] < res.relays[j]
})
sort.Slice(res.publicKeys, func(i, j int) bool {
return res.publicKeys[i] < res.publicKeys[j]
})
return
}
Expand Down
3 changes: 2 additions & 1 deletion tests/queries/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type twin struct {
grid_version uint64
twin_id uint64
account_id string
ip string
relay string
public_key string
}
type public_ip struct {
id string
Expand Down
3 changes: 2 additions & 1 deletion tools/db/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func generateTwins(db *sql.DB) error {
twin := twin{
id: fmt.Sprintf("twin-%d", i),
account_id: fmt.Sprintf("account-id-%d", i),
ip: fmt.Sprintf("account-ip-%d", i),
relay: fmt.Sprintf("relay-%d", i),
public_key: fmt.Sprintf("public-key-%d", i),
twin_id: i,
grid_version: 3,
}
Expand Down
3 changes: 2 additions & 1 deletion tools/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ CREATE TABLE public.twin (
grid_version integer NOT NULL,
twin_id integer NOT NULL,
account_id text NOT NULL,
ip text NOT NULL
relay text,
public_key text
);


Expand Down
3 changes: 2 additions & 1 deletion tools/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type twin struct {
grid_version uint64
twin_id uint64
account_id string
ip string
relay string
public_key string
}
type public_ip struct {
id string
Expand Down

0 comments on commit da02fc7

Please sign in to comment.