Skip to content

Commit

Permalink
Merge pull request #506 from shaojunda/shaojunda-adjust-daily-statist…
Browse files Browse the repository at this point in the history
…ic-worker

[ᚬmaster] chore: adjust daily statistic worker
  • Loading branch information
shaojunda authored Dec 11, 2019
2 parents 1fdd346 + 1ade2f1 commit 16acdbc
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .env.test.local.travis
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ DEFAULT_HASH_PREFIX="0x"
BLOCK_SAFETY_INTERVAL="10"
AUTHENTICSYNC_LOOP_INTERVAL = "10"
INAUTHENTICSYNC_LOOP_INTERVAL = "1"
TRANSACTION_UPDATER_LOOP_INTERVAL = "30"
TRANSACTION_UPDATER_LOOP_INTERVAL = "30"
CKB_NET_MODE="mainnet"
1 change: 0 additions & 1 deletion app/controllers/api/v1/epoch_statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ def validate_query_params
end
end
end

2 changes: 1 addition & 1 deletion app/models/block_statistic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BlockStatistic < ApplicationRecord
VALID_INDICATORS = %w(difficulty hash_rate live_cells_count dead_cells_count)
VALID_INDICATORS = %w(difficulty hash_rate live_cells_count dead_cells_count).freeze
end

# == Schema Information
Expand Down
2 changes: 1 addition & 1 deletion app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DailyStatistic < ApplicationRecord
VALID_INDICATORS = %w(transactions_count addresses_count total_dao_deposit)
VALID_INDICATORS = %w(transactions_count addresses_count total_dao_deposit).freeze
end

# == Schema Information
Expand Down
2 changes: 1 addition & 1 deletion app/models/epoch_statistic.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class EpochStatistic < ApplicationRecord
VALID_INDICATORS = %w(difficulty uncle_rate)
VALID_INDICATORS = %w(difficulty uncle_rate).freeze
end

# == Schema Information
Expand Down
11 changes: 6 additions & 5 deletions app/models/market_data.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MarketData
VALID_INDICATORS = %w(total_supply circulating_supply)
VALID_INDICATORS = %w(total_supply circulating_supply).freeze
INITIAL_SUPPLY = BigDecimal(336 * 10**16)
BURN_QUOTA = BigDecimal(84 * 10**16)
ECOSYSTEM_QUOTA = INITIAL_SUPPLY * 0.17
Expand All @@ -24,10 +24,11 @@ def call
private

def parsed_dao
@parsed_dao ||= begin
latest_dao = Block.recent.pick(:dao)
CkbUtils.parse_dao(latest_dao)
end
@parsed_dao ||=
begin
latest_dao = Block.recent.pick(:dao)
CkbUtils.parse_dao(latest_dao)
end
end

def total_supply
Expand Down
2 changes: 1 addition & 1 deletion app/models/statistic_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def miner_ranking
end

def address_balance_ranking
Rails.cache.realize("address_balance_ranking", expires_in: 4.hour) do
Rails.cache.realize("address_balance_ranking", expires_in: 4.hours) do
addresses = Address.order(balance: :desc).limit(50)
addresses.each.with_index(1).map do |address, index|
{ ranking: index.to_s, address: address.address_hash, balance: address.balance.to_s }
Expand Down
8 changes: 4 additions & 4 deletions app/serializers/block_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ class BlockStatisticSerializer
object.block_number.to_s
end

attribute :difficulty, if: Proc.new {|_record, params|
attribute :difficulty, if: Proc.new { |_record, params|
params && params[:indicator].include?("difficulty")
}

attribute :hash_rate, if: Proc.new {|_record, params|
attribute :hash_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("hash_rate")
}

attribute :live_cells_count, if: Proc.new {|_record, params|
attribute :live_cells_count, if: Proc.new { |_record, params|
params && params[:indicator].include?("live_cells_count")
}

attribute :dead_cells_count, if: Proc.new {|_record, params|
attribute :dead_cells_count, if: Proc.new { |_record, params|
params && params[:indicator].include?("dead_cells_count")
}
end
6 changes: 3 additions & 3 deletions app/serializers/daily_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class DailyStatisticSerializer
object.created_at_unixtimestamp.to_s
end

attribute :transactions_count, if: Proc.new {|_record, params|
attribute :transactions_count, if: Proc.new { |_record, params|
params && params[:indicator] == "transactions_count"
}

attribute :addresses_count, if: Proc.new {|_record, params|
attribute :addresses_count, if: Proc.new { |_record, params|
params && params[:indicator] == "addresses_count"
}

attribute :total_dao_deposit, if: Proc.new {|_record, params|
attribute :total_dao_deposit, if: Proc.new { |_record, params|
params && params[:indicator] == "total_dao_deposit"
}
end
4 changes: 2 additions & 2 deletions app/serializers/epoch_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class EpochStatisticSerializer
object.epoch_number.to_s
end

attribute :difficulty, if: Proc.new {|_record, params|
attribute :difficulty, if: Proc.new { |_record, params|
params && params[:indicator].include?("difficulty")
}

attribute :uncle_rate, if: Proc.new {|_record, params|
attribute :uncle_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("uncle_rate")
}
end
9 changes: 4 additions & 5 deletions app/workers/charts/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DailyStatistic
sidekiq_options queue: "critical"

def perform(datetime = nil)
to_be_counted_date = datetime.blank? ? DateTime.now - 1.days : datetime
to_be_counted_date = datetime.presence || DateTime.now - 1.day
started_at = to_be_counted_date.beginning_of_day.strftime("%Q")
ended_at = to_be_counted_date.end_of_day.strftime("%Q")
daily_ckb_transactions_count = CkbTransaction.where("block_timestamp >= ? and block_timestamp <= ?", started_at, ended_at).count
Expand All @@ -15,10 +15,9 @@ def perform(datetime = nil)
total_dao_deposit = datetime.blank? ? deposit_cells.where(status: "live").sum(:capacity) : deposit_cells.sum(:capacity)

block_timestamp = Block.created_after(started_at).created_before(ended_at).recent.pick(:timestamp)
::DailyStatistic.create_or_find_by!(created_at_unixtimestamp: to_be_counted_date.to_i,
block_timestamp: block_timestamp,
transactions_count: daily_ckb_transactions_count,
addresses_count: addresses_count, total_dao_deposit: total_dao_deposit)
daily_statistic = ::DailyStatistic.create_or_find_by!(block_timestamp: block_timestamp)
daily_statistic.update(created_at_unixtimestamp: to_be_counted_date.to_i, transactions_count: daily_ckb_transactions_count,
addresses_count: addresses_count, total_dao_deposit: total_dao_deposit)
end
end
end
13 changes: 6 additions & 7 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Rack::Attack

### Configure Cache ###

# If you don't want to use Rails.cache (Rack::Attack's default), then
Expand All @@ -24,7 +23,7 @@ class Rack::Attack
# Throttle all requests by IP (60rpm)
#
# Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
throttle('req/ip', limit: 300, period: 5.minutes) do |req|
throttle("req/ip", limit: 1500, period: 5.minutes) do |req|
req.ip # unless req.path.start_with?('/assets')
end

Expand All @@ -43,15 +42,15 @@ class Rack::Attack
# end
#
self.throttled_response = lambda do |env|
match_data = env['rack.attack.match_data']
match_data = env["rack.attack.match_data"]
now = match_data[:epoch_time]

headers = {
'RateLimit-Limit' => match_data[:limit].to_s,
'RateLimit-Remaining' => '0',
'RateLimit-Reset' => (now + (match_data[:period] - now % match_data[:period])).to_s
"RateLimit-Limit" => match_data[:limit].to_s,
"RateLimit-Remaining" => "0",
"RateLimit-Reset" => (now + (match_data[:period] - now % match_data[:period])).to_s
}

[ 429, headers, ["Throttled\n"]]
[429, headers, ["Throttled\n"]]
end
end
2 changes: 1 addition & 1 deletion config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
schedule_file = "config/schedule.yml"

if Rails.env.development?
dev_schedule_file= "config/schedule.dev.yml"
dev_schedule_file = "config/schedule.dev.yml"
if File.exist?(dev_schedule_file)
schedule_file = dev_schedule_file
end
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/migration/generate_block_statistic_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace :migration do
Block.where(number: range.to_a).each do |block|
hash_rate = statistic_info.hash_rate(block.number)
total_cells = CellOutput.where("block_timestamp <= ?", block.timestamp)
ckb_transaction_ids = CkbTransaction.where("block_number <= ?", block.number).ids
dead_cells_count = total_cells.where(consumed_by_id: ckb_transaction_ids).count
ckb_transactions = CkbTransaction.where("block_number <= ?", block.number)
dead_cells_count = total_cells.where(consumed_by: ckb_transactions).count
live_cells_count = total_cells.count - dead_cells_count

BlockStatistic.create(block_number: block.number, epoch_number: block.epoch, difficulty: block.difficulty, hash_rate: hash_rate, live_cells_count: live_cells_count, dead_cells_count: live_cells_count)
BlockStatistic.create(block_number: block.number, epoch_number: block.epoch, difficulty: block.difficulty, hash_rate: hash_rate, live_cells_count: live_cells_count, dead_cells_count: dead_cells_count)
progress_bar.increment
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,3 @@ class BlockStatisticsControllerTest < ActionDispatch::IntegrationTest
end
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ class DailyStatisticsControllerTest < ActionDispatch::IntegrationTest
end
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ class EpochStatisticsControllerTest < ActionDispatch::IntegrationTest
end
end
end

2 changes: 1 addition & 1 deletion test/models/daily_statistic_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'test_helper'
require "test_helper"

class DailyStatisticTest < ActiveSupport::TestCase
# test "the truth" do
Expand Down
14 changes: 8 additions & 6 deletions test/models/statistic_info_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ class StatisticInfoTest < ActiveSupport::TestCase
end

test ".address_balance_ranking should return top 50 holders list" do
addresses = create_list(:address, 100).each.with_index(1) do |address, index|
address.update(balance: index * 100)
end
expected_address_balance_ranking = addresses.sort_by(&:balance).reverse[0..49].each.with_index(1).map do |address, index|
{ ranking: index.to_s, address: address.address_hash.to_s, balance: address.balance.to_s }
end
addresses =
create_list(:address, 100).each.with_index(1) do |address, index|
address.update(balance: index * 100)
end
expected_address_balance_ranking =
addresses.sort_by(&:balance).reverse[0..49].each.with_index(1).map do |address, index|
{ ranking: index.to_s, address: address.address_hash.to_s, balance: address.balance.to_s }
end

assert_equal expected_address_balance_ranking, StatisticInfo.new.address_balance_ranking
end
Expand Down

0 comments on commit 16acdbc

Please sign in to comment.