@@ -321,66 +321,86 @@ def collect_active_capacity_commitments_stats(client, provider_id, provider_name
321321 logger .error (f"Error collecting active capacity commitments: { e } " )
322322 raise
323323
324+
324325def collect_current_epoch_proof_stats (client , providers ):
325326 """Collect proof stats for the current epoch."""
326327 try :
327328 current_epoch = get_current_epoch (client )
328329 network_info = get_network_info (client )
329- if current_epoch and network_info :
330- epoch_id = current_epoch ['id' ]
331- max_proofs = int (network_info ['graphNetworks' ][0 ]['maxProofsPerEpoch' ])
332- min_proofs = int (network_info ['graphNetworks' ][0 ]['minRequiredProofsPerEpoch' ])
333- epoch_duration = int (network_info ['graphNetworks' ][0 ]['coreEpochDuration' ])
334- time_from_epoch_start = int (time .time ()) - int (current_epoch ['startTimestamp' ])
335-
336- query = gql (f'''
337- query {{
338- capacityCommitmentStatsPerEpoches(
339- where: {{epoch: "{ epoch_id } ", capacityCommitment_: {{status: Active}}}}
340- ) {{
341- submittedProofsCount
342- totalFailCount
330+ if not current_epoch or not network_info :
331+ logger .warning ("Failed to get current epoch or network info" )
332+ return
333+
334+ epoch_id = current_epoch ['id' ]
335+ max_proofs = int (network_info ['graphNetworks' ][0 ]['maxProofsPerEpoch' ])
336+ min_proofs = int (network_info ['graphNetworks' ][0 ]['minRequiredProofsPerEpoch' ])
337+ epoch_duration = int (network_info ['graphNetworks' ][0 ]['coreEpochDuration' ])
338+
339+ if epoch_duration == 0 :
340+ logger .error ("Epoch duration is zero, cannot calculate projected proofs" )
341+ return
342+
343+ time_from_epoch_start = int (time .time ()) - int (current_epoch ['startTimestamp' ])
344+
345+ query = gql (f'''
346+ query {{
347+ capacityCommitmentStatsPerEpoches(
348+ where: {{epoch: "{ epoch_id } ", capacityCommitment_: {{status: Active}}}}
349+ ) {{
350+ submittedProofsCount
351+ totalFailCount
352+ id
353+ activeUnitCount
354+ capacityCommitment {{
355+ id
356+ provider {{
343357 id
344- activeUnitCount
345- capacityCommitment {{
346- id
347- provider {{
348- id
349- name
350- }}
351- }}
358+ name
352359 }}
353- }}
354- ''' )
355- response = client .execute (query )
356- stats = response ['capacityCommitmentStatsPerEpoches' ]
357- provider_ids = set ([provider ['id' ] for provider in providers ])
358-
359- for stat in stats :
360- provider_id = stat ['capacityCommitment' ]['provider' ]['id' ]
361- provider_name = stat ['capacityCommitment' ]['provider' ]['name' ]
362- cc_id = stat ['capacityCommitment' ]['id' ]
363- activeUnitCount = int (stat ['activeUnitCount' ])
364-
365- if provider_id in provider_ids :
366- submitted_proofs = stat ['submittedProofsCount' ]
367- max_projected_proofs = (activeUnitCount * max_proofs ) * (time_from_epoch_start / epoch_duration )
368- min_projected_proofs = (activeUnitCount * min_proofs ) * (time_from_epoch_start / epoch_duration )
369-
370- COMMITMENT_CURRENT_EPOCH_SUBMITTED_PROOFS .labels (
371- cc_id = cc_id ,
372- provider_id = provider_id ,
373- provider_name = provider_name ).set (submitted_proofs )
374-
375- COMMITMENT_CURRENT_EPOCH_MAX_PROJECTED_PROOFS .labels (
376- cc_id = cc_id ,
377- provider_id = provider_id ,
378- provider_name = provider_name ).set (max_projected_proofs )
379-
380- COMMITMENT_CURRENT_EPOCH_MIN_PROJECTED_PROOFS .labels (
381- cc_id = cc_id ,
382- provider_id = provider_id ,
383- provider_name = provider_name ).set (min_projected_proofs )
360+ }}
361+ }}
362+ }}
363+ ''' )
364+ response = client .execute (query )
365+ stats = response .get ('capacityCommitmentStatsPerEpoches' , [])
366+ if not stats :
367+ logger .warning ("No stats returned from GraphQL query" )
368+ return
369+
370+ provider_ids = {provider ['id' ] for provider in providers }
371+ current_cc_ids = {stat ['capacityCommitment' ]['id' ] for stat in stats }
372+
373+ for metric in [COMMITMENT_CURRENT_EPOCH_SUBMITTED_PROOFS ,
374+ COMMITMENT_CURRENT_EPOCH_MIN_PROJECTED_PROOFS ,
375+ COMMITMENT_CURRENT_EPOCH_MAX_PROJECTED_PROOFS ]:
376+ for labels in list (metric ._metrics .keys ()):
377+ cc_id = labels [0 ]
378+ if cc_id not in current_cc_ids :
379+ metric .remove (* labels )
380+
381+ for stat in stats :
382+ provider_id = stat ['capacityCommitment' ]['provider' ]['id' ]
383+ provider_name = stat ['capacityCommitment' ]['provider' ]['name' ]
384+ cc_id = stat ['capacityCommitment' ]['id' ]
385+ active_unit_count = int (stat ['activeUnitCount' ])
386+
387+ if provider_id not in provider_ids :
388+ continue
389+
390+ submitted_proofs = int (stat ['submittedProofsCount' ])
391+ max_projected_proofs = (active_unit_count * max_proofs ) * (time_from_epoch_start / epoch_duration )
392+ min_projected_proofs = (active_unit_count * min_proofs ) * (time_from_epoch_start / epoch_duration )
393+
394+ COMMITMENT_CURRENT_EPOCH_SUBMITTED_PROOFS .labels (
395+ cc_id = cc_id , provider_id = provider_id , provider_name = provider_name
396+ ).set (submitted_proofs )
397+ COMMITMENT_CURRENT_EPOCH_MAX_PROJECTED_PROOFS .labels (
398+ cc_id = cc_id , provider_id = provider_id , provider_name = provider_name
399+ ).set (max_projected_proofs )
400+ COMMITMENT_CURRENT_EPOCH_MIN_PROJECTED_PROOFS .labels (
401+ cc_id = cc_id , provider_id = provider_id , provider_name = provider_name
402+ ).set (min_projected_proofs )
403+
384404 except Exception as e :
385405 logger .error (f"Error collecting proof stats: { e } " )
386406 raise
0 commit comments