Skip to content

Commit

Permalink
feat: show activity address by contract in daily statistic
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid committed Dec 25, 2024
1 parent 5629fe4 commit ca1812a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 47 deletions.
107 changes: 63 additions & 44 deletions app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }

Check warning on line 423 in app/models/daily_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/daily_statistic.rb#L421-L423

Added lines #L421 - L423 were not covered by tests
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] }

Check warning on line 426 in app/models/daily_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/daily_statistic.rb#L426

Added line #L426 was not covered by tests
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
Expand Down Expand Up @@ -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
#
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/daily_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/services/charts/daily_statistic_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddActivityAddressContractDistributionToDailyStatistic < ActiveRecord::Migration[7.0]
def change
add_column :daily_statistics, :activity_address_contract_distribution, :jsonb
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);


Expand Down Expand Up @@ -6327,6 +6328,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20241205023729'),
('20241212022531'),
('20241213053309'),
('20241218085721');
('20241218085721'),
('20241225045757');


0 comments on commit ca1812a

Please sign in to comment.