Skip to content

Commit d943fbe

Browse files
authored
Merge pull request #478 from Concordium/fix-block-metric-future-bucket
Fix date_bin_series to not include bucket in future
2 parents f98931c + bed7026 commit d943fbe

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

backend-rust/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Database schema version: 2
2929
- Contract rejected event skips in the correct way.
3030
- Fix issue where `ContractUpdated::message` attempted to parse empty messages, resulting in parsing error messages instead of `null`.
3131
- Issue making `avgFinalizationTime` field of `Query::block_metrics` always return `null`.
32+
- Issue making `Query::block_metrics` included a bucket for a period in the future.
3233

3334
## [0.1.19] - 2025-01-30
3435

backend-rust/src/migrations/m0002-block-cumulative-fin-time-index.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ ALTER TABLE blocks ALTER COLUMN hash SET DATA TYPE VARCHAR(64);
1010
CREATE INDEX blocks_hash_gin_trgm_idx ON blocks USING gin(hash gin_trgm_ops);
1111
-- Important for quickly calculating the delegated stake to a baker pool.
1212
CREATE INDEX delegated_target_baker_id_index ON accounts(delegated_target_baker_id);
13+
-- Function for generating a table where each row is a bucket.
14+
-- Used by metrics queries.
15+
-- This is replacing the current `date_bin_series` in a backwards compatible way, fixing issue where
16+
-- a future bucket always got included.
17+
CREATE OR REPLACE FUNCTION date_bin_series(bucket_size interval, starting TIMESTAMPTZ, ending TIMESTAMPTZ)
18+
RETURNS TABLE(bucket_start TIMESTAMPTZ, bucket_end TIMESTAMPTZ) AS $$
19+
SELECT
20+
bucket_start,
21+
bucket_start + bucket_size
22+
FROM generate_series(
23+
date_bin(bucket_size, starting, TIMESTAMPTZ '2001-01-01'),
24+
date_bin(bucket_size, ending, TIMESTAMPTZ '2001-01-01'),
25+
bucket_size
26+
) as bucket_start;
27+
$$ LANGUAGE sql;

0 commit comments

Comments
 (0)