Skip to content

Commit

Permalink
Fix: big map stats query
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jul 16, 2023
1 parent e8eccd0 commit 6f1830b
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions internal/postgres/bigmapdiff/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,37 +158,31 @@ func (storage *Storage) Get(ctx bigmapdiff.GetContext) ([]bigmapdiff.Bucket, err

// GetStats -
func (storage *Storage) GetStats(ptr int64) (stats bigmapdiff.Stats, err error) {
totalQuery := storage.DB.Model().Table(models.DocBigMapState).
ColumnExpr("count(contract) as count, contract, 'total' as name").
total, err := storage.DB.Model((*bigmapdiff.BigMapState)(nil)).
Where("ptr = ?", ptr).
Group("contract")
Count()
if err != nil {
return stats, err
}

activeQuery := storage.DB.Model().Table(models.DocBigMapState).
ColumnExpr("count(contract) as count, contract, 'active' as name").
active, err := storage.DB.Model((*bigmapdiff.BigMapState)(nil)).
Where("ptr = ?", ptr).
Where("removed = false").
Group("contract")

type row struct {
Count int64
Contract string
Name string
Count()
if err != nil {
return stats, err
}
var rows []row

if _, err = storage.DB.Model().Query(&rows, "(?) union all (?)", totalQuery, activeQuery); err != nil {
if err = storage.DB.Model((*bigmapdiff.BigMapState)(nil)).
Column("contract").
Where("ptr = ?", ptr).
Limit(1).
Select(&stats.Contract); err != nil {
return
}

for i := range rows {
switch rows[i].Name {
case "active":
stats.Active = rows[i].Count
case "total":
stats.Total = rows[i].Count
}
stats.Contract = rows[i].Contract
}
stats.Active = int64(active)
stats.Total = int64(total)

return
}
Expand Down

0 comments on commit 6f1830b

Please sign in to comment.