@@ -2,21 +2,21 @@ module Charts
22 class DailyStatisticGenerator
33 MILLISECONDS_IN_DAY = BigDecimal ( 24 * 60 * 60 * 1000 )
44
5- def initialize ( datetime = nil )
5+ def initialize ( datetime = nil , from_scratch = false )
66 raise "datetime must be a Time" if datetime . present? && !datetime . is_a? ( Time )
77
88 @datetime = datetime
9+ @from_scratch = from_scratch
910 end
1011
1112 def call
12- daily_ckb_transactions_count = CkbTransaction . where ( "block_timestamp >= ? and block_timestamp <= ?" , started_at , ended_at ) . count
13- addresses_count = Address . where ( "block_timestamp <= ?" , ended_at ) . count
13+ daily_ckb_transactions_count = CkbTransaction . created_after ( started_at ) . created_before ( ended_at ) . count
1414 cell_outputs = CellOutput . where . not ( cell_type : "normal" )
15- current_tip_block = Block . where ( "timestamp <= ?" , ended_at ) . recent . first
1615 mining_reward = Block . where ( "timestamp <= ?" , ended_at ) . sum ( :secondary_reward )
1716 deposit_compensation = unclaimed_compensation ( cell_outputs , current_tip_block ) + claimed_compensation ( cell_outputs )
1817 estimated_apc = DaoContract . default_contract . estimated_apc ( current_tip_block . fraction_epoch )
1918 block_timestamp = Block . created_after ( started_at ) . created_before ( ended_at ) . recent . pick ( :timestamp )
19+ addresses_count = processed_addresses_count
2020 daily_statistic = ::DailyStatistic . find_or_create_by! ( created_at_unixtimestamp : to_be_counted_date . to_i )
2121 daily_statistic . update ( block_timestamp : block_timestamp , transactions_count : daily_ckb_transactions_count ,
2222 addresses_count : addresses_count , total_dao_deposit : total_dao_deposit ,
@@ -28,14 +28,35 @@ def call
2828
2929 private
3030
31+ attr_reader :datetime , :from_scratch
32+
33+ def processed_addresses_count
34+ if from_scratch
35+ Address . created_before ( ended_at ) . count
36+ else
37+ Address . created_after ( started_at ) . created_before ( ended_at ) . count + latest_daily_statistic . addresses_count . to_i
38+ end
39+ end
40+
41+ def current_tip_block
42+ @current_tip_block ||=
43+ begin
44+ if from_scratch
45+ Block . created_before ( ended_at ) . recent . first
46+ else
47+ Block . created_after ( started_at ) . created_before ( ended_at ) . recent . first
48+ end
49+ end
50+ end
51+
3152 def total_dao_deposit
32- deposit_amount = DaoEvent . processed . where ( "block_timestamp <= ?" , ended_at ) . where ( event_type : "deposit_to_dao" ) . sum ( :value )
33- withdraw_amount = DaoEvent . processed . where ( "block_timestamp <= ?" , ended_at ) . where ( event_type : "withdraw_from_dao" ) . sum ( :value )
53+ deposit_amount = DaoEvent . processed . deposit_to_dao . created_before ( ended_at ) . sum ( :value )
54+ withdraw_amount = DaoEvent . processed . withdraw_from_dao . created_before ( ended_at ) . sum ( :value )
3455 deposit_amount - withdraw_amount
3556 end
3657
3758 def dao_depositors_count
38- DaoEvent . processed . where ( event_type : " new_dao_depositor" ) . where ( "block_timestamp <= ?" , ended_at ) . count - DaoEvent . processed . where ( event_type : " take_away_all_deposit" ) . where ( "block_timestamp <= ?" , ended_at ) . count
59+ DaoEvent . processed . new_dao_depositor . created_before ( ended_at ) . count - DaoEvent . processed . take_away_all_deposit . created_before ( ended_at ) . count
3960 end
4061
4162 def to_be_counted_date
@@ -50,8 +71,6 @@ def ended_at
5071 @ended_at ||= time_in_milliseconds ( to_be_counted_date . end_of_day )
5172 end
5273
53- attr_reader :datetime
54-
5574 def unclaimed_compensation ( cell_outputs , current_tip_block )
5675 @unclaimed_compensation ||=
5776 begin
@@ -62,22 +81,22 @@ def unclaimed_compensation(cell_outputs, current_tip_block)
6281 def claimed_compensation ( cell_outputs )
6382 @claimed_compensation ||=
6483 begin
65- cell_outputs . where ( "consumed_block_timestamp <= ?" , ended_at ) . nervos_dao_withdrawing . dead . reduce ( 0 ) do |memo , nervos_dao_withdrawing_cell |
84+ cell_outputs . nervos_dao_withdrawing . consumed_before ( ended_at ) . reduce ( 0 ) do |memo , nervos_dao_withdrawing_cell |
6685 memo + CkbUtils . dao_interest ( nervos_dao_withdrawing_cell )
6786 end
6887 end
6988 end
7089
7190 def phase1_dao_interests ( cell_outputs )
72- cell_outputs . where ( "block_timestamp <= ?" , ended_at ) . nervos_dao_withdrawing . live . reduce ( 0 ) do |memo , nervos_dao_withdrawing_cell |
91+ cell_outputs . nervos_dao_withdrawing . generated_before ( ended_at ) . unconsumed_at ( ended_at ) . reduce ( 0 ) do |memo , nervos_dao_withdrawing_cell |
7392 memo + CkbUtils . dao_interest ( nervos_dao_withdrawing_cell )
7493 end
7594 end
7695
7796 def unmade_dao_interests ( cell_outputs , current_tip_block )
7897 @unmade_dao_interests ||=
7998 begin
80- cell_outputs . where ( "block_timestamp <= ?" , ended_at ) . nervos_dao_deposit . live . reduce ( 0 ) do |memo , cell_output |
99+ cell_outputs . nervos_dao_deposit . generated_before ( ended_at ) . unconsumed_at ( ended_at ) . reduce ( 0 ) do |memo , cell_output |
81100 dao = cell_output . block . dao
82101 tip_dao = current_tip_block . dao
83102 parse_dao = CkbUtils . parse_dao ( dao )
@@ -90,17 +109,16 @@ def unmade_dao_interests(cell_outputs, current_tip_block)
90109 def average_deposit_time ( cell_outputs )
91110 interest_bearing_deposits = 0
92111 uninterest_bearing_deposits = 0
93- sum_interest_bearing = cell_outputs . where ( "block_timestamp <= ?" , ended_at ) . nervos_dao_withdrawing . live . reduce ( 0 ) do |memo , nervos_dao_withdrawing_cell |
112+ sum_interest_bearing = cell_outputs . nervos_dao_withdrawing . generated_before ( ended_at ) . unconsumed_at ( ended_at ) . reduce ( 0 ) do |memo , nervos_dao_withdrawing_cell |
94113 nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell . generated_by
95114 nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx . cell_inputs . order ( :id ) [ nervos_dao_withdrawing_cell . cell_index ] . previous_cell_output
96115 interest_bearing_deposits += nervos_dao_deposit_cell . capacity
97116 memo + nervos_dao_deposit_cell . capacity * ( nervos_dao_withdrawing_cell . block_timestamp - nervos_dao_deposit_cell . block_timestamp ) / MILLISECONDS_IN_DAY
98117 end
99- sum_uninterest_bearing = cell_outputs . where ( "block_timestamp <= ?" , ended_at ) . nervos_dao_deposit . live . reduce ( 0 ) do |memo , nervos_dao_deposit_cell |
100- current_time = time_in_milliseconds ( Time . current )
118+ sum_uninterest_bearing = cell_outputs . nervos_dao_deposit . generated_before ( ended_at ) . unconsumed_at ( ended_at ) . reduce ( 0 ) do |memo , nervos_dao_deposit_cell |
101119 uninterest_bearing_deposits += nervos_dao_deposit_cell . capacity
102120
103- memo + nervos_dao_deposit_cell . capacity * ( current_time - nervos_dao_deposit_cell . block_timestamp ) / MILLISECONDS_IN_DAY
121+ memo + nervos_dao_deposit_cell . capacity * ( ended_at - nervos_dao_deposit_cell . block_timestamp ) / MILLISECONDS_IN_DAY
104122 end
105123
106124 ( sum_interest_bearing + sum_uninterest_bearing ) / ( interest_bearing_deposits + uninterest_bearing_deposits )
@@ -114,5 +132,9 @@ def treasury_amount(cell_outputs, current_tip_block)
114132 def time_in_milliseconds ( time )
115133 ( time . to_f * 1000 ) . floor
116134 end
135+
136+ def latest_daily_statistic
137+ ::DailyStatistic . order ( created_at_unixtimestamp : :desc ) . first || OpenStruct . new ( addresses_count : 0 , total_dao_deposit : 0 , dao_depositors_count : 0 , unclaimed_compensation : 0 , claimed_compensation : 0 , average_deposit_time : 0 , mining_reward : 0 , deposit_compensation : 0 , treasury_amount : 0 )
138+ end
117139 end
118140end
0 commit comments