Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2023 04 21 sort by field new #1295

Merged
merged 24 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5213e51
added sort to address transactions
iamyates Apr 21, 2023
6c25646
added sort to block list
iamyates Apr 21, 2023
270d509
added index to blocks.reward
iamyates Apr 21, 2023
b8750e0
added sort to transactions, added index
iamyates Apr 23, 2023
99a5e99
added sort to pending transactions
iamyates Apr 23, 2023
d7dc894
added sort to udts
iamyates Apr 23, 2023
0918d8a
added sort to nfts ( token_collections ) and filter
iamyates Apr 23, 2023
6a48ccc
added filter to udt_transactions
iamyates Apr 23, 2023
276d8f2
added filter to dao transactions
iamyates Apr 23, 2023
d4f522a
fixed filter bug for blocks, udts, pending tx
iamyates May 7, 2023
70d1e7f
finished: sorting in blocks api
iamyates May 7, 2023
5814306
finished: sorting in tx api
iamyates May 7, 2023
1b3dca2
finished: sorting in pending_tx api
iamyates May 7, 2023
e3b14ee
finished: sorting in udts api
iamyates May 7, 2023
ce3e2ff
finished: sorting in /api/v1/address_transactions/[:id] api
iamyates May 7, 2023
f1f0464
fixed failed unit test of: app/controllers/api/v2/nft/collections_con…
iamyates May 7, 2023
bc31f85
fixed failt unit test for: controllers/api/v1/block_transactions_cont…
iamyates May 8, 2023
111d341
remove useless index
iamyates May 8, 2023
530c14f
added more columns to sort
iamyates May 8, 2023
c183675
fixed failed unit test, for api/v1/ckb_transactions
iamyates May 22, 2023
d32a2bb
used order(order_by => asc_or_desc)
iamyates May 29, 2023
6aecb15
improved code, used split(., 2)
iamyates May 29, 2023
24dbcb4
fixed failed unit tests
iamyates May 29, 2023
0dafd53
merge
iamyates May 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,29 @@ def show
@address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if @address.is_a?(NullAddress)

@tx_ids = AccountBook.where(address_id: @address.id).order("ckb_transaction_id" => :desc).select("ckb_transaction_id").page(@page).per(@page_size).fast_page
@ckb_transactions = CkbTransaction.tx_committed.where(id: @tx_ids.map(&:ckb_transaction_id)).select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).order(id: :desc)
@tx_ids = AccountBook
.joins(:ckb_transaction)
.where(address_id: @address.id)

params[:sort] ||= "ckb_transaction_id.desc"
order_by, asc_or_desc = params[:sort].split('.', 2)
order_by = case order_by
when 'time' then 'ckb_transactions.block_timestamp'
else order_by
end

head :not_found and return unless order_by.in? %w[ckb_transaction_id block_timestamp ckb_transactions.block_timestamp]

@tx_ids = @tx_ids
.order(order_by => asc_or_desc)
.select("ckb_transaction_id")
.page(@page).per(@page_size).fast_page

order_by = 'id' if order_by == 'ckb_transaction_id'
@ckb_transactions = CkbTransaction.tx_committed.where(id: @tx_ids.map(&:ckb_transaction_id))
.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :capacity_involved)
.order(order_by => asc_or_desc)

json =
Rails.cache.realize("#{@ckb_transactions.cache_key}/#{@address.query_address}", version: @ckb_transactions.cache_version) do
@options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: @ckb_transactions, page: @page, page_size: @page_size, records_counter: @tx_ids).call
Expand Down
14 changes: 9 additions & 5 deletions app/controllers/api/v1/block_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ class BlockTransactionsController < ApplicationController
include Pagy::Backend
def show
block = Block.find_by!(block_hash: params[:id])
temp_transactions = block.ckb_transactions
.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at)
.where(block_timestamp: block.timestamp)
temp_transactions = temp_transactions.where(tx_hash: params[:tx_hash]) if params[:tx_hash].present?
temp_transactions = temp_transactions.order(id: :desc)

@pagy, ckb_transactions = pagy(
block.ckb_transactions
.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at)
.where(block_timestamp: block.timestamp)
.order(:id),
temp_transactions,
items: params[:page_size] || 10,
overflow: :empty_page)
overflow: :empty_page
)

json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
Expand Down
14 changes: 13 additions & 1 deletion app/controllers/api/v1/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ def index
BlockListSerializer.new(blocks).serialized_json
end
else
blocks = Block.recent.select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes, :updated_at).page(@page).per(@page_size).fast_page
blocks = Block.select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes, :updated_at)
params[:sort] ||= "timestamp.desc"

order_by, asc_or_desc = params[:sort].split('.', 2)
order_by = case order_by
when 'height' then 'number'
when 'transactions' then 'ckb_transactions_count'
else order_by
end

head :not_found and return unless order_by.in? %w[number reward timestamp ckb_transactions_count]
blocks = blocks.order(order_by => asc_or_desc).page(@page).per(@page_size).fast_page

json =
Rails.cache.realize(blocks.cache_key, version: blocks.cache_version, race_condition_ttl: 3.seconds) do
records_counter = RecordCounters::Blocks.new
Expand Down
19 changes: 17 additions & 2 deletions app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@ def index
end
render json: json
else
ckb_transactions = CkbTransaction.tx_committed.recent.normal.select(
ckb_transactions = CkbTransaction.normal.select(
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at
).page(@page).per(@page_size).fast_page
)

params[:sort] ||= "id.desc"

order_by, asc_or_desc = params[:sort].split('.', 2)
order_by = case order_by
when 'height' then 'block_number'
when 'capacity' then 'capacity_involved'
else order_by
end

head :not_found and return unless order_by.in? %w[id block_number block_timestamp transaction_fee capacity_involved]

ckb_transactions = ckb_transactions.order(order_by => asc_or_desc)
.page(@page).per(@page_size).fast_page

json =
Rails.cache.realize(ckb_transactions.cache_key,
version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) do
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/api/v1/contract_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ def show
raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != DaoContract::CONTRACT_NAME

dao_contract = DaoContract.default_contract
ckb_transactions = dao_contract.ckb_transactions.includes(:cell_inputs, :cell_outputs).tx_committed.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent.page(@page).per(@page_size).fast_page
ckb_transactions = dao_contract.ckb_transactions.includes(:cell_inputs, :cell_outputs).tx_committed
.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent

ckb_transactions = ckb_transactions.where(tx_hash: params[:tx_hash]) if params[:tx_hash].present?
ckb_transactions = ckb_transactions
.page(@page).per(@page_size).fast_page
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
records_counter = RecordCounters::DaoTransactions.new(dao_contract)
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/api/v1/udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ class UdtTransactionsController < ApplicationController

def show
udt = Udt.find_by!(type_hash: params[:id], published: true)
ckb_transactions = udt.ckb_transactions.tx_committed.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent.page(@page).per(@page_size).fast_page
ckb_transactions = udt.ckb_transactions.tx_committed
.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent

# TODO minted? burn? transfer?
ckb_transactions = ckb_transactions.where(tx_hash: params[:tx_hash]) if params[:tx_hash].present?
ckb_transactions = ckb_transactions
.page(@page).per(@page_size).fast_page

json =
Rails.cache.realize("#{udt.symbol}/#{ckb_transactions.cache_key}", version: ckb_transactions.cache_version) do
records_counter = RecordCounters::UdtTransactions.new(udt)
Expand Down
19 changes: 18 additions & 1 deletion app/controllers/api/v1/udts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@ class Api::V1::UdtsController < ApplicationController
before_action :validate_pagination_params, :pagination_params, only: :index

def index
udts = Udt.sudt.order(addresses_count: :desc, id: :asc).page(@page).per(@page_size).fast_page
udts = Udt.sudt

params[:sort] ||= "id.desc"

order_by, asc_or_desc = params[:sort].split('.', 2)
order_by = case order_by
when 'created_time' then 'block_timestamp'
# current we don't support this in DB
# need a new PR https://github.com/nervosnetwork/ckb-explorer/pull/1266/
# when 'transactions' then 'h24_ckb_transactions_count'
else order_by
end

head :not_found and return unless order_by.in? %w[id addresses_count block_timestamp]

udts = udts.order(order_by => asc_or_desc)
.page(@page).per(@page_size).fast_page

options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: udts, page: @page, page_size: @page_size).call
render json: UdtSerializer.new(udts, options)
end
Expand Down
25 changes: 22 additions & 3 deletions app/controllers/api/v2/nft/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ module Api
module V2
class NFT::CollectionsController < BaseController
def index
pagy, collections = pagy(TokenCollection.order(id: :desc))
params[:sort] ||= "id.desc"

order_by, asc_or_desc = params[:sort].split('.', 2)
order_by = case order_by
# TODO need to merge PR: https://github.com/nervosnetwork/ckb-explorer/pull/1266
when 'transactions' then 'h24_transactions_count'
when 'holder' then 'holders_count'
when 'minted' then 'items_count'
else order_by
end

head :not_found and return unless order_by.in? %w[id holders_count items_count]

collections = TokenCollection
collections = collections.where(standard: params[:type]) if params[:type].present?
collections = collections
.order(order_by => asc_or_desc)
.page(@page).per(@page_size).fast_page

@pagy, @collections = pagy(collections)
render json: {
data: collections,
pagination: pagy_metadata(pagy)
data: @collections,
pagination: pagy_metadata(@pagy)
}
end

Expand Down
18 changes: 16 additions & 2 deletions app/controllers/api/v2/pending_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ module Api::V2
class PendingTransactionsController < BaseController
before_action :set_page_and_page_size
def index
pending_transactions = PoolTransactionEntry.pool_transaction_pending.order('id desc').page(@page).per(@page_size).fast_page
head :not_found and return if pending_transactions.blank?
pending_transactions = PoolTransactionEntry.pool_transaction_pending

params[:sort] ||= "id.desc"
order_by, asc_or_desc = params[:sort].split('.', 2)
order_by = case order_by
when 'time' then 'created_at'
when 'fee' then 'transaction_fee'
# current we don't support this in DB
#when 'capacity' then 'capacity_involved'
else order_by
end

head :not_found and return unless order_by.in? %w[id created_at transaction_fee]

pending_transactions = pending_transactions.order(order_by => asc_or_desc)
.page(@page).per(@page_size).fast_page

render json: {
data: pending_transactions.map {|tx|
Expand Down
11 changes: 6 additions & 5 deletions test/controllers/api/v1/block_transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest

valid_get api_v1_block_transaction_url(block.block_hash)

ckb_transactions = block.ckb_transactions.order(:id).page(page).per(page_size)
ckb_transactions = block.ckb_transactions.order('id desc').page(page).per(page_size)
records_counter = RecordCounters::BlockTransactions.new(block)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: page, page_size: page_size, records_counter: records_counter).call

Expand Down Expand Up @@ -157,7 +157,7 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 2
page_size = 10
block = create(:block, :with_ckb_transactions, transactions_count: 30)
block_ckb_transactions = block.ckb_transactions.order(:id).page(page).per(page_size)
block_ckb_transactions = block.ckb_transactions.order('id desc').page(page).per(page_size)

valid_get api_v1_block_transaction_url(block.block_hash), params: { page: page }

Expand All @@ -173,7 +173,7 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 1
page_size = 12
block = create(:block, :with_ckb_transactions, transactions_count: 15)
block_ckb_transactions = block.ckb_transactions.order(:id).page(page).per(page_size)
block_ckb_transactions = block.ckb_transactions.order('id desc').page(page).per(page_size)

valid_get api_v1_block_transaction_url(block.block_hash), params: { page_size: page_size }

Expand All @@ -186,10 +186,11 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest
end

test "should return the corresponding transactions when page and page_size are set" do
CkbTransaction.delete_all
page = 2
page_size = 5
block = create(:block, :with_ckb_transactions, transactions_count: 30)
block_ckb_transactions = block.ckb_transactions.order(:id).page(page).per(page_size)
block_ckb_transactions = block.ckb_transactions.order('id desc').page(page).per(page_size)

valid_get api_v1_block_transaction_url(block.block_hash), params: { page: page, page_size: page_size }

Expand All @@ -204,7 +205,7 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 2
page_size = 5
block = create(:block)
block_ckb_transactions = block.ckb_transactions.order(:id).page(page).per(page_size)
block_ckb_transactions = block.ckb_transactions.order('id desc').page(page).per(page_size)

valid_get api_v1_block_transaction_url(block.block_hash), params: { page: page, page_size: page_size }

Expand Down