
    yj-                     n   d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
  ej        e          Z G d de          Z G d	 d
e          Z G d d          Zdedz  fdZdededeeeeez  ez  ef                  ddfdZdedeeeeeeeez  ez  ef                  f                  fdZdS )z
Global metrics collector for aggregating performance metrics across benchmark runs.

This module provides functionality to collect, aggregate, and export performance
metrics from deriver and dialectic operations during benchmarking.
    N)datetime)Path)	TypedDict)settingsc                   p    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   eed<   ee         ed	<   d
S )MetricStatsz$Statistics for a single metric type.countmeanmedianminmaxstd_devunit
raw_valuesN)	__name__
__module____qualname____doc__int__annotations__floatstrlist     G/DATA/AppData/hermes/projects/honcho/src/telemetry/metrics_collector.pyr   r      sg         ..JJJ
KKKMMM	JJJ	JJJNNN
IIIUr   r   c                   |    e Zd ZU dZeed<   eed<   eed<   eed<   eeee	         f         ed<   eee
f         ed<   dS )	MetricsExportz"Complete metrics export structure.run_id
start_timeend_timetotal_tasksmetrics_by_typeaggregated_statsN)r   r   r   r   r   r   r   dictr   r   r   r   r   r   r   r   "   so         ,,KKKOOOMMM#tE{*++++3+,,,,,,r   r   c                       e Zd ZdZddZdeddfdZdeeeee	z  e
z  ef                  ddfdZd	eddfd
ZddZdeeef         fdZd	eddfdZddZddZdS )MetricsCollectora  
    Collects and aggregates performance metrics across multiple deriver runs.

    This collector is designed to work alongside the existing logging system,
    capturing metrics from individual tasks and providing aggregated statistics
    for benchmarking analysis.
    returnNc                 Z    d| _         d| _        d| _        i | _        d| _        d| _        dS )z!Initialize the metrics collector.Nr   F)r   r    r!   r#   
task_countis_collecting)selfs    r   __init__zMetricsCollector.__init__6   s4    "&+/)-79 #(r   r   c                     || _         t          j                    | _        d| _        | j                                         d| _        d| _        t          
                    d|           dS )z
        Initialize metrics collection for a new benchmark run.

        Args:
            run_id: Unique identifier for this benchmark run
        Nr   Tz&Started metrics collection for run: %s)r   r   nowr    r!   r#   clearr*   r+   loggerinfo)r,   r   s     r   start_collectionz!MetricsCollector.start_collection?   s^     ",..""$$$!<fEEEEEr   metrics_listc                 h   | j         sdS | xj        dz  c_        |D ]\  }}}|                                                    dd          }|dv r3	 t	          |          }n# t
          t          f$ r Y Ww xY w| d| }|| j        vr
g | j        |<   | j        |                             |           dS )z
        Collect metrics from a completed task.

        Args:
            metrics_list: List of (metric_name, value, unit) tuples
        N    _)id)	r+   r*   lowerreplacer   
ValueError	TypeErrorr#   append)r,   r4   metric_namevaluer   normalized_namenumeric_value
metric_keys           r   collect_metricsz MetricsCollector.collect_metricsN   s     ! 	F1(4 	C 	C$K)//1199#sCCO v~~ %e	*    ,44d44J!55535$Z0 ,33MBBBB)	C 	Cs   A  A43A4filepathc                 `   |                                 sm|j                            dd           |                                 t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t          |          }|D ]\  }}|                     |           dS )z
        Load metrics from a file into this collector.
        Creates the file if it doesn't exist.

        Args:
            filepath: Path to the metrics file to load
        Tparentsexist_okw N)existsparentmkdirtouchopenwriteload_metrics_from_filerD   )r,   rE   ffile_metrics
_task_namer4   s         r   load_from_filezMetricsCollector.load_from_filer   s        	O!!$!>>>NNh$$                .h77(4 	/ 	/$J  ....	/ 	/s   A77A;>A;c                    | j         sdS t                      }|r|                     |           t          j                    | _        d| _         | j        r(| j        r!| j        | j        z
                                  nd}t          	                    d| j
                   t          	                    d| j                   t          	                    d|           t          	                    dt          | j                             dS )zR
        Finalize metrics collection and calculate aggregated statistics.
        NFr   z(Finalized metrics collection for run: %szTasks processed: %szCollection duration: %.2fszMetric types collected: %s)r+   get_metrics_file_pathrV   r   r/   r!   r    total_secondsr1   r2   r   r*   lenr#   )r,   metrics_filedurations      r   finalize_collectionz$MetricsCollector.finalize_collection   s     ! 	F -.. 	.--- " }!%T]T_,;;=== 	
 	>LLL)4?;;;0(;;;0#d6J2K2KLLLLLr   c                    i }| j                                         D ]\  }}|s|                    dd          }t          |          dk    r|\  }}n|d}}t	          t          |          t          j        |          t          j        |          t          |          t          |          t          |          dk    rt          j
        |          nd||                                          ||<   |S )z
        Calculate aggregated statistics for all collected metrics.

        Returns:
            Dictionary mapping metric names to their statistics
        r8   r6      unknowng        )r	   r
   r   r   r   r   r   r   )r#   itemsrsplitrZ   r   
statisticsr
   r   r   r   stdevcopy)r,   statsrC   valuespartsr?   r   s          r   get_aggregated_statsz%MetricsCollector.get_aggregated_stats   s     )+"&"6"<"<">"> 	 	J  %%c1--E5zzQ$)!TT$.	T "-&kk_V,,!(00KKKK47KK!OO
(000!;;==	" 	" 	"E+ r   c                    | j         r| j        st          d          i }| j                                        D ]=\  }}|                    dd          }t          |          dk    r|d         n|}|||<   >| j         | j                                        | j        r| j                                        nd| j	        || 
                                d}|j                            dd	           t          |d
          5 }t          j        ||dt                      ddd           n# 1 swxY w Y   t"                              d|           dS )z
        Export collected metrics to a JSON file.

        Args:
            filepath: Path where the JSON file should be written
        z4Cannot export metrics - collection was never startedr8   r6   r_   r   rK   )r   r    r!   r"   r#   r$   TrG   rJ   )indentdefaultNzExported metrics to: %s)r   r    r<   r#   ra   rb   rZ   	isoformatr!   r*   ri   rM   rN   rP   jsondumpr   r1   r2   )	r,   rE   raw_metricsrC   rg   rh   
clean_nameexport_datarS   s	            r   export_to_jsonzMetricsCollector.export_to_json   s    { 	U$/ 	USTTT /1"&"6"<"<">"> 	- 	-J%%c1--E%(ZZ1__q*J&,K
## k/335559]J//111?* $ 9 9 ; ;&
 &
 	dT:::(C   	=AIk1Q<<<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	-x88888s   ?D))D-0D-c                 "   | j         st          d           dS |                                 }t          d           t          d| j                    t          d           t          d| j                    | j        r<| j        r5| j        | j        z
                                  }t          d|dd           t          d	           t          d
dddddddddddddddddd            t          d dd dd dd dd dd dd            t          |	                                          D ]}||         }|d         }|dk    r)|d         d}|d         d}|d         d}|d         d}	nW|dk    r)|d         d}|d         d}|d         d}|d         d}	n(|d         d}|d         d}|d         d}|d         d}	t          |dd|d          dd|dd|dd|dd|	dd|            t          d           dS )!zF
        Print a summary of collected metrics to the console.
        zNo metrics collectedNzP================================================================================zPERFORMANCE METRICS SUMMARY - zTasks processed: zCollection duration: z.2fszAggregated Performance Metrics:Metricz<40r7   Countz<8Meanz<12MedianMinMaxUnitz(----------------------------------------z--------z------------r   msr
   z.1fr   r   r   z.3fr	   )
r#   printri   r   r*   r    r!   rY   sortedkeys)
r,   rf   r\   r?   statunit_displaymean_str
median_strmin_strmax_strs
             r   print_summaryzMetricsCollector.print_summary   s@    # 	()))F))++h<t{<<===h3$/33444? 	;t} 	;7FFHHH9(9999:::/000ggggggg6ggggggugggSXggg_egg	
 	
 	
 	YY'YYHYYxYY(YYXYYPWYY	
 	
 	

 "%**,,// 	 	K%D<L t##"6l00 $X44
!%[..!%[..$$"6l00 $X44
!%[..!%[.."6l00 $X44
!%[..!%[..  C  C  CT']  C  C  C  C  C  Cz  C  C  CX_  C  C  Cfm  C  C  C  uA  C  C    	hr   c                     d| _         t          j                    | _        t	                      }|r*|                                r|                                 dS dS dS )z-
        Cleanup metrics collection.
        FN)r+   r   r/   r!   rX   rL   unlink)r,   r[   s     r   cleanup_collectionz#MetricsCollector.cleanup_collection  sk     # ,.. 	"L//11 	"!!!!!	" 	" 	" 	"r   )r(   N)r   r   r   r   r-   r   r3   r   tupler   r   rD   r   rV   r]   r%   r   ri   rs   r   r   r   r   r   r'   r'   -   sK        ) ) ) )Fs Ft F F F F"C sC#I,=s'B!CD"C	"C "C "C "CH/t / / / / /$M M M M2 d3+;&<        D!9t !9 !9 !9 !9 !9F5 5 5 5n	" 	" 	" 	" 	" 	"r   r'   r(   c                  D    t           j        } | rt          |           }|S dS )z"Get the current metrics file path.N)r   LOCAL_METRICS_FILEr   )env_path_metrics_file_paths     r   rX   rX   %  s)    *H "!(^^!!4r   	task_slug	task_namer4   c                     t                      }|sdS ddl}ddl}|                                }|  d|  fd|D             d}t          |d          5 }|                    |                                |j                   |                    t          j	        |          dz              |                    |                                |j
                   ddd           dS # 1 swxY w Y   dS )a  
    Append metrics to the shared metrics file for cross-process collection.

    Args:
        task_slug: Slug of the task that generated these metrics
        task_name: Name of the task that generated these metrics
        metrics_list: List of (metric_name, value, unit) tuples
    Nr   r8   c                 .    g | ]\  }}} d | ||dS )r8   namer@   r   r   ).0r   r@   r   r   s       r   
<listcomp>z*append_metrics_to_file.<locals>.<listcomp>H  sG     
 
 
!eT "**D**UDII
 
 
r   )	timestampr   metricsa
)rX   fcntltimerP   flockfilenoLOCK_EXrQ   rn   dumpsLOCK_UN)	r   r   r4   r[   r   r   r   metrics_entryrS   s	   `        r   append_metrics_to_filer   /  sT    )**L LLLKKK 		I!//I//
 
 
 
%1
 
 
 M 
lC	 	  /AAHHJJ...	
=))D0111AHHJJ.../ / / / / / / / / / / / / / / / / /s   BC))C-0C-rE   c                 f   |                                  sg S g }t          |           5 }|D ]m}|                                rWt          j        |                                          }|d         }d |d         D             }|                    ||f           n	 ddd           n# 1 swxY w Y   |S )z
    Load all metrics from the shared metrics file.

    Args:
        filepath: Path to the metrics file

    Returns:
        List of (task_name, metrics_list) tuples
    r   c                 <    g | ]}|d          |d         |d         fS r   r   )r   ms     r   r   z*load_metrics_from_file.<locals>.<listcomp>j  s8          ;<QvY'
AfI6     r   r   N)rL   rP   striprn   loadsr>   )rE   all_metricsrS   lineentryr   r4   s          r   rR   rR   U  s
    ?? 	NPK	h >1 	> 	>Dzz|| >
4::<<00!+.	   @Ei@P      ""I|#<===	>> > > > > > > > > > > > > > > s   A1B&&B*-B*)r   rn   loggingrc   r   pathlibr   typing_extensionsr   
src.configr   	getLoggerr   r1   r   r   r'   rX   r   r   r   r   r   r   rR   r   r   r   <module>r      s                      ' ' ' ' ' '      		8	$	$
 
 
 
 
) 
 
 
- - - - -I - - -u" u" u" u" u" u" u" u"ptd{    #/#/#/ uS#)e"3S89:#/ 
	#/ #/ #/ #/L	%T%S3Y%6 ;<==
>?     r   