From 6d8cc5ed4a4da2f52d05c5ccf6e2e4fd71da5e69 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 8 Oct 2024 10:22:24 +0800 Subject: [PATCH] fix: enhance dead cell_outputs count query (#2213) Signed-off-by: Miles Zhang --- app/models/cell_output.rb | 6 ++++++ app/services/charts/block_statistic_generator.rb | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/cell_output.rb b/app/models/cell_output.rb index 05bd0aaa8..aa71bc73d 100644 --- a/app/models/cell_output.rb +++ b/app/models/cell_output.rb @@ -292,6 +292,12 @@ def self.update_cell_types_for_cota end end end + + def self.dead_reltuples_count + sql = "SELECT reltuples FROM pg_class WHERE relname = 'cell_outputs_dead'" + result = ActiveRecord::Base.connection.execute(sql) + result.getvalue(0, 0).to_i + end end # == Schema Information diff --git a/app/services/charts/block_statistic_generator.rb b/app/services/charts/block_statistic_generator.rb index 00eaf9e46..7e06c9693 100644 --- a/app/services/charts/block_statistic_generator.rb +++ b/app/services/charts/block_statistic_generator.rb @@ -10,13 +10,13 @@ def call hash_rate = StatisticInfo.hash_rate(block_number) live_cells_count = CellOutput.live.count - dead_cells_count = CellOutput.dead.count - block_statistic = ::BlockStatistic.find_or_create_by(block_number: block_number) + dead_cells_count = CellOutput.dead_reltuples_count + block_statistic = ::BlockStatistic.find_or_create_by(block_number:) block_statistic.update(epoch_number: target_block.epoch, difficulty: target_block.difficulty, - hash_rate: hash_rate, - live_cells_count: live_cells_count, - dead_cells_count: dead_cells_count) + hash_rate:, + live_cells_count:, + dead_cells_count:) end private