@@ -8,18 +8,15 @@ def perform
88 @graph_node_ids = [ ]
99 @graph_channel_outpoints = [ ]
1010
11- # sync graph nodes and channels
12- [ "nodes" , "channels" ] . each { fetch_graph_infos ( _1 ) }
13- # purge outdated graph nodes
14- FiberGraphNode . where . not ( node_id : @graph_node_ids ) . destroy_all
15- # purge outdated graph channels
16- FiberGraphChannel . where . not ( channel_outpoint : @graph_channel_outpoints ) . destroy_all
17- # check channel is closed
18- FiberGraphChannel . open_channels . each do |channel |
19- funding_cell = channel . funding_cell
20- if funding_cell . consumed_by
21- channel . update ( closed_transaction_id : funding_cell . consumed_by_id )
22- end
11+ ApplicationRecord . transaction do
12+ # sync graph nodes and channels
13+ [ "nodes" , "channels" ] . each { fetch_graph_infos ( _1 ) }
14+ # purge outdated graph nodes
15+ FiberGraphNode . where . not ( node_id : @graph_node_ids ) . destroy_all
16+ # purge outdated graph channels
17+ FiberGraphChannel . where . not ( channel_outpoint : @graph_channel_outpoints ) . destroy_all
18+ # generate statistic
19+ compute_statistic
2320 end
2421 end
2522
@@ -42,7 +39,7 @@ def fetch_graph_infos(data_type)
4239
4340 def fetch_nodes ( last_cursor )
4441 data = rpc . graph_nodes ( ENV [ "FIBER_NODE_URL" ] , { limit : "0x64" , after : last_cursor } )
45- ApplicationRecord . transaction { data . dig ( "result" , "nodes" ) . each { upsert_node_with_cfg_info ( _1 ) } }
42+ data . dig ( "result" , "nodes" ) . each { upsert_node_with_cfg_info ( _1 ) }
4643 data . dig ( "result" , "last_cursor" )
4744 rescue StandardError => e
4845 Rails . logger . error ( "Error fetching nodes: #{ e . message } " )
@@ -54,9 +51,6 @@ def fetch_channels(last_cursor)
5451 channel_attributes = data . dig ( "result" , "channels" ) . map { build_channel_attributes ( _1 ) } . compact
5552 FiberGraphChannel . upsert_all ( channel_attributes , unique_by : %i[ channel_outpoint ] ) if channel_attributes . any?
5653 data . dig ( "result" , "last_cursor" )
57- rescue StandardError => e
58- Rails . logger . error ( "Error fetching channels: #{ e . message } " )
59- nil
6054 end
6155
6256 def upsert_node_with_cfg_info ( node )
@@ -124,6 +118,44 @@ def extract_peer_id(addresses)
124118 end
125119 end
126120
121+ def compute_statistic
122+ total_nodes = FiberGraphNode . count
123+ total_channels = FiberGraphChannel . count
124+ # 资金总量
125+ total_liquidity = FiberGraphChannel . sum ( :capacity )
126+ # 资金均值
127+ mean_value_locked = total_channels . zero? ? 0.0 : total_liquidity . to_f / total_channels
128+ # fee 均值
129+ mean_fee_rate = FiberGraphChannel . average ( "fee_rate_of_node1 + fee_rate_of_node2" ) || 0.0
130+ # 获取 capacity 的数据
131+ capacities = FiberGraphChannel . pluck ( :capacity ) . compact
132+ # 获取 fee_rate_of_node1 和 fee_rate_of_node2 的数据并合并
133+ fee_rate_of_node1 = FiberGraphChannel . pluck ( :fee_rate_of_node1 ) . compact
134+ fee_rate_of_node2 = FiberGraphChannel . pluck ( :fee_rate_of_node2 ) . compact
135+ combined_fee_rates = fee_rate_of_node1 + fee_rate_of_node2
136+ # 计算中位数
137+ medium_value_locked = calculate_median ( capacities )
138+ medium_fee_rate = calculate_median ( combined_fee_rates )
139+ created_at_unixtimestamp = Time . now . beginning_of_day . to_i
140+ FiberStatistic . upsert (
141+ { total_nodes :, total_channels :, total_liquidity :,
142+ mean_value_locked :, mean_fee_rate :, medium_value_locked :,
143+ medium_fee_rate :, created_at_unixtimestamp : } , unique_by : %i[ created_at_unixtimestamp ]
144+ )
145+ end
146+
147+ def calculate_median ( array )
148+ sorted = array . sort
149+ count = sorted . size
150+ return nil if count . zero?
151+
152+ if count . odd?
153+ sorted [ count / 2 ] # 奇数个,取中间值
154+ else
155+ ( sorted [ ( count / 2 ) - 1 ] + sorted [ count / 2 ] ) . to_f / 2 # 偶数个,取中间两个的平均值
156+ end
157+ end
158+
127159 def rpc
128160 @rpc ||= FiberCoordinator . instance
129161 end
0 commit comments