Skip to content

Commit

Permalink
Fix: get contracts by network
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Dec 29, 2021
1 parent 76c5acc commit 8ddc628
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cmd/metadata/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func NewIndexer(ctx context.Context, network string, indexerConfig *config.Index
thumbnail.WithTimeout(settings.Thumbnail.Timeout),
)
}
indexer.contracts = service.NewContractService(db, indexer.resolveContractMetadata, int64(settings.MaxRetryCountOnError))
indexer.tokens = service.NewTokenService(db, indexer.resolveTokenMetadata, int64(settings.MaxRetryCountOnError))
indexer.contracts = service.NewContractService(db, indexer.resolveContractMetadata, network, int64(settings.MaxRetryCountOnError))
indexer.tokens = service.NewTokenService(db, indexer.resolveTokenMetadata, network, int64(settings.MaxRetryCountOnError))

return indexer, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/metadata/models/contract_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cm *ContractMetadata) BeforeUpdate(ctx context.Context) (context.Context,

// ContractRepository -
type ContractRepository interface {
GetContractMetadata(status Status, limit, offset, retryCount int) ([]ContractMetadata, error)
GetContractMetadata(network string, status Status, limit, offset, retryCount int) ([]ContractMetadata, error)
UpdateContractMetadata(ctx context.Context, metadata []*ContractMetadata) error
SaveContractMetadata(ctx context.Context, metadata []*ContractMetadata) error
LastContractUpdateID() (int64, error)
Expand Down
8 changes: 4 additions & 4 deletions cmd/metadata/models/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error {
}

// GetContractMetadata -
func (db *RelativeDatabase) GetContractMetadata(status Status, limit, offset, retryCount int) (all []ContractMetadata, err error) {
query := db.DB().Model(&all).Where("status = ?", status)
func (db *RelativeDatabase) GetContractMetadata(network string, status Status, limit, offset, retryCount int) (all []ContractMetadata, err error) {
query := db.DB().Model(&all).Where("status = ?", status).Where("network = ?", network)
if limit > 0 {
query.Limit(limit)
}
Expand Down Expand Up @@ -117,8 +117,8 @@ func (db *RelativeDatabase) LastContractUpdateID() (updateID int64, err error) {
}

// GetTokenMetadata -
func (db *RelativeDatabase) GetTokenMetadata(status Status, limit, offset, retryCount int) (all []TokenMetadata, err error) {
query := db.DB().Model(&all).Where("status = ?", status)
func (db *RelativeDatabase) GetTokenMetadata(network string, status Status, limit, offset, retryCount int) (all []TokenMetadata, err error) {
query := db.DB().Model(&all).Where("status = ?", status).Where("network = ?", network)
if limit > 0 {
query.Limit(limit)
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/metadata/models/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func (e *Elastic) bulk(buf *bytes.Buffer) error {
}

// GetContractMetadata -
func (e *Elastic) GetContractMetadata(status Status, limit, offset, retryCount int) ([]ContractMetadata, error) {
// TODO: filter network
func (e *Elastic) GetContractMetadata(network string, status Status, limit, offset, retryCount int) ([]ContractMetadata, error) {
hits, err := e.search(
fmt.Sprintf(`{"query":{"term":{"status": %d}}}`, status),
e.Search.WithIndex(ContractMetadata{}.TableName()),
Expand Down Expand Up @@ -228,7 +229,8 @@ func (e *Elastic) LastContractUpdateID() (value int64, err error) {
}

// GetContractMetadata -
func (e *Elastic) GetTokenMetadata(status Status, limit, offset, retryCount int) ([]TokenMetadata, error) {
// TODO: filter network
func (e *Elastic) GetTokenMetadata(network string, status Status, limit, offset, retryCount int) ([]TokenMetadata, error) {
hits, err := e.search(
fmt.Sprintf(`{"query":{"term":{"status": %d}}}`, status),
e.Search.WithIndex(TokenMetadata{}.TableName()),
Expand Down
2 changes: 1 addition & 1 deletion cmd/metadata/models/token_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (tm *TokenMetadata) BeforeUpdate(ctx context.Context) (context.Context, err

// TokenRepository -
type TokenRepository interface {
GetTokenMetadata(status Status, limit, offset, retryCount int) ([]TokenMetadata, error)
GetTokenMetadata(network string, status Status, limit, offset, retryCount int) ([]TokenMetadata, error)
SetImageProcessed(token TokenMetadata) error
GetUnprocessedImage(from uint64, limit int) ([]TokenMetadata, error)
UpdateTokenMetadata(ctx context.Context, metadata []*TokenMetadata) error
Expand Down
6 changes: 4 additions & 2 deletions cmd/metadata/service/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// ContractService -
type ContractService struct {
network string
maxRetryCount int64
db models.Database
handler func(ctx context.Context, contract *models.ContractMetadata) error
Expand All @@ -21,14 +22,15 @@ type ContractService struct {
}

// NewContractService -
func NewContractService(db models.Database, handler func(context.Context, *models.ContractMetadata) error, maxRetryCount int64) *ContractService {
func NewContractService(db models.Database, handler func(context.Context, *models.ContractMetadata) error, network string, maxRetryCount int64) *ContractService {
return &ContractService{
maxRetryCount: maxRetryCount,
db: db,
handler: handler,
tasks: make(chan *models.ContractMetadata, 1024*128),
result: make(chan *models.ContractMetadata, 15),
workers: make(chan struct{}, 10),
network: network,
}
}

Expand All @@ -43,7 +45,7 @@ func (s *ContractService) Start(ctx context.Context) {
var offset int
var end bool
for !end {
contracts, err := s.db.GetContractMetadata(models.StatusNew, 100, offset, int(s.maxRetryCount))
contracts, err := s.db.GetContractMetadata(s.network, models.StatusNew, 100, offset, int(s.maxRetryCount))
if err != nil {
log.Err(err).Msg("GetContractMetadata")
continue
Expand Down
6 changes: 4 additions & 2 deletions cmd/metadata/service/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// TokenService -
type TokenService struct {
network string
maxRetryCount int64
repo models.TokenRepository
handler func(ctx context.Context, token *models.TokenMetadata) error
Expand All @@ -21,9 +22,10 @@ type TokenService struct {
}

// NewContractService -
func NewTokenService(repo models.TokenRepository, handler func(context.Context, *models.TokenMetadata) error, maxRetryCount int64) *TokenService {
func NewTokenService(repo models.TokenRepository, handler func(context.Context, *models.TokenMetadata) error, network string, maxRetryCount int64) *TokenService {
return &TokenService{
maxRetryCount: maxRetryCount,
network: network,
repo: repo,
handler: handler,
tasks: make(chan *models.TokenMetadata, 1024*128),
Expand All @@ -43,7 +45,7 @@ func (s *TokenService) Start(ctx context.Context) {
var offset int
var end bool
for !end {
tokens, err := s.repo.GetTokenMetadata(models.StatusNew, 100, offset, int(s.maxRetryCount))
tokens, err := s.repo.GetTokenMetadata(s.network, models.StatusNew, 100, offset, int(s.maxRetryCount))
if err != nil {
log.Err(err).Msg("GetTokenMetadata")
continue
Expand Down

0 comments on commit 8ddc628

Please sign in to comment.