Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add network to bitcoin statistics #2376

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions app/models/bitcoin_statistic.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
class BitcoinStatistic < ApplicationRecord
enum :network, %i[ckb btc]
default_scope { order(timestamp: :asc) }

def self.refresh
transaction do
current_time = Time.current
end_time = Time.zone.local(current_time.year, current_time.month, current_time.day, current_time.hour, current_time.min)
start_time = end_time - 30.minutes

Rails.logger.info "current_time: #{current_time}, start_time: #{start_time}, end_time: #{end_time}"

# Count the number of newly generated addresses within half an hour before the current time point
addresses_count = BitcoinAddress.where(created_at: start_time..end_time).count
# Count the number of newly generated transactions within half an hour before the current time point
transactions_count = BitcoinTransaction.where(created_at: start_time..end_time).count
Rails.logger.info "update bitcoin_statistics addresses_count(#{addresses_count}) transactions_count(#{transactions_count})"

statistic = BitcoinStatistic.find_or_initialize_by(timestamp: end_time.utc.to_i * 1000)
statistic.addresses_count = addresses_count
statistic.transactions_count = transactions_count
statistic.save!
end
end
end

# == Schema Information
Expand All @@ -34,5 +14,5 @@ def self.refresh
#
# Indexes
#
# index_bitcoin_statistics_on_timestamp (timestamp) UNIQUE
# index_bitcoin_statistics_on_timestamp (timestamp)
#
62 changes: 62 additions & 0 deletions app/workers/generate_bitcoin_statistic_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class GenerateBitcoinStatisticWorker
include Sidekiq::Job
sidekiq_options queue: "rgbpp"

attr_accessor :datetime

def perform
start_time, end_time = time_range
statistic_attributes = [
btc_attributes(start_time, end_time),
ckb_attributes(start_time, end_time),
]

Rails.logger.info "update bitcoin_statistics #{statistic_attributes}"

BitcoinStatistic.upsert_all(statistic_attributes, unique_by: %i[timestamp network])
rescue StandardError => e
Rails.logger.error "Error occurred during GenerateBitcoinStatisticWorker error: #{e.message}"
end

private

def btc_attributes(start_time, end_time)
addresses_count = BitcoinAddress.where(created_at: start_time..end_time).count
transactions_count = BitcoinTransaction.where(created_at: start_time..end_time).count
timestamp = end_time.utc.to_i * 1000
{ timestamp:, addresses_count:, transactions_count:, network: :btc }
end

def ckb_attributes(start_time, end_time)
start_timestamp = CkbUtils.time_in_milliseconds(start_time)
end_timestamp = CkbUtils.time_in_milliseconds(end_time) - 1

ft_transaction_ids = Set.new
Udt.where(udt_type: %i[xudt xudt_compatible]).find_each do |xudt|
ft_transaction_ids.merge(xudt.ckb_transactions.where(block_timestamp: start_timestamp..end_timestamp).ids)
end

dob_transaction_ids = Set.new
TokenCollection.spore.find_each do |token_collection|
transfers = token_collection.transfers.joins(:ckb_transaction).
where("ckb_transactions.block_timestamp >= ?", start_timestamp).
where("ckb_transactions.block_timestamp < ?", end_timestamp)

dob_transaction_ids.merge(transfers.map(&:transaction_id))
end

transactions_count = ft_transaction_ids.length + dob_transaction_ids.length
timestamp = end_time.utc.to_i * 1000
{ timestamp:, addresses_count: 0, transactions_count:, network: :ckb }
end

def time_range
current_time = Time.current
end_time = Time.zone.local(current_time.year, current_time.month, current_time.day, current_time.hour, current_time.min)
start_time = end_time - 30.minutes

Rails.logger.info "current_time: #{current_time}, start_time: #{start_time}, end_time: #{end_time}"

[start_time, end_time]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddNetworkToBitcoinStatistics < ActiveRecord::Migration[7.0]
def change
add_column :bitcoin_statistics, :network, :integer
remove_index :bitcoin_statistics, :timestamp
add_index :bitcoin_statistics, %i[timestamp network], unique: true
end
end
2 changes: 1 addition & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4771,7 +4771,7 @@ CREATE UNIQUE INDEX index_bitcoin_annotations_on_ckb_transaction_id ON public.bi
-- Name: index_bitcoin_statistics_on_timestamp; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_bitcoin_statistics_on_timestamp ON public.bitcoin_statistics USING btree ("timestamp");
CREATE INDEX index_bitcoin_statistics_on_timestamp ON public.bitcoin_statistics USING btree ("timestamp");


--
Expand Down
2 changes: 1 addition & 1 deletion lib/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def call_worker(clz)
end

s.cron "0,30 * * * *" do
BitcoinStatistic.refresh
call_worker GenerateBitcoinStatisticWorker
end

s.every "2m", overlap: false do
Expand Down
Loading