From 1e120e39eef04f5c961c6ff334ed247b07c43b50 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 17 Dec 2024 18:34:41 +0900 Subject: [PATCH 1/2] fix: query default limit set 5000 Signed-off-by: Miles Zhang --- app/controllers/api/v2/scripts_controller.rb | 4 ++-- app/interactions/addresses/ckb_transactions.rb | 4 ++-- app/lib/fast_jsonapi/pagination_meta_generator.rb | 12 ++++-------- config/settings.mainnet.yml | 1 + config/settings.testnet.yml | 1 + 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v2/scripts_controller.rb b/app/controllers/api/v2/scripts_controller.rb index 3332a8fe0..9848fa024 100644 --- a/app/controllers/api/v2/scripts_controller.rb +++ b/app/controllers/api/v2/scripts_controller.rb @@ -21,7 +21,7 @@ def ckb_transactions base_query = CkbTransaction.joins(:cell_dependencies). where(cell_dependencies: { contract_cell_id: contract_cell_ids }). order("cell_dependencies.block_number DESC, cell_dependencies.tx_index DESC"). - limit(10000) + limit(Settings.query_default_limit) @ckb_transactions = CkbTransaction.from("(#{base_query.to_sql}) AS ckb_transactions"). order("block_number DESC, tx_index DESC"). page(@page). @@ -43,7 +43,7 @@ def referring_cells scope = Contract.referring_cells_query(@contracts). order("block_timestamp DESC, cell_index DESC"). - limit(10000) + limit(Settings.query_default_limit) if params[:args].present? type_script = TypeScript.find_by(args: params[:args]) scope = scope.or(CellOutput.where(type_script_id: type_script.id)) diff --git a/app/interactions/addresses/ckb_transactions.rb b/app/interactions/addresses/ckb_transactions.rb index cb38536c6..c47373737 100644 --- a/app/interactions/addresses/ckb_transactions.rb +++ b/app/interactions/addresses/ckb_transactions.rb @@ -13,7 +13,7 @@ def execute raise AddressNotFoundError if address.is_a?(NullAddress) address_id = address.map(&:id) - account_books = AccountBook.where(address_id:).order("ckb_transaction_id desc").select(:ckb_transaction_id).distinct.limit(5000) + account_books = AccountBook.where(address_id:).order("ckb_transaction_id desc").select(:ckb_transaction_id).distinct.limit(Settings.query_default_limit) records = CkbTransaction.where(tx_status: :committed, id: account_books.map(&:ckb_transaction_id)).order(transactions_ordering).page(page).per(page_size) options = paginate_options(records, address_id) options.merge!(params: { previews: true, address: }) @@ -35,7 +35,7 @@ def transactions_ordering def paginate_options(records, address_id) total_count = AccountBook.where(address_id:).distinct.count FastJsonapi::PaginationMetaGenerator.new( - request:, records:, page:, page_size:, total_count:, + request:, records:, page:, page_size:, total_pages: records.total_pages, total_count:, ).call end diff --git a/app/lib/fast_jsonapi/pagination_meta_generator.rb b/app/lib/fast_jsonapi/pagination_meta_generator.rb index 09aad67f8..9da998ff1 100644 --- a/app/lib/fast_jsonapi/pagination_meta_generator.rb +++ b/app/lib/fast_jsonapi/pagination_meta_generator.rb @@ -3,18 +3,18 @@ class PaginationMetaGenerator DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 20 - def initialize(request:, records:, page:, page_size:, records_counter: nil, total_count: nil) + def initialize(request:, records:, page:, page_size:, total_pages: nil, records_counter: nil, total_count: nil) @url = request.base_url + request.path + query_string(request.query_parameters) @page = page.to_i @page_size = limit_page_size(records, page_size.to_i) @records = records @records_counter = records_counter || records @total_count = total_count || @records_counter.total_count.to_i - @total_pages = total_pages - @hash = { links: {}, meta: { total: @total_count, page_size: @page_size } } + @total_pages = total_pages || calculated_total_pages + @hash = { links: {}, meta: { total: @total_count, page_size: @page_size, total_pages: @total_pages } } end - def total_pages + def calculated_total_pages (total_count / @page_size).ceil end @@ -31,10 +31,6 @@ def current_page records.current_page end - def last_page? - current_page == total_pages - end - def next_page current_page + 1 unless last_page? || out_of_range? end diff --git a/config/settings.mainnet.yml b/config/settings.mainnet.yml index bbc2f5c80..04e04b657 100644 --- a/config/settings.mainnet.yml +++ b/config/settings.mainnet.yml @@ -83,6 +83,7 @@ type_id_code_hash: "0x0000000000000000000000000000000000000000000000000054595045 homepage_transactions_records_count: 15 homepage_block_records_count: 15 proposal_window: 10 +query_default_limit: 5000 # rgbpp code hash rgbpp_code_hash: diff --git a/config/settings.testnet.yml b/config/settings.testnet.yml index 790e071cd..707f2faac 100644 --- a/config/settings.testnet.yml +++ b/config/settings.testnet.yml @@ -84,6 +84,7 @@ type_id_code_hash: "0x0000000000000000000000000000000000000000000000000054595045 homepage_transactions_records_count: 15 homepage_block_records_count: 15 proposal_window: 10 +query_default_limit: 5000 # rgbpp code hash rgbpp_code_hash: From d65a837bd5c97934b43c7b8c6ddb4d8c358b16bf Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 17 Dec 2024 22:04:39 +0900 Subject: [PATCH 2/2] test: fix test Signed-off-by: Miles Zhang --- app/lib/fast_jsonapi/pagination_meta_generator.rb | 2 +- test/controllers/api/v1/address_live_cells_controller_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/lib/fast_jsonapi/pagination_meta_generator.rb b/app/lib/fast_jsonapi/pagination_meta_generator.rb index 9da998ff1..0104f61e7 100644 --- a/app/lib/fast_jsonapi/pagination_meta_generator.rb +++ b/app/lib/fast_jsonapi/pagination_meta_generator.rb @@ -15,7 +15,7 @@ def initialize(request:, records:, page:, page_size:, total_pages: nil, records_ end def calculated_total_pages - (total_count / @page_size).ceil + (total_count.to_f / @page_size).ceil end def call diff --git a/test/controllers/api/v1/address_live_cells_controller_test.rb b/test/controllers/api/v1/address_live_cells_controller_test.rb index a4446429c..91d6eaf57 100644 --- a/test/controllers/api/v1/address_live_cells_controller_test.rb +++ b/test/controllers/api/v1/address_live_cells_controller_test.rb @@ -23,7 +23,7 @@ class AddressLiveCellsControllerTest < ActionDispatch::IntegrationTest address = create(:address, :with_udt_transactions) valid_get api_v1_address_live_cell_url(address.address_hash) - assert_equal ({ "data" => [], "meta" => { "total" => 0, "page_size" => 20 } }), json + assert_equal ({ "data" => [], "meta" => { "total" => 0, "page_size" => 20, "total_pages" => 0 } }), json end test "should return all live cells" do