From cdb76c7f1d1235226f4a0722814ffdfd06c1346c Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 25 Dec 2024 20:10:33 +0900 Subject: [PATCH 1/2] fix: query default limit set 5000 (#2355) * fix: query default limit set 5000 Signed-off-by: Miles Zhang * test: fix test Signed-off-by: Miles Zhang --------- 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 | 14 +++++--------- config/settings.mainnet.yml | 1 + config/settings.testnet.yml | 1 + .../api/v1/address_live_cells_controller_test.rb | 2 +- 6 files changed, 12 insertions(+), 14 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..0104f61e7 100644 --- a/app/lib/fast_jsonapi/pagination_meta_generator.rb +++ b/app/lib/fast_jsonapi/pagination_meta_generator.rb @@ -3,19 +3,19 @@ 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 - (total_count / @page_size).ceil + def calculated_total_pages + (total_count.to_f / @page_size).ceil end def call @@ -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 b68e08602..bbe41057b 100644 --- a/config/settings.mainnet.yml +++ b/config/settings.mainnet.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: 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: 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 From d43caaf201f3e4d71319f1bdc6cba6768219bc1a Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 25 Dec 2024 20:54:13 +0900 Subject: [PATCH 2/2] feat: show activity address by contract in daily statistic (#2372) Signed-off-by: Miles Zhang --- app/models/daily_statistic.rb | 107 +++++++++++------- app/serializers/daily_statistic_serializer.rb | 4 + .../charts/daily_statistic_generator.rb | 2 +- ...ontract_distribution_to_daily_statistic.rb | 5 + db/structure.sql | 6 +- 5 files changed, 77 insertions(+), 47 deletions(-) create mode 100644 db/migrate/20241225045757_add_activity_address_contract_distribution_to_daily_statistic.rb diff --git a/app/models/daily_statistic.rb b/app/models/daily_statistic.rb index ff2d11f17..40f3b863b 100644 --- a/app/models/daily_statistic.rb +++ b/app/models/daily_statistic.rb @@ -5,7 +5,7 @@ class DailyStatistic < ApplicationRecord transactions_count addresses_count total_dao_deposit live_cells_count dead_cells_count avg_hash_rate avg_difficulty uncle_rate total_depositors_count address_balance_distribution total_tx_fee occupied_capacity daily_dao_deposit daily_dao_depositors_count circulation_ratio daily_dao_withdraw nodes_count circulating_supply burnt locked_capacity treasury_amount mining_reward - deposit_compensation liquidity created_at_unixtimestamp ckb_hodl_wave holder_count knowledge_size + deposit_compensation liquidity created_at_unixtimestamp ckb_hodl_wave holder_count knowledge_size activity_address_contract_distribution ).freeze MILLISECONDS_IN_DAY = BigDecimal(24 * 60 * 60 * 1000) GENESIS_TIMESTAMP = 1573852190812 @@ -409,8 +409,26 @@ def liquidity dead_query = CellOutput.dead.generated_before(to_be_counted_date.to_i * 1000 - 1).consumed_after(to_be_counted_date.to_i * 1000).select(:address_id).to_sql combined_query = "#{live_query} UNION #{dead_query}" count_query = "SELECT COUNT(DISTINCT address_id) AS count FROM (#{combined_query}) AS combined_results;" - count = ActiveRecord::Base.connection.execute(count_query).first["count"] - count + ActiveRecord::Base.connection.execute(count_query).first["count"] + end + + define_logic :activity_address_contract_distribution do + block_ids = blocks_in_current_period.pluck(:id) + uniq_address_ids = CellOutput.established_status.where(block_id: block_ids).select(:address_id).distinct.map { |cell_output| cell_output.address_id } + results = Address.joins(:lock_script).where(id: uniq_address_ids).group(:code_hash).count + parsed_results = + results.each_with_object({}) do |(key, value), hash| + hex_key = "0x#{key.unpack1('H*')}" + hash[hex_key] = value + end.sort_by { |_k, v| -v } + data = + parsed_results.map do |result| + { Contract.where(is_lock_script: true).where.not(name: nil).where("type_hash = ? OR data_hash = ?", result[0], result[0]).first&.name => result[1] } + end + nil_sum = data.select { |item| item.keys.include?(nil) }.sum { |item| item[nil] } + filtered_data = data.reject { |item| item.keys.include?(nil) } + filtered_data << { "Others" => nil_sum } if nil_sum > 0 + filtered_data end private @@ -534,47 +552,48 @@ def aggron_first_day? # # Table name: daily_statistics # -# id :bigint not null, primary key -# transactions_count :string default("0") -# addresses_count :string default("0") -# total_dao_deposit :string default("0.0") -# block_timestamp :decimal(30, ) -# created_at_unixtimestamp :integer -# created_at :datetime not null -# updated_at :datetime not null -# dao_depositors_count :string default("0") -# unclaimed_compensation :string default("0") -# claimed_compensation :string default("0") -# average_deposit_time :string default("0") -# estimated_apc :string default("0") -# mining_reward :string default("0") -# deposit_compensation :string default("0") -# treasury_amount :string default("0") -# live_cells_count :string default("0") -# dead_cells_count :string default("0") -# avg_hash_rate :string default("0") -# avg_difficulty :string default("0") -# uncle_rate :string default("0") -# total_depositors_count :string default("0") -# total_tx_fee :decimal(30, ) -# address_balance_distribution :jsonb -# occupied_capacity :decimal(30, ) -# daily_dao_deposit :decimal(30, ) -# daily_dao_depositors_count :integer -# daily_dao_withdraw :decimal(30, ) -# circulation_ratio :decimal(, ) -# total_supply :decimal(30, ) -# circulating_supply :decimal(, ) -# block_time_distribution :jsonb -# epoch_time_distribution :jsonb -# epoch_length_distribution :jsonb -# average_block_time :jsonb -# nodes_distribution :jsonb -# nodes_count :integer -# locked_capacity :decimal(30, ) -# ckb_hodl_wave :jsonb -# holder_count :integer -# knowledge_size :decimal(30, ) +# id :bigint not null, primary key +# transactions_count :string default("0") +# addresses_count :string default("0") +# total_dao_deposit :string default("0.0") +# block_timestamp :decimal(30, ) +# created_at_unixtimestamp :integer +# created_at :datetime not null +# updated_at :datetime not null +# dao_depositors_count :string default("0") +# unclaimed_compensation :string default("0") +# claimed_compensation :string default("0") +# average_deposit_time :string default("0") +# estimated_apc :string default("0") +# mining_reward :string default("0") +# deposit_compensation :string default("0") +# treasury_amount :string default("0") +# live_cells_count :string default("0") +# dead_cells_count :string default("0") +# avg_hash_rate :string default("0") +# avg_difficulty :string default("0") +# uncle_rate :string default("0") +# total_depositors_count :string default("0") +# total_tx_fee :decimal(30, ) +# address_balance_distribution :jsonb +# occupied_capacity :decimal(30, ) +# daily_dao_deposit :decimal(30, ) +# daily_dao_depositors_count :integer +# daily_dao_withdraw :decimal(30, ) +# circulation_ratio :decimal(, ) +# total_supply :decimal(30, ) +# circulating_supply :decimal(, ) +# block_time_distribution :jsonb +# epoch_time_distribution :jsonb +# epoch_length_distribution :jsonb +# average_block_time :jsonb +# nodes_distribution :jsonb +# nodes_count :integer +# locked_capacity :decimal(30, ) +# ckb_hodl_wave :jsonb +# holder_count :integer +# knowledge_size :decimal(30, ) +# activity_address_contract_distribution :jsonb # # Indexes # diff --git a/app/serializers/daily_statistic_serializer.rb b/app/serializers/daily_statistic_serializer.rb index dbd75c091..a03aed58b 100644 --- a/app/serializers/daily_statistic_serializer.rb +++ b/app/serializers/daily_statistic_serializer.rb @@ -138,4 +138,8 @@ class DailyStatisticSerializer attribute :knowledge_size, if: Proc.new { |_record, params| params.present? && params[:indicator].include?("knowledge_size") } + + attribute :activity_address_contract_distribution, if: Proc.new { |_record, params| + params.present? && params[:indicator].include?("activity_address_contract_distribution") + } end diff --git a/app/services/charts/daily_statistic_generator.rb b/app/services/charts/daily_statistic_generator.rb index 0d68f26e0..0043dd275 100644 --- a/app/services/charts/daily_statistic_generator.rb +++ b/app/services/charts/daily_statistic_generator.rb @@ -36,7 +36,7 @@ def updated_attrs treasury_amount estimated_apc live_cells_count dead_cells_count avg_hash_rate avg_difficulty uncle_rate address_balance_distribution total_tx_fee occupied_capacity daily_dao_deposit total_supply block_time_distribution - epoch_time_distribution epoch_length_distribution locked_capacity ckb_hodl_wave holder_count + epoch_time_distribution epoch_length_distribution locked_capacity ckb_hodl_wave holder_count activity_address_contract_distribution } established_order + others diff --git a/db/migrate/20241225045757_add_activity_address_contract_distribution_to_daily_statistic.rb b/db/migrate/20241225045757_add_activity_address_contract_distribution_to_daily_statistic.rb new file mode 100644 index 000000000..e09e52866 --- /dev/null +++ b/db/migrate/20241225045757_add_activity_address_contract_distribution_to_daily_statistic.rb @@ -0,0 +1,5 @@ +class AddActivityAddressContractDistributionToDailyStatistic < ActiveRecord::Migration[7.0] + def change + add_column :daily_statistics, :activity_address_contract_distribution, :jsonb + end +end diff --git a/db/structure.sql b/db/structure.sql index db42dedbe..a8e7a0280 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1574,7 +1574,8 @@ CREATE TABLE public.daily_statistics ( locked_capacity numeric(30,0), ckb_hodl_wave jsonb, holder_count integer, - knowledge_size numeric(30,0) + knowledge_size numeric(30,0), + activity_address_contract_distribution jsonb ); @@ -6327,6 +6328,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20241205023729'), ('20241212022531'), ('20241213053309'), -('20241218085721'); +('20241218085721'), +('20241225045757');