-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update rgbpp assets statistics
- Loading branch information
Showing
9 changed files
with
200 additions
and
50 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
app/controllers/api/v2/rgbpp_assets_statistics_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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? | ||
|
||
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 |
21 changes: 0 additions & 21 deletions
21
app/controllers/api/v2/rgbpp_hourly_statistics_controller.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
# == 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 | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
class GenerateRgbppAssetsStatisticWorker | ||
include Sidekiq::Job | ||
sidekiq_options queue: "rgbpp" | ||
|
||
def perform | ||
statistic_attributes = [ | ||
ft_count_attributes, | ||
dob_count_attributes, | ||
btc_transactions_count_attributes, | ||
ckb_transactions_count_attributes, | ||
btc_holders_count_attributes, | ||
ckb_holders_count_attributes, | ||
] | ||
|
||
puts "====" | ||
puts statistic_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 | ||
|
||
private | ||
|
||
def ft_count_attributes | ||
xudts_count = Udt.published_xudt.joins(:xudt_tag).where("xudt_tags.tags && ARRAY[?]::varchar[]", ["rgb++"]).count | ||
{ indicator: "ft_count", value: xudts_count, network: "global" } | ||
end | ||
|
||
def dob_count_attributes | ||
token_collections_count = TokenCollection.where("tags && ARRAY[?]::varchar[]", ["rgb++"]).count | ||
{ indicator: "dob_count", value: token_collections_count, network: "global" } | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
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).distinct.count(:bitcoin_address_id) | ||
{ indicator: "holders_count", value: holders_count, network: "btc" } | ||
end | ||
|
||
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).where("amount > 0").distinct.count(:address_id) | ||
{ indicator: "holders_count", value: holders_count, network: "ckb" } | ||
end | ||
|
||
def to_be_counted_date | ||
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 | ||
|
||
def started_at | ||
@started_at ||= to_be_counted_date.beginning_of_day | ||
end | ||
|
||
def ended_at | ||
@ended_at ||= to_be_counted_date.end_of_day | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
db/migrate/20241218085721_create_rgbpp_assets_statistics.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateRgbppAssetsStatistics < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :rgbpp_assets_statistics do |t| | ||
t.integer :indicator, null: false | ||
t.decimal :value, precision: 40, default: 0.0 | ||
t.integer :network, default: 0 | ||
t.integer :created_at_unixtimestamp | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :rgbpp_assets_statistics, %i[indicator network created_at_unixtimestamp], unique: true, | ||
name: "index_on_indicator_and_network_and_created_at_unixtimestamp" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters