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

Deploy to mainnet #2348

Merged
merged 22 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1b5f9ed
eIssue 256 (#2341)
rabbitz Dec 16, 2024
1deeccc
Merge pull request #2347 from nervosnetwork/develop
rabbitz Dec 16, 2024
1b35118
chore: update cell_outputs query (#2349)
rabbitz Dec 16, 2024
87dd3a1
Merge pull request #2350 from nervosnetwork/develop
rabbitz Dec 16, 2024
5b602c4
Feat/fiber (#2351)
rabbitz Dec 17, 2024
9fa846d
Merge pull request #2352 from nervosnetwork/develop
rabbitz Dec 17, 2024
68b6600
chore: adjust workflows config (#2353)
rabbitz Dec 17, 2024
6cee6d7
Merge pull request #2354 from nervosnetwork/develop
rabbitz Dec 17, 2024
c0bffec
Issue 256 (#2356)
rabbitz Dec 20, 2024
7a71a2e
Merge pull request #2357 from nervosnetwork/develop
rabbitz Dec 20, 2024
84e4ac0
chore: add new xudt compatible code hash (#2358)
zmcNotafraid Dec 20, 2024
d2e5a55
Merge pull request #2359 from nervosnetwork/develop
zmcNotafraid Dec 20, 2024
e9e14fe
Merge released refs/heads/master into develop (#2361)
github-actions[bot] Dec 20, 2024
373bd14
chore: update ft count filter (#2362)
rabbitz Dec 20, 2024
b9ab42e
Merge pull request #2363 from nervosnetwork/develop
rabbitz Dec 20, 2024
cf2f62a
chore: add rgbpp sidekiq queue (#2368)
zmcNotafraid Dec 22, 2024
99a38c8
Merge pull request #2369 from nervosnetwork/develop
zmcNotafraid Dec 22, 2024
5629fe4
Fix/issue 256 (#2370)
rabbitz Dec 23, 2024
1664d75
Merge pull request #2371 from nervosnetwork/develop
rabbitz Dec 23, 2024
cdb76c7
fix: query default limit set 5000 (#2355)
zmcNotafraid Dec 25, 2024
d43caaf
feat: show activity address by contract in daily statistic (#2372)
zmcNotafraid Dec 25, 2024
fbaebd5
Merge pull request #2373 from nervosnetwork/develop
zmcNotafraid Dec 25, 2024
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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: satackey/[email protected]
# Ignore the failure of a step and avoid terminating the job.
continue-on-error: true
# - uses: satackey/[email protected]
# # Ignore the failure of a step and avoid terminating the job.
# continue-on-error: true
- name: Build and push
id: docker_build
uses: mr-smithers-excellent/docker-build-push@v5
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/api/v2/rgbpp_assets_statistics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Api
module V2
class RgbppAssetsStatisticsController < BaseController
def index
expires_in 15.minutes, public: true, stale_while_revalidate: 5.minutes, stale_if_error: 5.minutes

Check warning on line 5 in app/controllers/api/v2/rgbpp_assets_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgbpp_assets_statistics_controller.rb#L1-L5

Added lines #L1 - L5 were not covered by tests

statistics = RgbppAssetsStatistic.all.order(created_at_unixtimestamp: :asc)
statistics = statistics.where(network: params[:network]) if params[:network].present?
statistics = statistics.where(indicator: params[:indicators].split(",")) if params[:indicators].present?

Check warning on line 9 in app/controllers/api/v2/rgbpp_assets_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgbpp_assets_statistics_controller.rb#L7-L9

Added lines #L7 - L9 were not covered by tests

render json: {
data: statistics.map do |statistic|
{
indicator: statistic.indicator,
value: statistic.value.to_s,
network: statistic.network,
created_at_unixtimestamp: statistic.created_at_unixtimestamp.to_s,
}
end,
}
end
end
end
end

Check warning on line 24 in app/controllers/api/v2/rgbpp_assets_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgbpp_assets_statistics_controller.rb#L11-L24

Added lines #L11 - L24 were not covered by tests
28 changes: 28 additions & 0 deletions app/controllers/api/v2/udt_hourly_statistics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Api
module V2
class UdtHourlyStatisticsController < BaseController
def show
expires_in 15.minutes, public: true, stale_while_revalidate: 5.minutes, stale_if_error: 5.minutes

Check warning on line 5 in app/controllers/api/v2/udt_hourly_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/udt_hourly_statistics_controller.rb#L1-L5

Added lines #L1 - L5 were not covered by tests

udt = Udt.find_by!(type_hash: params[:id], published: true)
hourly_statistics =
if udt.present?
UdtHourlyStatistic.where(udt:).order(created_at_unixtimestamp: :asc)
else
UdtHourlyStatistic.none
end

Check warning on line 13 in app/controllers/api/v2/udt_hourly_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/udt_hourly_statistics_controller.rb#L7-L13

Added lines #L7 - L13 were not covered by tests

render json: {
data: hourly_statistics.map do |statistic|
{
ckb_transactions_count: statistic.ckb_transactions_count.to_s,
amount: statistic.amount.to_s,
holders_count: statistic.holders_count.to_s,
created_at_unixtimestamp: statistic.created_at_unixtimestamp.to_s,
}
end,
}
end
end
end
end

Check warning on line 28 in app/controllers/api/v2/udt_hourly_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/udt_hourly_statistics_controller.rb#L15-L28

Added lines #L15 - L28 were not covered by tests
6 changes: 4 additions & 2 deletions app/models/cell_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def to_raw
# Table name: cell_dependencies
#
# id :bigint not null, primary key
# contract_id :bigint
# ckb_transaction_id :bigint not null
# dep_type :integer
# contract_cell_id :bigint not null
# script_id :bigint
# contract_id :bigint
# implicit :boolean
# implicit :boolean default(TRUE), not null
# block_number :bigint
# tx_index :integer
# contract_analyzed :boolean default(FALSE)
Expand All @@ -40,6 +40,8 @@ def to_raw
#
# index_cell_dependencies_on_block_number_and_tx_index (block_number,tx_index)
# index_cell_dependencies_on_contract_analyzed (contract_analyzed)
# index_cell_dependencies_on_contract_id (contract_id)
# index_cell_dependencies_on_script_id (script_id)
# index_cell_dependencies_on_tx_id_and_cell_id_and_dep_type (ckb_transaction_id,contract_cell_id,dep_type) UNIQUE
# index_on_cell_dependencies_contract_cell_block_tx (contract_cell_id,block_number DESC,tx_index DESC)
#
21 changes: 21 additions & 0 deletions app/models/rgbpp_assets_statistic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class RgbppAssetsStatistic < ApplicationRecord
enum :network, %i[global ckb btc]
enum :indicator, %i[ft_count dob_count holders_count transactions_count]
end

Check warning on line 4 in app/models/rgbpp_assets_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/rgbpp_assets_statistic.rb#L1-L4

Added lines #L1 - L4 were not covered by tests

# == Schema Information
#
# Table name: rgbpp_assets_statistics
#
# id :bigint not null, primary key
# indicator :integer not null
# value :decimal(40, ) default(0)
# network :integer default("global")
# created_at_unixtimestamp :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_on_indicator_and_network_and_created_at_unixtimestamp (indicator,network,created_at_unixtimestamp) UNIQUE
#
18 changes: 18 additions & 0 deletions app/models/rgbpp_hourly_statistic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class RgbppHourlyStatistic < ApplicationRecord
end

Check warning on line 2 in app/models/rgbpp_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/rgbpp_hourly_statistic.rb#L1-L2

Added lines #L1 - L2 were not covered by tests

# == Schema Information
#
# Table name: rgbpp_hourly_statistics
#
# id :bigint not null, primary key
# xudt_count :integer default(0)
# dob_count :integer default(0)
# created_at_unixtimestamp :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_rgbpp_hourly_statistics_on_created_at_unixtimestamp (created_at_unixtimestamp) UNIQUE
#
40 changes: 40 additions & 0 deletions app/models/udt_hourly_statistic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class UdtHourlyStatistic < ApplicationRecord
belongs_to :udt

Check warning on line 2 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L1-L2

Added lines #L1 - L2 were not covered by tests

def percentage_change(attribute)
yesterday = previous_stat(udt_id, 1)
day_before_yesterday = previous_stat(udt_id, 2)

Check warning on line 6 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L4-L6

Added lines #L4 - L6 were not covered by tests

return nil unless yesterday && day_before_yesterday

Check warning on line 8 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L8

Added line #L8 was not covered by tests

yesterday_value = yesterday.public_send(attribute)
day_before_yesterday_value = day_before_yesterday.public_send(attribute)

Check warning on line 11 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L10-L11

Added lines #L10 - L11 were not covered by tests

return nil if day_before_yesterday_value.zero?

Check warning on line 13 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L13

Added line #L13 was not covered by tests

((yesterday_value - day_before_yesterday_value).to_f / day_before_yesterday_value * 100).round(2)
end

Check warning on line 16 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L15-L16

Added lines #L15 - L16 were not covered by tests

def previous_stat(udt_id, days_ago)
timestamp = (Time.current - days_ago.days).beginning_of_day.to_i
self.class.find_by(udt_id:, created_at_unixtimestamp: timestamp)
end
end

Check warning on line 22 in app/models/udt_hourly_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt_hourly_statistic.rb#L18-L22

Added lines #L18 - L22 were not covered by tests

# == Schema Information
#
# Table name: udt_hourly_statistics
#
# id :bigint not null, primary key
# udt_id :bigint not null
# ckb_transactions_count :integer default(0)
# amount :decimal(40, ) default(0)
# holders_count :integer default(0)
# created_at_unixtimestamp :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_on_udt_id_and_unixtimestamp (udt_id,created_at_unixtimestamp) UNIQUE
#
2 changes: 1 addition & 1 deletion app/views/api/v2/fiber/graph_nodes/show.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ json.data do
json.udt_cfg_infos @node.udt_cfg_infos

json.fiber_graph_channels @graph_channels do |channel|
json.(channel, :channel_outpoint, :node1, :node2, :chain_hash, :open_transaction_info, :closed_transaction_info)
json.(channel, :channel_outpoint, :node1, :node2, :chain_hash, :open_transaction_info, :closed_transaction_info, :udt_info)
json.funding_tx_block_number channel.funding_tx_block_number.to_s
json.funding_tx_index channel.funding_tx_index.to_s
json.last_updated_timestamp channel.last_updated_timestamp.to_s
Expand Down
12 changes: 11 additions & 1 deletion app/workers/fiber_graph_detect_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
include Sidekiq::Worker
sidekiq_options queue: "fiber"

attr_accessor :graph_node_ids, :graph_channel_outpoint

Check warning on line 5 in app/workers/fiber_graph_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/fiber_graph_detect_worker.rb#L5

Added line #L5 was not covered by tests

def perform
@graph_node_ids = []
@graph_channel_outpoints = []

Check warning on line 9 in app/workers/fiber_graph_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/fiber_graph_detect_worker.rb#L8-L9

Added lines #L8 - L9 were not covered by tests

# sync graph nodes and channels
["nodes", "channels"].each { fetch_graph_infos(_1) }
# purge outdated graph nodes
FiberGraphNode.where.not(node_id: @graph_node_ids).destroy_all

Check warning on line 14 in app/workers/fiber_graph_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/fiber_graph_detect_worker.rb#L14

Added line #L14 was not covered by tests
# purge outdated graph channels
FiberGraphChannel.where.not(channel_outpoint: @graph_channel_outpoints).destroy_all

Check warning on line 16 in app/workers/fiber_graph_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/fiber_graph_detect_worker.rb#L16

Added line #L16 was not covered by tests

# check channel is closed
FiberGraphChannel.open_channels.each do |channel|
Expand Down Expand Up @@ -61,7 +70,7 @@
peer_id: extract_peer_id(node["addresses"]),
auto_accept_min_ckb_funding_amount: node["auto_accept_min_ckb_funding_amount"],
}

@graph_node_ids << node_attributes[:node_id]

Check warning on line 73 in app/workers/fiber_graph_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/fiber_graph_detect_worker.rb#L73

Added line #L73 was not covered by tests
fiber_graph_node = FiberGraphNode.upsert(node_attributes, unique_by: %i[node_id], returning: %i[id])

return unless fiber_graph_node && node["udt_cfg_infos"].present?
Expand All @@ -87,6 +96,7 @@

channel_outpoint = channel["channel_outpoint"]
open_transaction = CkbTransaction.find_by(tx_hash: channel_outpoint[0..65])
@graph_channel_outpoints << channel_outpoint

Check warning on line 99 in app/workers/fiber_graph_detect_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/fiber_graph_detect_worker.rb#L99

Added line #L99 was not covered by tests

{
channel_outpoint:,
Expand Down
88 changes: 88 additions & 0 deletions app/workers/generate_rgbpp_assets_statistic_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class GenerateRgbppAssetsStatisticWorker
include Sidekiq::Job
sidekiq_options queue: "rgbpp"

Check warning on line 3 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L1-L3

Added lines #L1 - L3 were not covered by tests

attr_accessor :datetime

Check warning on line 5 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L5

Added line #L5 was not covered by tests

def perform(datetime = nil)
@datetime = datetime
statistic_attributes = [
ft_count_attributes,
dob_count_attributes,
btc_transactions_count_attributes,
ckb_transactions_count_attributes,
btc_holders_count_attributes,
ckb_holders_count_attributes,
]
statistic_attributes.each { _1[:created_at_unixtimestamp] = started_at.to_i }
RgbppAssetsStatistic.upsert_all(statistic_attributes, unique_by: %i[indicator network created_at_unixtimestamp])
rescue StandardError => e
Rails.logger.error "Error occurred during GenerateRgbppHourlyStatistic error: #{e.message}"
end

Check warning on line 21 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L7-L21

Added lines #L7 - L21 were not covered by tests

private

Check warning on line 23 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L23

Added line #L23 was not covered by tests

def ft_count_attributes
timestamp = CkbUtils.time_in_milliseconds(ended_at) - 1
xudts_count = Udt.published_xudt.where(block_timestamp: ..timestamp).count
{ indicator: "ft_count", value: xudts_count, network: "global" }
end

Check warning on line 29 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L25-L29

Added lines #L25 - L29 were not covered by tests

def dob_count_attributes
timestamp = CkbUtils.time_in_milliseconds(ended_at) - 1
token_collections_count = TokenCollection.where("tags && ARRAY[?]::varchar[]", ["rgb++"]).
where(block_timestamp: ..timestamp).count
{ indicator: "dob_count", value: token_collections_count, network: "global" }
end

Check warning on line 36 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L31-L36

Added lines #L31 - L36 were not covered by tests

def btc_transactions_count_attributes
transactions_count = BitcoinTransaction.where(time: started_at.to_i..ended_at.to_i).count
{ indicator: "transactions_count", value: transactions_count, network: "btc" }
end

Check warning on line 41 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L38-L41

Added lines #L38 - L41 were not covered by tests

def ckb_transactions_count_attributes
started_timestamp = CkbUtils.time_in_milliseconds(started_at)
ended_timestamp = CkbUtils.time_in_milliseconds(ended_at) - 1
transactions_count = BitcoinAnnotation.includes(:ckb_transaction).
where(ckb_transactions: { block_timestamp: started_timestamp..ended_timestamp }).count
{ indicator: "transactions_count", value: transactions_count, network: "ckb" }
end

Check warning on line 49 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L43-L49

Added lines #L43 - L49 were not covered by tests

def btc_holders_count_attributes
udt_types = %i[xudt xudt_compatible spore_cell did_cell]
udt_ids = Udt.where(udt_type: udt_types, published: true).ids
address_ids = UdtAccount.where(udt_id: udt_ids).where("amount > 0").pluck(:address_id).uniq
holders_count = BitcoinAddressMapping.where(ckb_address_id: address_ids, created_at: ..ended_at).
distinct.count(:bitcoin_address_id)
{ indicator: "holders_count", value: holders_count, network: "btc" }
end

Check warning on line 58 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L51-L58

Added lines #L51 - L58 were not covered by tests

def ckb_holders_count_attributes
udt_types = %i[xudt xudt_compatible spore_cell did_cell]
udt_ids = Udt.where(udt_type: udt_types, published: true).ids
holders_count = UdtAccount.where(udt_id: udt_ids, created_at: ..ended_at).
where("amount > 0").distinct.count(:address_id)
{ indicator: "holders_count", value: holders_count, network: "ckb" }
end

Check warning on line 66 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L60-L66

Added lines #L60 - L66 were not covered by tests

def to_be_counted_date
if @datetime.present?
return Time.zone.parse(@datetime)
end

Check warning on line 71 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L68-L71

Added lines #L68 - L71 were not covered by tests

last_record = UdtHourlyStatistic.order(created_at_unixtimestamp: :desc).first
if last_record
Time.zone.at(last_record.created_at_unixtimestamp) + 1.day
else
Time.current.yesterday
end
end

Check warning on line 79 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L73-L79

Added lines #L73 - L79 were not covered by tests

def started_at
@started_at ||= to_be_counted_date.beginning_of_day
end

Check warning on line 83 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L81-L83

Added lines #L81 - L83 were not covered by tests

def ended_at
@ended_at ||= to_be_counted_date.end_of_day
end
end

Check warning on line 88 in app/workers/generate_rgbpp_assets_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_rgbpp_assets_statistic_worker.rb#L85-L88

Added lines #L85 - L88 were not covered by tests
61 changes: 61 additions & 0 deletions app/workers/generate_udt_hourly_statistic_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class GenerateUdtHourlyStatisticWorker
include Sidekiq::Job

Check warning on line 2 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L1-L2

Added lines #L1 - L2 were not covered by tests

def perform(datetime = nil)
ActiveRecord::Base.connection.execute("SET statement_timeout = 0")
start_time = to_be_counted_date(datetime)
generate_statistics(start_time)
ActiveRecord::Base.connection.execute("RESET statement_timeout")
rescue StandardError => e
Rails.logger.error "Error occurred during GenerateUdtHourlyStatistic error: #{e.message}"
end

Check warning on line 11 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L4-L11

Added lines #L4 - L11 were not covered by tests

private

Check warning on line 13 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L13

Added line #L13 was not covered by tests

def to_be_counted_date(datetime)
last_record = UdtHourlyStatistic.order(created_at_unixtimestamp: :desc).first
if last_record
Time.zone.at(last_record.created_at_unixtimestamp) + 1.day
else
datetime.is_a?(String) ? Time.zone.parse(datetime) : Time.current.yesterday
end
end

Check warning on line 22 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L15-L22

Added lines #L15 - L22 were not covered by tests

def generate_statistics(start_time)
puts "Generating udt hourly statistics for #{start_time}"
statistic_attributes = []
udt_types = %i[xudt xudt_compatible spore_cell did_cell]
Udt.where(udt_type: udt_types, published: true).find_each do |udt|
statistic_attributes << {
udt_id: udt.id,
amount: calc_amount(udt),
ckb_transactions_count: calc_ckb_transactions_count(udt),
holders_count: calc_holders_count(udt),
created_at_unixtimestamp: start_time.beginning_of_day.to_i,
}
end

Check warning on line 36 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L24-L36

Added lines #L24 - L36 were not covered by tests

if statistic_attributes.present?
UdtHourlyStatistic.upsert_all(statistic_attributes, unique_by: %i[udt_id created_at_unixtimestamp])
end
end

Check warning on line 41 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L38-L41

Added lines #L38 - L41 were not covered by tests

def calc_amount(udt)
inputs_amount = 0
outputs_amount = 0
udt.ckb_transactions.includes(:cell_outputs).find_in_batches(batch_size: 1000) do |transactions|
ids = transactions.map(&:id)
inputs_amount += CellOutput.select(:udt_amount).where(consumed_by_id: ids).sum(:udt_amount)
outputs_amount += CellOutput.select(:udt_amount).where(ckb_transaction_id: ids).sum(:udt_amount)
end
[inputs_amount, outputs_amount].max
end

Check warning on line 52 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L43-L52

Added lines #L43 - L52 were not covered by tests

def calc_ckb_transactions_count(udt)
udt.ckb_transactions.count
end

Check warning on line 56 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L54-L56

Added lines #L54 - L56 were not covered by tests

def calc_holders_count(udt)
udt.udt_holder_allocations.sum("ckb_holder_count + btc_holder_count")
end
end

Check warning on line 61 in app/workers/generate_udt_hourly_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/generate_udt_hourly_statistic_worker.rb#L58-L61

Added lines #L58 - L61 were not covered by tests
2 changes: 2 additions & 0 deletions config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,7 @@
resources :graph_nodes, param: :node_id, only: %i[index show]
resources :graph_channels, only: :index
end
resources :udt_hourly_statistics, only: :show
resources :rgbpp_assets_statistics, only: :index
end
end
14 changes: 14 additions & 0 deletions db/migrate/20241212022531_create_udt_hourly_statistics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateUdtHourlyStatistics < ActiveRecord::Migration[7.0]
def change
create_table :udt_hourly_statistics do |t|
t.bigint :udt_id, null: false
t.integer :ckb_transactions_count, default: 0
t.decimal :amount, precision: 40, default: 0.0
t.integer :holders_count, default: 0
t.integer :created_at_unixtimestamp
t.timestamps
end

add_index :udt_hourly_statistics, %i[udt_id created_at_unixtimestamp], name: "index_on_udt_id_and_unixtimestamp", unique: true
end
end
Loading
Loading