-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_node_metrics.py
More file actions
535 lines (474 loc) · 19 KB
/
graph_node_metrics.py
File metadata and controls
535 lines (474 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import time
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
from metrics import *
import based58
import logging
logger = logging.getLogger(__name__)
logging.getLogger("gql").setLevel(logging.WARNING)
FLT_PRECISION = 1e18
def connect_graph_node(graph_node_url):
"""Connect to the Graph Node using the provided URL."""
try:
transport = RequestsHTTPTransport(
url=graph_node_url,
verify=True,
retries=3,
)
client = Client(transport=transport, fetch_schema_from_transport=True)
logger.info(f"Successfully connected to Graph Node: {graph_node_url}")
return client
except Exception as e:
logger.error(f"Failed to connect to Graph Node: {e}")
raise
def get_latest_block(client):
"""Get latest block number"""
try:
query = gql('''
query {
_meta {
block {
number
}
}
}
''')
response = client.execute(query)
block = response['_meta']['block']['number']
FLUENCE_SUBGRAPH_LATEST_BLOCK.set(block)
except Exception as e:
logger.error(f"Error fetching latest block number: {e}")
raise
def get_current_epoch(client):
"""Get current epoch info"""
try:
query = gql('''
query {
epochStatistics(first: 1, orderBy: startTimestamp, orderDirection: desc) {
id
startBlock
startTimestamp
}
}
''')
response = client.execute(query)
epochs = response.get('epochStatistics', [])
if epochs:
return epochs[0]
return None
except Exception as e:
logger.error(f"Error fetching current epoch: {e}")
raise
def get_approved_providers(client):
"""Fetch all providers from the Graph Node."""
try:
query = gql('''
query {
providers(where: {approved: true}) {
id
name
}
}
''')
response = client.execute(query)
return response['providers']
except Exception as e:
logger.error(f"Error fetching providers: {e}")
raise
def decode_peer_id(peer_id):
"""Decode the Peer ID to a human-readable format."""
try:
fixed_prefix_peer_id = peer_id.replace("0x", "002408011220")
bytes_peer_id = bytes.fromhex(fixed_prefix_peer_id)
human_readable_peer_id = based58.b58encode(
bytes_peer_id).decode('utf-8')
return human_readable_peer_id
except Exception as e:
logger.error(f"Error decoding peer ID: {e}")
raise
def get_provider_name(client, provider_id):
"""Fetch the provider's name using its ID."""
try:
query = gql(f'''
query {{
providers(where: {{id: "{provider_id}"}}) {{
name
}}
}}
''')
response = client.execute(query)
providers = response.get('providers', [])
if providers:
return providers[0]['name']
return None
except Exception as e:
logger.error(f"Error fetching provider name for ID {provider_id}: {e}")
raise
def get_network_info(client):
try:
query = gql('''
query {
graphNetworks {
fltPrice
slashingRate
coreEpochDuration
capacityMaxFailedRatio
usdTargetRevenuePerEpoch
minRequiredProofsPerEpoch
dealsTotal
proofsTotal
offersTotal
providersRegisteredTotal
approvedProviders
effectorsTotal
capacityCommitmentsTotal
usdCollateralPerUnit
minRewardPerEpoch
maxRewardPerEpoch
vestingPeriodDuration
vestingPeriodCount
maxProofsPerEpoch
withdrawEpochsAfterFailed
difficulty
}
}
''')
response = client.execute(query)
return response
except Exception as e:
logger.error(f"Failed to connect to Graph Node: {e}")
raise
def collect_peer_cc_metrics(client, provider_id, provider_name):
"""Collect Capacity Commitment metrics for peers of a given provider."""
try:
query = gql(f'''
query {{
peers(where: {{deleted:false,provider_: {{id: "{provider_id}"}}}}) {{
id
computeUnitsTotal
computeUnitsInDeal
currentCapacityCommitment {{
id
status
}}
}}
}}
''')
response = client.execute(query)
peers = response['peers']
status_mapping = {
"Inactive": 0,
"Active": 1,
"WaitDelegation": 2,
"WaitStart": 3,
"Failed": 4,
"Removed": 5
}
for peer in peers:
peer_id = peer['id']
peer_id_decoded = decode_peer_id(peer_id)
if peer['currentCapacityCommitment'] is None:
FLUENCE_PEER_CC_COUNT.labels(
provider_id=provider_id,
provider_name=provider_name,
peer_id=peer_id_decoded).set(0)
else:
FLUENCE_PEER_CC_COUNT.labels(
provider_id=provider_id,
provider_name=provider_name,
peer_id=peer_id_decoded).set(1)
cc_status = peer['currentCapacityCommitment']['status']
# Default to 0 if the status is unknown
status_code = status_mapping.get(cc_status, 0)
FLUENCE_PEER_CС_STATUS.labels(
provider_id=provider_id,
provider_name=provider_name,
peer_id=peer_id_decoded).set(status_code)
FLUENCE_PEER_CU_UNIT_TOTAL.labels(
provider_id=provider_id,
provider_name=provider_name,
peer_id=peer_id_decoded).set(
peer['computeUnitsTotal'])
FLUENCE_PEER_CU_UNIT_IN_DEAL.labels(
provider_id=provider_id,
provider_name=provider_name,
peer_id=peer_id_decoded).set(
peer['computeUnitsInDeal'])
except Exception as e:
logger.error(
f"Error collecting peer CC metrics for provider {provider_name} (ID: {provider_id}): {e}")
raise
def collect_peer_to_deal_metrics(client, provider_id, provider_name):
"""Collect Deal metrics for peers of a given provider."""
try:
query = gql(f'''
query {{
peers(where: {{provider_: {{id: "{provider_id}"}}}}) {{
id
joinedDeals {{
id
}}
}}
}}
''')
response = client.execute(query)
peers = response['peers']
for peer in peers:
peer_id = peer['id']
peer_id_decoded = decode_peer_id(peer_id)
joined_deals_count = len(peer['joinedDeals'])
FLUENCE_PEER_JOINED_DEALS.labels(
provider_id=provider_id,
provider_name=provider_name,
peer_id=peer_id_decoded).set(joined_deals_count)
except Exception as e:
logger.error(
f"Error collecting peer to deal metrics for provider {provider_name} (ID: {provider_id}): {e}")
raise
def collect_deal_metrics(client, provider_id, provider_name):
"""Collect general Deal metrics for a given provider."""
try:
query = gql(f'''
query {{
dealToPeers(where: {{peer_: {{provider: "{provider_id}"}}}}) {{
deal {{
id
createdAt
}}
}}
}}
''')
response = client.execute(query)
deals = response['dealToPeers']
unique_deal_ids = set(deal['deal']['id'] for deal in deals)
active_deals_count = len(unique_deal_ids)
FLUENCE_DEAL_ACTIVE.labels(
provider_id=provider_id,
provider_name=provider_name).set(active_deals_count)
for deal in deals:
deal_id = deal['deal']['id']
deal_created_at = deal['deal']['createdAt']
FLUENCE_DEAL_ACTIVE_START_DATE.labels(
provider_id=provider_id,
provider_name=provider_name,
deal_id=deal_id).set(deal_created_at)
except Exception as e:
logger.error(
f"Error collecting deal metrics for provider {provider_name} (ID: {provider_id}): {e}")
raise
def collect_active_capacity_commitments_stats(client, provider_id, provider_name):
"""Collect stats for active capacity commitments."""
try:
query = gql(f'''
query {{
capacityCommitments(where:{{ and: [{{status: Active}}, {{provider_: {{id: "{provider_id}"}}}}]}}) {{
id
status
totalFailCount
submittedProofsCount
}}
}}
''')
response = client.execute(query)
capacity_commitments = response['capacityCommitments']
for cc in capacity_commitments:
cc_id = cc['id']
totalFailCount = cc['totalFailCount']
submittedProofsCount = cc['submittedProofsCount']
COMMITMENT_TOTAL_FAILED_CUS.labels(
cc_id=cc_id,
provider_id=provider_id,
provider_name=provider_name).set(totalFailCount)
COMMITMENT_SUBMITTED_PROOFS.labels(
cc_id=cc_id,
provider_id=provider_id,
provider_name=provider_name).set(submittedProofsCount)
except Exception as e:
logger.error(f"Error collecting active capacity commitments: {e}")
raise
def collect_current_epoch_proof_stats(client, providers):
"""Collect proof stats for the current epoch."""
try:
current_epoch = get_current_epoch(client)
CORE_CURRENT_EPOCH_START.set(current_epoch['startTimestamp'])
network_info = get_network_info(client)
if not current_epoch or not network_info:
logger.warning("Failed to get current epoch or network info")
return
epoch_id = current_epoch['id']
max_proofs = int(network_info['graphNetworks'][0]['maxProofsPerEpoch'])
min_proofs = int(network_info['graphNetworks'][0]['minRequiredProofsPerEpoch'])
epoch_duration = int(network_info['graphNetworks'][0]['coreEpochDuration'])
if epoch_duration == 0:
logger.error("Epoch duration is zero, cannot calculate projected proofs")
return
time_from_epoch_start = int(time.time()) - int(current_epoch['startTimestamp'])
query = gql(f'''
query {{
capacityCommitmentStatsPerEpoches(
where: {{epoch: "{epoch_id}", capacityCommitment_: {{status: Active}}}}
) {{
submittedProofsCount
totalFailCount
id
activeUnitCount
exitedUnitCount
computeUnitsWithMinRequiredProofsSubmittedCounter
capacityCommitment {{
id
provider {{
id
name
}}
}}
}}
}}
''')
response = client.execute(query)
stats = response.get('capacityCommitmentStatsPerEpoches', [])
if not stats:
logger.warning("No stats returned from GraphQL query")
return
provider_ids = {provider['id'] for provider in providers}
current_cc_ids = {stat['capacityCommitment']['id'] for stat in stats}
for metric in [COMMITMENT_CURRENT_EPOCH_SUBMITTED_PROOFS,
COMMITMENT_CURRENT_EPOCH_MIN_PROJECTED_PROOFS,
COMMITMENT_CURRENT_EPOCH_MAX_PROJECTED_PROOFS,
COMMITMENT_CURRENT_UNITS]:
for labels in list(metric._metrics.keys()):
cc_id = labels[0]
if cc_id not in current_cc_ids:
metric.remove(*labels)
for stat in stats:
provider_id = stat['capacityCommitment']['provider']['id']
provider_name = stat['capacityCommitment']['provider']['name']
cc_id = stat['capacityCommitment']['id']
active_unit_count = int(stat['activeUnitCount'])
exited_unit_count = int(stat['exitedUnitCount'])
finished_unit_count = int(stat['computeUnitsWithMinRequiredProofsSubmittedCounter'])
if provider_id not in provider_ids:
continue
submitted_proofs = int(stat['submittedProofsCount'])
max_projected_proofs = (active_unit_count * max_proofs) * (time_from_epoch_start / epoch_duration)
min_projected_proofs = (active_unit_count * min_proofs) * (time_from_epoch_start / epoch_duration)
COMMITMENT_CURRENT_EPOCH_SUBMITTED_PROOFS.labels(
cc_id=cc_id, provider_id=provider_id, provider_name=provider_name
).set(submitted_proofs)
COMMITMENT_CURRENT_EPOCH_MAX_PROJECTED_PROOFS.labels(
cc_id=cc_id, provider_id=provider_id, provider_name=provider_name
).set(max_projected_proofs)
COMMITMENT_CURRENT_EPOCH_MIN_PROJECTED_PROOFS.labels(
cc_id=cc_id, provider_id=provider_id, provider_name=provider_name
).set(min_projected_proofs)
COMMITMENT_CURRENT_UNITS.labels(
cc_id=cc_id, provider_id=provider_id, provider_name=provider_name, status='active'
).set(active_unit_count)
COMMITMENT_CURRENT_UNITS.labels(
cc_id=cc_id, provider_id=provider_id, provider_name=provider_name, status='exited'
).set(exited_unit_count)
COMMITMENT_CURRENT_UNITS.labels(
cc_id=cc_id, provider_id=provider_id, provider_name=provider_name, status='sent_enough_proofs'
).set(finished_unit_count)
except Exception as e:
logger.error(f"Error collecting proof stats: {e}")
raise
def collect_cc_rewards_balances(client):
"""Collect Capacity Commitment rewards balances."""
try:
query = gql('''
query {
ccsReward(id: "total") {
totalCapacityRewards
unlockedCapacityRewards
withdrawnCapacityRewards
totalDealStakerRewards
}
}
''')
response = client.execute(query)
values = response.get('ccsReward', [])
if not values:
logger.warning("No ccsReward values returned from GraphQL query")
return
total_capacity_reward_balance_flt = float(values['totalCapacityRewards']) / FLT_PRECISION
unlocked_capacity_reward_balance_flt = float(values['unlockedCapacityRewards']) / FLT_PRECISION
withdrawn_capacity_reward_balance_flt = float(values['withdrawnCapacityRewards']) / FLT_PRECISION
total_deal_staker_reward_flt = float(values['totalDealStakerRewards']) / FLT_PRECISION
TOTAL_CAPACITY_REWARD_FLT.set(total_capacity_reward_balance_flt)
UNLOCKED_CAPACITY_REWARD_FLT.set(unlocked_capacity_reward_balance_flt)
WITHDRAWN_CAPACITY_REWARD_FLT.set(withdrawn_capacity_reward_balance_flt)
TOTAL_DEAL_STAKER_REWARD_FLT.set(total_deal_staker_reward_flt)
except Exception as e:
logger.error(f"Error collecting CC rewards balances: {e}")
raise
def collect_graph_networks_metrics(client):
try:
response = get_network_info(client)
graphNetwork = response['graphNetworks'][0]
fltPrice = graphNetwork['fltPrice']
slashingRate = graphNetwork['slashingRate']
coreEpochDuration = graphNetwork['coreEpochDuration']
capacityMaxFailedRatio = graphNetwork['capacityMaxFailedRatio']
usdTargetRevenuePerEpoch = graphNetwork['usdTargetRevenuePerEpoch']
minRequiredProofsPerEpoch = graphNetwork['minRequiredProofsPerEpoch']
dealsTotal = graphNetwork['dealsTotal']
proofsTotal = graphNetwork['proofsTotal']
offersTotal = graphNetwork['offersTotal']
providersRegisteredTotal = graphNetwork['providersRegisteredTotal']
approvedProviders = graphNetwork['approvedProviders']
effectorsTotal = graphNetwork['effectorsTotal']
commitmentCreatedCount = graphNetwork['capacityCommitmentsTotal']
usdCollateralPerUnit = graphNetwork['usdCollateralPerUnit']
minRewardPerEpoch = graphNetwork['minRewardPerEpoch']
maxRewardPerEpoch = graphNetwork['maxRewardPerEpoch']
vestingPeriodDuration = graphNetwork['vestingPeriodDuration']
vestingPeriodCount = graphNetwork['vestingPeriodCount']
maxProofsPerEpoch = graphNetwork['maxProofsPerEpoch']
withdrawEpochsAfterFailed = graphNetwork['withdrawEpochsAfterFailed']
difficulty = graphNetwork['difficulty']
FLT_PRICE.set(fltPrice)
SLASHING_RATE.set(slashingRate)
CORE_EPOCH_DURATION.set(coreEpochDuration)
CAPACITY_MAX_FAILED_RATIO.set(capacityMaxFailedRatio)
USD_TARGET_REVENUE_PER_EPOCH.set(usdTargetRevenuePerEpoch)
MIN_REQUIRED_PROOFS_PER_EPOCH.set(minRequiredProofsPerEpoch)
DEALS_TOTAL.set(dealsTotal)
PROOFS_TOTAL.set(proofsTotal)
OFFERS_TOTAL.set(offersTotal)
PROVIDERS_TOTAL.set(providersRegisteredTotal)
APPROVED_PROVIDERS.set(approvedProviders)
EFFECTORS_TOTAL.set(effectorsTotal)
COMMITMENT_CREATED_COUNT.set(commitmentCreatedCount)
USD_COLLATERAL_PER_UNIT.set(usdCollateralPerUnit)
MIN_REWARD_PER_EPOCH.set(minRewardPerEpoch)
MAX_REWARD_PER_EPOCH.set(maxRewardPerEpoch)
VESTING_PERIOD_DURATION.set(vestingPeriodDuration)
VESTING_PERIOD_COUNT.set(vestingPeriodCount)
MAX_PROOFS_PER_EPOCH.set(maxProofsPerEpoch)
WITHDRAW_EPOCHS_AFTER_FAILED.set(withdrawEpochsAfterFailed)
INFO.info({'name': 'difficulty', 'difficulty': difficulty})
except Exception as e:
logger.error(f"Error collecting graph networks: {e}")
raise
def collect_metrics(graph_node):
try:
get_latest_block(graph_node)
collect_graph_networks_metrics(graph_node)
collect_cc_rewards_balances(graph_node)
providers_to_monitor = get_approved_providers(graph_node)
if providers_to_monitor:
collect_current_epoch_proof_stats(graph_node, providers_to_monitor)
for provider in providers_to_monitor:
provider_id = provider['id']
provider_name = provider['name']
collect_peer_cc_metrics(
graph_node, provider_id, provider_name)
collect_peer_to_deal_metrics(
graph_node, provider_id, provider_name)
collect_deal_metrics(
graph_node, provider_id, provider_name)
collect_active_capacity_commitments_stats(
graph_node, provider_id, provider_name)
except Exception as e:
logger.error(f"Error in collecting metrics from graph-node: {e}")
raise ()