Skip to content

Commit

Permalink
Merge pull request #718 from nervosnetwork/rc/v0.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda authored Aug 11, 2020
2 parents 455a466 + e8c2bc2 commit 620f9c0
Show file tree
Hide file tree
Showing 76 changed files with 1,164 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public/* linguist-documentation
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [v0.11.1](https://github.com/shaojunda/ckb-explorer/compare/v0.11.0...v0.11.1) (2020-08-12)

### Performance Improvements

* [#716](https://github.com/nervosnetwork/ckb-explorer/pull/716): use record counter to replace count(*)

# [v0.11.0](https://github.com/nervosnetwork/ckb-explorer/compare/v0.10.1...v0.11.0) (2020-07-30)


Expand Down
9 changes: 4 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ GEM
simplecov
url
coderay (1.1.2)
concurrent-ruby (1.1.6)
concurrent-ruby (1.1.7)
config (1.7.2)
activesupport (>= 3.0)
deep_merge (~> 1.2, >= 1.2.1)
Expand Down Expand Up @@ -160,7 +160,7 @@ GEM
activesupport (>= 4.2.0)
hashdiff (1.0.0)
hiredis (0.6.3)
i18n (1.8.3)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.2)
json (2.3.1)
Expand Down Expand Up @@ -316,8 +316,7 @@ GEM
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
spring (2.0.2)
activesupport (>= 4.2)
spring (2.1.0)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
spring (>= 1.2, < 3.0)
Expand All @@ -343,7 +342,7 @@ GEM
websocket-driver (0.7.2)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.3.0)
zeitwerk (2.4.0)

PLATFORMS
ruby
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def show
ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase).recent.page(@page).per(@page_size)
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size).call
records_counter = RecordCounters::AddressDaoTransactions.new(address)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
CkbTransactionsSerializer.new(ckb_dao_transactions, options.merge(params: { previews: true })).serialized_json
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def show

json =
Rails.cache.realize(@ckb_transactions.cache_key, version: @ckb_transactions.cache_version) do
@options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: @ckb_transactions, page: @page, page_size: @page_size).call
records_counter = RecordCounters::AddressTransactions.new(@address)
@options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: @ckb_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
json_result
end

Expand Down
5 changes: 3 additions & 2 deletions app/controllers/api/v1/address_udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def show
udt = Udt.find_by(type_hash: params[:type_hash], published: true)
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank?

ckb_dao_transactions = address.ckb_udt_transactions(params[:type_hash]).select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase).recent.page(@page).per(@page_size)
ckb_dao_transactions = address.ckb_udt_transactions(udt.id).select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase).recent.page(@page).per(@page_size)
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size).call
records_counter = RecordCounters::AddressUdtTransactions.new(address, udt.id)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
CkbTransactionsSerializer.new(ckb_dao_transactions, options.merge(params: { previews: true })).serialized_json
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/block_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def show
ckb_transactions = block.ckb_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase).order(:id).page(@page).per(@page_size)
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
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
CkbTransactionsSerializer.new(ckb_transactions, options.merge(params: { previews: true })).serialized_json
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def index
blocks = Block.recent.select(:id, :miner_hash, :number, :timestamp, :reward, :ckb_transactions_count, :live_cell_changes).page(@page).per(@page_size)
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
records_counter = RecordCounters::Blocks.new
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: blocks, page: @page, page_size: @page_size, records_counter: records_counter).call
BlockListSerializer.new(blocks, options).serialized_json
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def index
ckb_transactions = CkbTransaction.recent.normal.page(@page).per(@page_size).select(:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved)
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version, race_condition_ttl: 3.seconds) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
records_counter = RecordCounters::Transactions.new
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
CkbTransactionListSerializer.new(ckb_transactions, options).serialized_json
end
render json: json
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/contract_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def show
ckb_transactions = dao_contract.ckb_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase).recent.page(@page).per(@page_size)
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
records_counter = RecordCounters::DaoTransactions.new(dao_contract)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
CkbTransactionsSerializer.new(ckb_transactions, options.merge(params: { previews: true })).serialized_json
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def show
ckb_transactions = udt.ckb_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase).recent.page(@page).per(@page_size)
json =
Rails.cache.realize(ckb_transactions.cache_key, version: ckb_transactions.cache_version) do
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call
records_counter = RecordCounters::UdtTransactions.new(udt)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
CkbTransactionsSerializer.new(ckb_transactions, options.merge(params: { previews: true })).serialized_json
end

Expand Down
7 changes: 0 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ class ApplicationController < ActionController::API
before_action :check_header_info, :set_raven_context

rescue_from Api::V1::Exceptions::Error, with: :api_error
rescue_from ActionController::RoutingError do |exception|
render json: { message: exception.message }, status: :not_found
end

def homepage
render json: { message: "Please read more API info at https://github.com/nervosnetwork/ckb-explorer/" }
end

def catch_404
raise ActionController::RoutingError.new(params[:path])
end

private

def set_raven_context
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ErrorsController < ActionController::API
def routing_error
render json: { message: "Not Found" }, status: :not_found
end
end
14 changes: 10 additions & 4 deletions app/lib/fast_jsonapi/pagination_meta_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ class PaginationMetaGenerator
DEFAULT_PAGE = 1
DEFAULT_PER_PAGE = 20

def initialize(request:, records:, page:, page_size:)
def initialize(request:, records:, page:, page_size:, records_counter: 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)
@total_pages = records.total_pages
@records = records
@hash = { links: {}, meta: { total: records.total_count, page_size: @page_size } }
@records_counter = records_counter.presence || records
@total_count = @records_counter.total_count
@total_pages = total_pages
@hash = { links: {}, meta: { total: @total_count, page_size: @page_size } }
end

def total_pages
(total_count / @page_size).ceil
end

def call
Expand All @@ -30,7 +36,7 @@ def call

private

attr_reader :page, :page_size, :records, :total_pages
attr_reader :page, :page_size, :records, :records_counter, :total_count
attr_accessor :url, :hash

def generate_url(page)
Expand Down
10 changes: 4 additions & 6 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ def custom_ckb_transactions
end

def ckb_dao_transactions
CkbTransaction.where("contained_address_ids @> array[?]::bigint[]", [id]).where("tags @> array[?]::varchar[]", ["dao"])
CkbTransaction.where("dao_address_ids @> array[?]::bigint[]", [id])
end

def ckb_udt_transactions(type_hash)
udt = Udt.where(type_hash: type_hash).select(:id).first
return [] if udt.blank?

CkbTransaction.where("contained_address_ids @> array[?]::bigint[]", [id]).where("contained_udt_ids @> array[?]::bigint[]", [udt.id])
def ckb_udt_transactions(udt_id)
CkbTransaction.where("udt_address_ids @> array[?]::bigint[]", [id]).where("contained_udt_ids @> array[?]::bigint[]", [udt_id])
end

def lock_info
Expand Down Expand Up @@ -133,6 +130,7 @@ def unmade_dao_interests
# average_deposit_time :decimal(, )
# unclaimed_compensation :decimal(30, )
# is_depositor :boolean default(FALSE)
# dao_transactions_count :decimal(30, ) default(0)
#
# Indexes
#
Expand Down
Loading

0 comments on commit 620f9c0

Please sign in to comment.