Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: division by zero in stats #324

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading