From 11ad77c3f4d99d8b8601a27c5f917ff3ff94698b Mon Sep 17 00:00:00 2001 From: NanZhang Date: Mon, 18 Sep 2023 15:50:54 +0800 Subject: [PATCH] feat: add contract statistic (#1456) * feat: add contract statistic * chore: adjust tests --- app/controllers/api/v2/scripts_controller.rb | 27 +++++------- app/models/contract.rb | 29 +++++++------ .../v2/scripts/deployed_cells.json.jbuilder | 43 +++++++++---------- .../v2/scripts/referring_cells.json.jbuilder | 43 +++++++++---------- app/workers/contact_statistic_worker.rb | 20 +++++++++ ...230918033957_add_statistics_to_contract.rb | 9 ++++ db/structure.sql | 10 ++++- lib/scheduler.rb | 4 ++ 8 files changed, 110 insertions(+), 75 deletions(-) create mode 100644 app/workers/contact_statistic_worker.rb create mode 100644 db/migrate/20230918033957_add_statistics_to_contract.rb diff --git a/app/controllers/api/v2/scripts_controller.rb b/app/controllers/api/v2/scripts_controller.rb index 0378d3872..63c80ce39 100644 --- a/app/controllers/api/v2/scripts_controller.rb +++ b/app/controllers/api/v2/scripts_controller.rb @@ -9,12 +9,8 @@ class ScriptsController < BaseController def general_info head :not_found and return if @script.blank? || @contract.blank? - key = ["contract_info", @contract.code_hash, @contract.hash_type] - result = - Rails.cache.fetch(key, expires_in: 10.minutes) do - get_script_content - end - render json: { data: result } + expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds + render json: { data: get_script_content } end def ckb_transactions @@ -31,23 +27,20 @@ def deployed_cells head :not_found and return if @contract.blank? expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds - @deployed_cells = @contract.deployed_cells.page(@page).per(@page_size).fast_page + @deployed_cells = @contract.deployed_cell_outputs.live.page(@page).per(@page_size).fast_page end def referring_cells head :not_found and return if @contract.blank? expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds - @referring_cells = @contract.referring_cells.page(@page).per(@page_size).fast_page + @referring_cells = @contract.referring_cell_outputs.live.page(@page).per(@page_size).fast_page end private def get_script_content - referring_cells = @contract&.referring_cell_outputs - deployed_cells = @contract&.deployed_cell_outputs&.live - transactions = @contract&.cell_dependencies - + deployed_cells = @contract.deployed_cell_outputs if deployed_cells.present? deployed_type_script = deployed_cells[0].type_script if deployed_type_script.code_hash == Settings.type_id_code_hash @@ -60,11 +53,11 @@ def get_script_content code_hash: @script.code_hash, hash_type: @script.hash_type, script_type: @script.class.to_s, - capacity_of_deployed_cells: deployed_cells&.sum(:capacity), - capacity_of_referring_cells: referring_cells&.sum(:capacity), - count_of_transactions: transactions&.count.to_i, - count_of_deployed_cells: deployed_cells&.count.to_i, - count_of_referring_cells: referring_cells&.count.to_i + capacity_of_deployed_cells: @contract.total_deployed_cells_capacity, + capacity_of_referring_cells: @contract.total_referring_cells_capacity, + count_of_transactions: @contract.ckb_transactions_count, + count_of_deployed_cells: @contract.deployed_cells_count, + count_of_referring_cells: @contract.referring_cells_count } end diff --git a/app/models/contract.rb b/app/models/contract.rb index 115e6e5d5..f6aeee812 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -25,18 +25,23 @@ def self.create_initial_data # # Table name: contracts # -# id :bigint not null, primary key -# code_hash :binary -# hash_type :string -# deployed_args :string -# role :string default("type_script") -# name :string -# symbol :string -# description :string -# verified :boolean default(FALSE) -# created_at :datetime not null -# updated_at :datetime not null -# deprecated :boolean +# id :bigint not null, primary key +# code_hash :binary +# hash_type :string +# deployed_args :string +# role :string default("type_script") +# name :string +# symbol :string +# description :string +# verified :boolean default(FALSE) +# created_at :datetime not null +# updated_at :datetime not null +# deprecated :boolean +# ckb_transactions_count :decimal(30, ) default(0) +# deployed_cells_count :decimal(30, ) default(0) +# referring_cells_count :decimal(30, ) default(0) +# total_deployed_cells_capacity :decimal(30, ) default(0) +# total_referring_cells_capacity :decimal(30, ) default(0) # # Indexes # diff --git a/app/views/api/v2/scripts/deployed_cells.json.jbuilder b/app/views/api/v2/scripts/deployed_cells.json.jbuilder index 7f84e408f..ddf21033d 100644 --- a/app/views/api/v2/scripts/deployed_cells.json.jbuilder +++ b/app/views/api/v2/scripts/deployed_cells.json.jbuilder @@ -1,27 +1,26 @@ json.data do json.deployed_cells @deployed_cells do |deployed_cell| - cell_output = deployed_cell.cell_output - json.id deployed_cell.cell_output.id - json.capacity deployed_cell.cell_output.capacity - json.ckb_transaction_id deployed_cell.cell_output.ckb_transaction_id - json.created_at deployed_cell.cell_output.created_at - json.updated_at deployed_cell.cell_output.updated_at - json.status cell_output.status - json.address_id cell_output.address_id - json.block_id cell_output.block_id - json.tx_hash cell_output.tx_hash - json.cell_index cell_output.cell_index - json.consumed_by_id cell_output.consumed_by_id - json.cell_type cell_output.cell_type - json.data_size cell_output.data_size - json.occupied_capacity cell_output.occupied_capacity - json.block_timestamp cell_output.block_timestamp - json.consumed_block_timestamp cell_output.consumed_block_timestamp - json.type_hash cell_output.type_hash - json.udt_amount cell_output.udt_amount - json.dao cell_output.dao - json.lock_script_id cell_output.lock_script_id - json.type_script_id cell_output.type_script_id + json.id deployed_cell.id + json.capacity deployed_cell.capacity + json.ckb_transaction_id deployed_cell.ckb_transaction_id + json.created_at deployed_cell.created_at + json.updated_at deployed_cell.updated_at + json.status deployed_cell.status + json.address_id deployed_cell.address_id + json.block_id deployed_cell.block_id + json.tx_hash deployed_cell.tx_hash + json.cell_index deployed_cell.cell_index + json.consumed_by_id deployed_cell.consumed_by_id + json.cell_type deployed_cell.cell_type + json.data_size deployed_cell.data_size + json.occupied_capacity deployed_cell.occupied_capacity + json.block_timestamp deployed_cell.block_timestamp + json.consumed_block_timestamp deployed_cell.consumed_block_timestamp + json.type_hash deployed_cell.type_hash + json.udt_amount deployed_cell.udt_amount + json.dao deployed_cell.dao + json.lock_script_id deployed_cell.lock_script_id + json.type_script_id deployed_cell.type_script_id end json.meta do json.total @deployed_cells.total_count diff --git a/app/views/api/v2/scripts/referring_cells.json.jbuilder b/app/views/api/v2/scripts/referring_cells.json.jbuilder index a4cc5f13f..c496c2e9e 100644 --- a/app/views/api/v2/scripts/referring_cells.json.jbuilder +++ b/app/views/api/v2/scripts/referring_cells.json.jbuilder @@ -1,27 +1,26 @@ json.data do json.referring_cells @referring_cells do |referring_cell| - cell_output = referring_cell.cell_output - json.id cell_output.id - json.capacity cell_output.capacity - json.ckb_transaction_id cell_output.ckb_transaction_id - json.created_at cell_output.created_at - json.updated_at cell_output.updated_at - json.status cell_output.status - json.address_id cell_output.address_id - json.block_id cell_output.block_id - json.tx_hash cell_output.tx_hash - json.cell_index cell_output.cell_index - json.consumed_by_id cell_output.consumed_by_id - json.cell_type cell_output.cell_type - json.data_size cell_output.data_size - json.occupied_capacity cell_output.occupied_capacity - json.block_timestamp cell_output.block_timestamp - json.consumed_block_timestamp cell_output.consumed_block_timestamp - json.type_hash cell_output.type_hash - json.udt_amount cell_output.udt_amount - json.dao cell_output.dao - json.lock_script_id cell_output.lock_script_id - json.type_script_id cell_output.type_script_id + json.id referring_cell.id + json.capacity referring_cell.capacity + json.ckb_transaction_id referring_cell.ckb_transaction_id + json.created_at referring_cell.created_at + json.updated_at referring_cell.updated_at + json.status referring_cell.status + json.address_id referring_cell.address_id + json.block_id referring_cell.block_id + json.tx_hash referring_cell.tx_hash + json.cell_index referring_cell.cell_index + json.consumed_by_id referring_cell.consumed_by_id + json.cell_type referring_cell.cell_type + json.data_size referring_cell.data_size + json.occupied_capacity referring_cell.occupied_capacity + json.block_timestamp referring_cell.block_timestamp + json.consumed_block_timestamp referring_cell.consumed_block_timestamp + json.type_hash referring_cell.type_hash + json.udt_amount referring_cell.udt_amount + json.dao referring_cell.dao + json.lock_script_id referring_cell.lock_script_id + json.type_script_id referring_cell.type_script_id end json.meta do json.total @referring_cells.total_count diff --git a/app/workers/contact_statistic_worker.rb b/app/workers/contact_statistic_worker.rb new file mode 100644 index 000000000..558132c41 --- /dev/null +++ b/app/workers/contact_statistic_worker.rb @@ -0,0 +1,20 @@ +class ContractStatisticWorker + include Sidekiq::Worker + sidekiq_options queue: "critical" + + def perform + Contract.find_each do |contract| + referring_cells = contract.referring_cell_outputs&.live + deployed_cells = contract.deployed_cell_outputs&.live + transactions = contract.cell_dependencies + + contract.update( + ckb_transactions_count: transactions.count, + deployed_cells_count: deployed_cells&.count.to_i, + referring_cells_count: referring_cells&.count.to_i, + total_deployed_cells_capacity: deployed_cells&.sum(:capacity), + total_referring_cells_capacity: referring_cells&.sum(:capacity) + ) + end + end +end diff --git a/db/migrate/20230918033957_add_statistics_to_contract.rb b/db/migrate/20230918033957_add_statistics_to_contract.rb new file mode 100644 index 000000000..e78c6eed2 --- /dev/null +++ b/db/migrate/20230918033957_add_statistics_to_contract.rb @@ -0,0 +1,9 @@ +class AddStatisticsToContract < ActiveRecord::Migration[7.0] + def change + add_column :contracts, :ckb_transactions_count, :decimal, precision: 30, default: 0 + add_column :contracts, :deployed_cells_count, :decimal, precision: 30, default: 0 + add_column :contracts, :referring_cells_count, :decimal, precision: 30, default: 0 + add_column :contracts, :total_deployed_cells_capacity, :decimal, precision: 30, default: 0 + add_column :contracts, :total_referring_cells_capacity, :decimal, precision: 30, default: 0 + end +end diff --git a/db/structure.sql b/db/structure.sql index 341b75993..3d2eae987 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1048,7 +1048,12 @@ CREATE TABLE public.contracts ( verified boolean DEFAULT false, created_at timestamp(6) without time zone NOT NULL, updated_at timestamp(6) without time zone NOT NULL, - deprecated boolean + deprecated boolean, + ckb_transactions_count numeric(30,0) DEFAULT 0.0, + deployed_cells_count numeric(30,0) DEFAULT 0.0, + referring_cells_count numeric(30,0) DEFAULT 0.0, + total_deployed_cells_capacity numeric(30,0) DEFAULT 0.0, + total_referring_cells_capacity numeric(30,0) DEFAULT 0.0 ); @@ -4750,6 +4755,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20230808020637'), ('20230829061910'), ('20230913091025'), -('20230914120928'); +('20230914120928'), +('20230918033957'); diff --git a/lib/scheduler.rb b/lib/scheduler.rb index f440a9937..ff8d0fa58 100644 --- a/lib/scheduler.rb +++ b/lib/scheduler.rb @@ -121,4 +121,8 @@ def call_worker(clz) call_worker CleanAddressBlockSnapshotWorker end +s.every "1h", overlap: false do + call_worker ContractStatisticWorker +end + s.join