From f005630284d5a5f3554be5fb9dcfe0bad239c6c0 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Thu, 17 Jul 2025 09:39:07 +0900 Subject: [PATCH] fix: query blocks/tx total_count timeout (#2639) Signed-off-by: Miles Zhang --- app/controllers/api/v1/blocks_controller.rb | 3 ++- app/controllers/api/v1/ckb_transactions_controller.rb | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index dffd03ad6..02fc25145 100644 --- a/app/controllers/api/v1/blocks_controller.rb +++ b/app/controllers/api/v1/blocks_controller.rb @@ -31,11 +31,12 @@ def index head :not_found and return unless order_by.in? %w[number reward timestamp ckb_transactions_count] blocks = blocks.order(order_by => asc_or_desc).order("number DESC").page(@page).per(@page_size) + total_count = TableRecordCount.find_by(table_name: "blocks")&.count json = Rails.cache.realize(blocks.cache_key, version: blocks.cache_version, race_condition_ttl: 3.seconds) do options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: blocks, page: @page, - page_size: @page_size).call + page_size: @page_size, total_count:).call BlockListSerializer.new(blocks, options).serialized_json end end diff --git a/app/controllers/api/v1/ckb_transactions_controller.rb b/app/controllers/api/v1/ckb_transactions_controller.rb index eb0e0aff1..9cd627389 100644 --- a/app/controllers/api/v1/ckb_transactions_controller.rb +++ b/app/controllers/api/v1/ckb_transactions_controller.rb @@ -41,7 +41,7 @@ def index ckb_transactions = ckb_transactions.order(order_by => asc_or_desc). page(@page).per(@page_size).fast_page - + total_count = TableRecordCount.find_by(table_name: "ckb_transactions")&.count json = Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) do @@ -50,6 +50,7 @@ def index records: ckb_transactions, page: @page, page_size: @page_size, + total_count:, ).call CkbTransactionListSerializer.new(ckb_transactions, options).serialized_json @@ -66,7 +67,6 @@ def query @address = Address.cached_find(params[:address]) end - total_count = 0 ckb_transactions = if @address @tx_ids = @@ -80,8 +80,8 @@ def query total_count = @tx_ids.total_count CkbTransaction.where(id: @tx_ids.map(&:ckb_transaction_id)).order(block_number: :desc, tx_index: :desc) else - txs = CkbTransaction.recent.normal.page(@page).per(@page_size).fast_page - total_count = txs.total_count + total_count = TableRecordCount.find_by(table_name: "ckb_transactions")&.count + CkbTransaction.recent.normal.page(@page).per(@page_size).fast_page end ckb_transactions = ckb_transactions.select(:id, :tx_hash, :block_id, :tags, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at)