Skip to content

Commit

Permalink
Fix: same count
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 7, 2022
1 parent fedaf50 commit af5c2cd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 84 deletions.
50 changes: 4 additions & 46 deletions cmd/api/handlers/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func GetSameContracts() gin.HandlerFunc {
return
}

count, err := ctx.Domains.SameCount(contract)
count, err := ctx.Domains.SameCount(contract, ctx.Config.API.Networks...)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand All @@ -97,7 +97,7 @@ func GetSameContracts() gin.HandlerFunc {
Contracts: make([]ContractWithStats, 0),
}

same, err := ctx.Domains.Same(req.Network, contract, int(page.Size), int(page.Offset))
same, err := ctx.Domains.Same(req.Network, contract, int(page.Size), int(page.Offset), ctx.Config.API.Networks...)
if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down Expand Up @@ -131,60 +131,18 @@ func contractWithStatsPostprocessing(ctxs config.Contexts, ctx *config.Context,
if err != nil {
return ContractWithStats{}, err
}
res := ContractWithStats{c, -1, 0}
res := ContractWithStats{c, 0, 0}

eventsCount, err := ctx.Operations.EventsCount(contractModel.AccountID)
if err != nil {
return res, err
}
res.EventsCount = eventsCount

stats, err := ctx.Domains.SameCount(contractModel)
stats, err := ctx.Domains.SameCount(contractModel, ctx.Config.API.Networks...)
if err != nil {
return res, err
}
res.SameCount += int64(stats)

for _, cur := range ctxs {
if cur.Network == ctx.Network {
continue
}

var buf contract.Contract
switch {
case contractModel.AlphaID > 0:
script, err := cur.Scripts.ByHash(contractModel.Alpha.Hash)
if err != nil {
if cur.Storage.IsRecordNotFound(err) {
continue
}
return res, err
}
buf.AlphaID = script.ID
case contractModel.BabylonID > 0:
script, err := cur.Scripts.ByHash(contractModel.Babylon.Hash)
if err != nil {
if cur.Storage.IsRecordNotFound(err) {
continue
}
return res, err
}
buf.BabylonID = script.ID
case contractModel.JakartaID > 0:
script, err := cur.Scripts.ByHash(contractModel.Jakarta.Hash)
if err != nil {
if cur.Storage.IsRecordNotFound(err) {
continue
}
return res, err
}
buf.JakartaID = script.ID
}
stats, err := cur.Domains.SameCount(buf)
if err != nil {
return res, err
}
res.SameCount += int64(stats)
}
return res, nil
}
4 changes: 2 additions & 2 deletions internal/models/domains/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
type Repository interface {
BigMapDiffs(lastID, size int64) ([]BigMapDiff, error)

Same(network string, c contract.Contract, limit, offset int) ([]Same, error)
SameCount(c contract.Contract) (int, error)
Same(network string, c contract.Contract, limit, offset int, availiableNetworks ...string) ([]Same, error)
SameCount(c contract.Contract, availiableNetworks ...string) (int, error)
}
34 changes: 22 additions & 12 deletions internal/models/mock/domains/mock.go

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

54 changes: 30 additions & 24 deletions internal/postgres/domains/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/baking-bad/bcdhub/internal/models/bigmapdiff"
"github.com/baking-bad/bcdhub/internal/models/contract"
"github.com/baking-bad/bcdhub/internal/models/domains"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/baking-bad/bcdhub/internal/postgres/core"
"github.com/go-pg/pg/v10"
)
Expand Down Expand Up @@ -47,21 +48,14 @@ var sameTemplate = template.Must(
template.New("same").Parse(
`select * from (
select * from (
select 'mainnet' as network, c1.*, accounts.address as account__address from mainnet.contracts as c1
join mainnet.accounts on c1.account_id = accounts.id
where (c1.alpha_id = {{.AlphaID}} or c1.babylon_id = {{.BabylonID}} or c1.jakarta_id = {{.JakartaID}})
{{- range $index, $network := .Networks }}
{{- if (gt $index 0) }}
union all
select 'ghostnet' as network, c2.*, accounts.address as account__address from ghostnet.contracts as c2
join ghostnet.accounts on c2.account_id = accounts.id
where (c2.alpha_id = {{.AlphaID}} or c2.babylon_id = {{.BabylonID}} or c2.jakarta_id = {{.JakartaID}})
union all
select 'jakartanet' as network, c3.*, accounts.address as account__address from jakartanet.contracts as c3
join jakartanet.accounts on c3.account_id = accounts.id
where (c3.alpha_id = {{.AlphaID}} or c3.babylon_id = {{.BabylonID}} or c3.jakarta_id = {{.JakartaID}})
union all
select 'kathmandunet' as network, c3.*, accounts.address as account__address from kathmandunet.contracts as c3
join kathmandunet.accounts on c3.account_id = accounts.id
where (c3.alpha_id = {{.AlphaID}} or c3.babylon_id = {{.BabylonID}} or c3.jakarta_id = {{.JakartaID}})
{{- end}}
select '{{ $network }}' as network, contracts.*, accounts.address as account__address from {{ $network }}.contracts
join {{ $network }}.accounts on contracts.account_id = accounts.id
where (contracts.alpha_id = {{$.AlphaID}} or contracts.babylon_id = {{$.BabylonID}} or contracts.jakarta_id = {{$.JakartaID}})
{{end}}
) as q
where NOT (network = '{{.network}}' and id = {{.ID}})
) as same
Expand All @@ -71,7 +65,7 @@ var sameTemplate = template.Must(
)

// Same -
func (storage *Storage) Same(network string, c contract.Contract, limit, offset int) ([]domains.Same, error) {
func (storage *Storage) Same(network string, c contract.Contract, limit, offset int, availiableNetworks ...string) ([]domains.Same, error) {
if limit < 1 || limit > 10 {
limit = 10
}
Expand All @@ -80,6 +74,10 @@ func (storage *Storage) Same(network string, c contract.Contract, limit, offset
offset = 0
}

if len(availiableNetworks) == 0 {
availiableNetworks = []string{types.Mainnet.String()}
}

data := map[string]any{
"ID": c.ID,
"AlphaID": c.AlphaID,
Expand All @@ -88,6 +86,7 @@ func (storage *Storage) Same(network string, c contract.Contract, limit, offset
"limit": limit,
"offset": offset,
"network": network,
"Networks": availiableNetworks,
}

var buffer strings.Builder
Expand All @@ -103,22 +102,29 @@ func (storage *Storage) Same(network string, c contract.Contract, limit, offset
var sameCountTemplate = template.Must(
template.New("sameCount").Parse(
`select sum(c) from (
select count(*) as c from mainnet.contracts as c1
where (c1.alpha_id = {{.AlphaID}} or c1.babylon_id = {{.BabylonID}} or c1.jakarta_id = {{.JakartaID}})
union all
select count(*) as c from ghostnet.contracts as c3
where (c3.alpha_id = {{.AlphaID}} or c3.babylon_id = {{.BabylonID}} or c3.jakarta_id = {{.JakartaID}})
{{- range $index, $network := .Networks }}
{{- if (gt $index 0) }}
union all
select count(*) as c from jakartanet.contracts as c2
where (c2.alpha_id = {{.AlphaID}} or c2.babylon_id = {{.BabylonID}} or c2.jakarta_id = {{.JakartaID}})
{{- end}}
select count(*) as c from {{$network}}.contracts
where (contracts.alpha_id = {{$.AlphaID}} or contracts.babylon_id = {{$.BabylonID}} or contracts.jakarta_id = {{$.JakartaID}})
{{end}}
) as same`,
),
)

// SameCount -
func (storage *Storage) SameCount(c contract.Contract) (int, error) {
func (storage *Storage) SameCount(c contract.Contract, availiableNetworks ...string) (int, error) {
data := map[string]any{
"ID": c.ID,
"AlphaID": c.AlphaID,
"BabylonID": c.BabylonID,
"JakartaID": c.JakartaID,
"Networks": availiableNetworks,
}

var buffer strings.Builder
if err := sameCountTemplate.Execute(&buffer, c); err != nil {
if err := sameCountTemplate.Execute(&buffer, data); err != nil {
return 0, err
}

Expand Down

0 comments on commit af5c2cd

Please sign in to comment.