Skip to content

Commit

Permalink
Fix: division by zero in stats
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Dec 17, 2024
1 parent c2dbe11 commit 4ab05bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/storage/postgres/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func (s Stats) Change24hBlockStats(ctx context.Context) (response storage.Change
With("s", second).
TableExpr("f, s").
ColumnExpr(`
(f.tx_count - s.tx_count)/s.tx_count as tx_count_24h,
(f.bytes_in_block - s.bytes_in_block)/s.bytes_in_block as bytes_in_block_24h,
(f.blobs_size - s.blobs_size)/s.blobs_size as blobs_size_24h,
(f.fee - s.fee)/s.fee as fee_24h
case when s.tx_count > 0 then (f.tx_count - s.tx_count)/s.tx_count when f.tx_count > 0 then 1 else 0 end as tx_count_24h,
case when s.bytes_in_block > 0 then (f.bytes_in_block - s.bytes_in_block)/s.bytes_in_block when f.bytes_in_block > 0 then 1 else 0 end as bytes_in_block_24h,
case when s.blobs_size > 0 then (f.blobs_size - s.blobs_size)/s.blobs_size when f.blobs_size > 0 then 1 else 0 end as blobs_size_24h,
case when s.fee > 0 then (f.fee - s.fee)/s.fee when f.fee > 0 then 1 else 0 end as fee_24h
`).
Scan(ctx, &response)
return
Expand Down
12 changes: 12 additions & 0 deletions internal/storage/postgres/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ func (s *StatsTestSuite) TestMessagesCount24h() {
s.Require().Len(items, 0)
}

func (s *StatsTestSuite) TestChange24hBlockStats() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

stats, err := s.storage.Stats.Change24hBlockStats(ctx)
s.Require().NoError(err)
s.Require().Equal(stats.BlobsSize, 0.0)
s.Require().Equal(stats.BytesInBlock, 0.0)
s.Require().Equal(stats.Fee, 0.0)
s.Require().Equal(stats.TxCount, 0.0)
}

func TestSuiteStats_Run(t *testing.T) {
suite.Run(t, new(StatsTestSuite))
}

0 comments on commit 4ab05bf

Please sign in to comment.