Skip to content

Commit

Permalink
fix query
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Sep 19, 2024
1 parent ecf09bb commit fd0ffaf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SELECT 'Drop update_block_latency procedure' AS comment;

DROP PROCEDURE IF EXISTS update_block_latency();
x
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,20 @@ class BlockProposerRecord(id: EntityID<Int>) : IntEntity(id) {

fun findAvgBlockCreation(limit: Int): BigDecimal = transaction {
val sqlQuery = """
WITH limited_blocks AS (
SELECT block_height, block_timestamp
FROM BlockProposerTable
WHERE block_timestamp IS NOT NULL
ORDER BY block_height DESC
LIMIT $limit
)
SELECT AVG(EXTRACT(EPOCH FROM (block_timestamp - LAG(block_timestamp)
OVER (ORDER BY block_height)))::numeric) AS avg_block_creation_time
WITH limited_blocks AS (
SELECT block_height, block_timestamp
FROM block_proposer
WHERE block_timestamp IS NOT NULL
ORDER BY block_height DESC
LIMIT $limit
)
SELECT AVG(diff_in_seconds) AS avg_block_creation_time
FROM (
SELECT EXTRACT(EPOCH FROM (block_timestamp - LAG(block_timestamp)
OVER (ORDER BY block_height))) AS diff_in_seconds
FROM limited_blocks
WHERE block_timestamp IS NOT NULL;
) AS time_differences
WHERE diff_in_seconds IS NOT NULL;
"""
exec(sqlQuery) { resultSet ->
if (resultSet.next()) {
Expand Down

0 comments on commit fd0ffaf

Please sign in to comment.