-
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.
Merge branch 'staging' of github.com:nervosnetwork/ckb-explorer into …
…staging
- Loading branch information
Showing
7 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/controllers/api/v2/udt_hourly_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,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 | ||
|
||
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 | ||
|
||
render json: { | ||
data: hourly_statistics.map do |statistic| | ||
{ | ||
ckb_transactions_count: statistic.ckb_transactions_count, | ||
amount: statistic.amount, | ||
holders_count: statistic.holders_count, | ||
created_at_unixtimestamp: statistic.created_at_unixtimestamp, | ||
} | ||
end, | ||
} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class UdtHourlyStatistic < ApplicationRecord | ||
belongs_to :udt | ||
|
||
def percentage_change(attribute) | ||
yesterday = previous_stat(udt_id, 1) | ||
day_before_yesterday = previous_stat(udt_id, 2) | ||
|
||
return nil unless yesterday && day_before_yesterday | ||
|
||
yesterday_value = yesterday.public_send(attribute) | ||
day_before_yesterday_value = day_before_yesterday.public_send(attribute) | ||
|
||
return nil if day_before_yesterday_value.zero? | ||
|
||
((yesterday_value - day_before_yesterday_value).to_f / day_before_yesterday_value * 100).round(2) | ||
end | ||
|
||
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 | ||
|
||
# == 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 | ||
# |
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,54 @@ | ||
class GenerateUdtHourlyStatisticWorker | ||
include Sidekiq::Job | ||
|
||
def perform | ||
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: to_be_counted_date.beginning_of_day.to_i, | ||
} | ||
end | ||
|
||
if statistic_attributes.present? | ||
DailyStatisticGenerator.upsert_all(statistic_attributes, | ||
unique_by: %i[udt_id created_at_unixtimestamp]) | ||
end | ||
rescue StandardError => e | ||
Rails.logger.error "Error occurred during GenerateUdtHourlyStatistic error: #{e.message}" | ||
end | ||
|
||
private | ||
|
||
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 calc_amount(udt) | ||
inputs_amount = 0 | ||
outputs_amount = 0 | ||
ckb_transaction_ids = udt.ckb_transactions.map(&:id) | ||
ckb_transaction_ids.each_slice(1000) do |ids| | ||
inputs_amount += CellOutput.where(consumed_by_id: ids).sum(:udt_amount) | ||
outputs_amount += CellOutput.where(ckb_transaction_id: ids).sum(:udt_amount) | ||
end | ||
[inputs_amount, outputs_amount].max | ||
end | ||
|
||
def calc_ckb_transactions_count(udt) | ||
udt.ckb_transactions.count | ||
end | ||
|
||
def calc_holders_count(udt) | ||
udt.udt_holder_allocations.sum("ckb_holder_count + btc_holder_count") | ||
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
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 |
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