diff --git a/app/controllers/api/v2/statistics_controller.rb b/app/controllers/api/v2/statistics_controller.rb index bf18d73f6..60e2bfefe 100644 --- a/app/controllers/api/v2/statistics_controller.rb +++ b/app/controllers/api/v2/statistics_controller.rb @@ -1,31 +1,36 @@ module Api::V2 class StatisticsController < BaseController def transaction_fees - expires_in 15.seconds, public: true stats_info = StatisticInfo.default + if stale?(stats_info) + expires_in 15.seconds, public: true - render json: { - transaction_fee_rates: stats_info.transaction_fee_rates, - pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates, - last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates, - } + render json: { + transaction_fee_rates: stats_info.transaction_fee_rates, + pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates, + last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates, + } + end end def contract_resource_distributed - expires_in 30.minutes, public: true + contracts = Contract.filter_nil_hash_type + if stale?(contracts) + expires_in 30.minutes, public: true + json = contracts.map do |contract| + { + name: contract.name, + code_hash: contract.code_hash, + hash_type: contract.hash_type, + tx_count: contract.ckb_transactions_count, + h24_tx_count: contract.h24_ckb_transactions_count, + ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8), + address_count: contract.addresses_count, + } + end - json = Contract.filter_nil_hash_type.map do |contract| - { - name: contract.name, - code_hash: contract.code_hash, - hash_type: contract.hash_type, - tx_count: contract.ckb_transactions_count, - ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8), - address_count: contract.addresses_count, - } + render json: end - - render json: end end end diff --git a/app/models/ckb_transaction.rb b/app/models/ckb_transaction.rb index 81cdd6e9d..5e94c1243 100644 --- a/app/models/ckb_transaction.rb +++ b/app/models/ckb_transaction.rb @@ -52,6 +52,7 @@ class CkbTransaction < ApplicationRecord created_after(start_block_timestamp).created_before(end_block_timestamp) } scope :inner_block, ->(block_id) { where("block_id = ?", block_id) } + scope :h24, -> { where("block_timestamp >= ?", 24.hours.ago.to_i * 1000) } after_commit :flush_cache before_destroy :recover_dead_cell diff --git a/app/models/contract.rb b/app/models/contract.rb index 1db207239..ccd01a011 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -44,6 +44,7 @@ def self.create_initial_data # total_deployed_cells_capacity :decimal(30, ) default(0) # total_referring_cells_capacity :decimal(30, ) default(0) # addresses_count :integer +# h24_ckb_transactions_count :integer # # Indexes # diff --git a/app/workers/contract_statistic_worker.rb b/app/workers/contract_statistic_worker.rb index 8ad6968a4..942c22254 100644 --- a/app/workers/contract_statistic_worker.rb +++ b/app/workers/contract_statistic_worker.rb @@ -3,9 +3,11 @@ class ContractStatisticWorker sidekiq_options queue: "critical" def perform + h24_tx_ids = CkbTransaction.h24.pluck(:id) Contract.find_each do |contract| contract.update( ckb_transactions_count: contract.cell_dependencies.count, + h24_ckb_transactions_count: contract.cell_dependencies.where(ckb_transaction_id: h24_tx_ids).count, deployed_cells_count: contract.deployed_cell_outputs&.live&.size, referring_cells_count: contract.referring_cell_outputs&.live&.size, total_deployed_cells_capacity: contract.deployed_cell_outputs&.live&.sum(:capacity), diff --git a/db/migrate/20240507041552_add_h24_ckb_transactions_count_to_contract.rb b/db/migrate/20240507041552_add_h24_ckb_transactions_count_to_contract.rb new file mode 100644 index 000000000..27c46036e --- /dev/null +++ b/db/migrate/20240507041552_add_h24_ckb_transactions_count_to_contract.rb @@ -0,0 +1,5 @@ +class AddH24CkbTransactionsCountToContract < ActiveRecord::Migration[7.0] + def change + add_column :contracts, :h24_ckb_transactions_count, :integer + end +end diff --git a/db/structure.sql b/db/structure.sql index 521cd05ef..10efc727a 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -624,39 +624,6 @@ CREATE SEQUENCE public.bitcoin_statistics_id_seq ALTER SEQUENCE public.bitcoin_statistics_id_seq OWNED BY public.bitcoin_statistics.id; --- --- Name: bitcoin_time_locks; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE public.bitcoin_time_locks ( - id bigint NOT NULL, - bitcoin_transaction_id bigint, - ckb_transaction_id bigint, - cell_output_id bigint, - created_at timestamp(6) without time zone NOT NULL, - updated_at timestamp(6) without time zone NOT NULL -); - - --- --- Name: bitcoin_time_locks_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE public.bitcoin_time_locks_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: bitcoin_time_locks_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE public.bitcoin_time_locks_id_seq OWNED BY public.bitcoin_time_locks.id; - - -- -- Name: bitcoin_transactions; Type: TABLE; Schema: public; Owner: - -- @@ -1302,7 +1269,8 @@ CREATE TABLE public.contracts ( 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, - addresses_count integer + addresses_count integer, + h24_ckb_transactions_count integer ); @@ -2703,13 +2671,6 @@ ALTER TABLE ONLY public.bitcoin_addresses ALTER COLUMN id SET DEFAULT nextval('p ALTER TABLE ONLY public.bitcoin_statistics ALTER COLUMN id SET DEFAULT nextval('public.bitcoin_statistics_id_seq'::regclass); --- --- Name: bitcoin_time_locks id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.bitcoin_time_locks ALTER COLUMN id SET DEFAULT nextval('public.bitcoin_time_locks_id_seq'::regclass); - - -- -- Name: bitcoin_transactions id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3102,14 +3063,6 @@ ALTER TABLE ONLY public.bitcoin_statistics ADD CONSTRAINT bitcoin_statistics_pkey PRIMARY KEY (id); --- --- Name: bitcoin_time_locks bitcoin_time_locks_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY public.bitcoin_time_locks - ADD CONSTRAINT bitcoin_time_locks_pkey PRIMARY KEY (id); - - -- -- Name: bitcoin_transactions bitcoin_transactions_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -3885,13 +3838,6 @@ CREATE UNIQUE INDEX index_bitcoin_addresses_on_mapping ON public.bitcoin_address CREATE UNIQUE INDEX index_bitcoin_statistics_on_timestamp ON public.bitcoin_statistics USING btree ("timestamp"); --- --- Name: index_bitcoin_time_locks_on_cell; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX index_bitcoin_time_locks_on_cell ON public.bitcoin_time_locks USING btree (bitcoin_transaction_id, cell_output_id); - - -- -- Name: index_bitcoin_transactions_on_txid; Type: INDEX; Schema: public; Owner: - -- @@ -5157,6 +5103,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20240408082159'), ('20240415080556'), ('20240428085020'), -('20240429102325'); +('20240429102325'), +('20240507041552');