
    &`i6              	       @   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZ ddlZddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lm Z  ddl!m"Z" erddl#m$Z$ ddl!m"Z"  ej%        e&          Z'dZ(dZ)e G d d                      Z*e G d d                      Z+e G d d                      Z,e G d d                      Z-de.de/de.fdZ0e)fdZ1	 	 d(de-de2de2defd Z3d!edej4        fd"Z5d)d$Z6 G d% d#e          Z7 G d& d'e7          Z8dS )*z,Metadata exporter API for Ray Data datasets.    N)ABCabstractmethod)asdict	dataclassfieldis_dataclass)TYPE_CHECKINGAnyDictListMappingOptionalSequence)EventLogTypecheck_export_api_enabledget_export_event_logger)ExportDatasetMetadata)Panel)OPERATOR_PANELS)DatasetState)DataContext)PhysicalOperatorunknownd   c                   (    e Zd ZU dZeed<   eed<   dS )SubStagezRepresents a sub-stage within an operator in the DAG.

    Attributes:
        name: The name of the sub-stage.
        id: The unique identifier of the sub-stage.
    nameidN)__name__
__module____qualname____doc__str__annotations__     x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/_internal/metadata_exporter.pyr   r   )   s.           IIIGGGGGr&   r   c                       e Zd ZU dZeed<   eed<   eed<   ee         ed<   ee         ed<   eed<    ee	          Z
ee         ed	<    ee	          Zee         ed
<    ee          Zeeef         ed<   dS )OperatoraX  Represents a data processing operator in the DAG.

    Attributes:
        name: The name of the operator.
        id: The unique identifier of the operator within the DAG structure, typically
            incorporating a position or index (e.g., "ReadParquet_0"). This is used for
            referencing operators within the DAG topology.
        uuid: The system-generated UUID of the physical operator instance. This is the
            internal unique identifier created when the operator instance is initialized
            and remains consistent throughout its lifetime.
        input_dependencies: List of operator IDs that this operator depends on for input.
        sub_stages: List of sub-stages contained within this operator.
        args: User-specified arguments associated with the operator, which may
            include configuration settings, options, or other relevant data for the operator.
        execution_start_time: The timestamp when the operator execution begins.
        execution_end_time: The timestamp when the operator execution ends.
        state: The state of the operator.
    r   r   uuidexecution_start_timeexecution_end_timestatedefault_factoryinput_dependencies
sub_stagesargsN)r   r    r!   r"   r#   r$   r   floatr   listr0   r   r1   r   dictr2   r   r
   r%   r&   r'   r)   r)   6   s          & IIIGGG
III"5/))) '''JJJ$)E$$?$?$?S	???!&t!<!<!<JX<<< 5666D$sCx.66666r&   r)   c                   v    e Zd ZU dZ ee          Zee         e	d<   e
dddedef         dd fd            Zd	S )
TopologyzRepresents the complete structure of the operator DAG.

    Attributes:
        operators: List of all operators in the DAG.
    r.   	operatorsdagr   op_to_idreturnc                    t                      }|                                 D ]}|         }t          |j        ||j        fd|j        D             t          |                                          ddt          j	        j                  }t          |d          rQ|j        rJt          |j                  D ]5\  }}| d| }|j                            t          ||                     6|j                            |           |S )zCreate a Topology structure from the physical operator DAG.

        Args:
            dag: The operator DAG to analyze.

        Returns:
            A Topology object representing the operator DAG structure.
        c                 (    g | ]}|v |         S r%   r%   ).0depr:   s     r'   
<listcomp>z5Topology.create_topology_metadata.<locals>.<listcomp>x   s)     $ $ $&)cXooHSMooor&   N)r   r   r*   r0   r2   r+   r,   r-   _sub_progress_bar_names_sub_r   r   )r7   post_order_iterr)   r   r   r0   sanitize_for_struct_get_logical_argsr   PENDINGhasattrrA   	enumerater1   appendr   r8   )	r9   r:   resultopop_idoperatorjsub_namesub_stage_ids	    `       r'   create_topology_metadataz!Topology.create_topology_metadata`   sB     %%'' 	. 	.BRLE  WU$ $ $ $-/-B$ $ $ ))=)=)?)?@@%)#'"*/  H r455 Y":T Y#,R-G#H#H Y YKAx&+#5#5!#5#5L'..xX,/W/W/WXXXX##H----r&   N)r   r    r!   r"   r   r4   r8   r   r)   r$   staticmethodr   r#   rR   r%   r&   r'   r7   r7   V   s           !&d ; ; ;ItH~;;;''+/0BC0G+H'	' ' ' \' ' 'r&   r7   c                   |    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   e	e         ed<   e	e         ed<   eed	<   d
S )DatasetMetadataa  Metadata about a Ray Data dataset.

    This class represents the metadata associated with a dataset, including its provenance
    information and execution details.

    Attributes:
        job_id: The ID of the job running this dataset.
        topology: The structure of the dataset's operator DAG.
        dataset_id: The unique ID of the dataset.
        start_time: The timestamp when the dataset is registered.
        data_context: The DataContext attached to the dataset.
        execution_start_time: The timestamp when the dataset execution starts.
        execution_end_time: The timestamp when the dataset execution ends.
        state: The state of the dataset.
    job_idtopology
dataset_id
start_timedata_contextr+   r,   r-   N)
r   r    r!   r"   r#   r$   r7   r3   r   r   r%   r&   r'   rU   rU      s{            KKKOOO"5/))) '''JJJJJr&   rU   struncate_lengthr;   c                 F    t          |           |k    r| d |         dz   S | S )N...)len)r[   r\   s     r'   _add_ellipsis_for_stringr`      s.    
1vv!/!"U**Hr&   c                    t          | t                    r fd|                                 D             S t          | t                    rt	          |           S t          | t
          t          f          rYg }t          |           D ]E\  }}|k    r|                    d            n$|                    t          |                     F|S 	 t          |           rt          t          |                     S t	          t          |                     S # t          $ r1 t           dt          |           j         }t	          |          cY S w xY w)a  Prepares the obj for Struct Protobuf format by recursively
    going through dictionaries, lists, etc...

    - Dataclasses will be converted to dicts
    - Dictionary keys will be converted to strings
    - Lists, tuples, sets, bytes, bytearrays will be converted to lists
    c                 R    i | ]#\  }}t          |          t          |          $S r%   )r#   rE   )r>   kvr\   s      r'   
<dictcomp>z'sanitize_for_struct.<locals>.<dictcomp>   s0    XXXDAqA+A??XXXr&   r^   z: )
isinstancer   itemsr#   r`   r   setrI   rJ   rE   r   r   	ExceptionUNKNOWNtyper   )objr\   resird   unk_names    `    r'   rE   rE      so    #w GXXXXCIIKKXXXX	C		 G'_===	C(C	)	) GcNN 	@ 	@DAqO##

5!!!JJ*1o>>????
	GC   I*6#;;HHH+CHHoFFF 	G 	G 	G!99T#YY%799H+HoFFFFF	Gs   +D >D 8EETdataset_metadatainclude_data_contextinclude_op_argsc                    ddl m} ddlm}m}m}m}  |            } |            }	| j        j        D ]}
 |            }|r|	                    |
j
                    ||
j        |
j        |
j        ||
j        |
j        |j                            |
j                            }|
j        D ]}|j                            |           |
j        D ]3} ||j        |j                  }|j                            |           4|	j                            |            |            }|r'|	                    t-          | j                              || j        | j        | j        || j        | j        |j                            | j                  d t8          D                       }|j                            |	           |S )a  Convert the dataset metadata to a protobuf message.

    Args:
        dataset_metadata: DatasetMetadata object containing the dataset's
            information and DAG structure.
        include_data_context: If DataContext will be exported
        include_op_args: If operator args will be exported

    Returns:
        The protobuf message representing the dataset metadata.
    r   )Struct)r   r)   r   r7   )r   r   r*   r2   r+   r,   r-   rC   c                 ,    g | ]}t          |          S r%   )_to_proto_dashboard_panel)r>   ps     r'   r@   z-dataset_metadata_to_proto.<locals>.<listcomp>  s!    OOO!2155OOOr&   )rX   rV   rY   rZ   r+   r,   r-   operator_panels)google.protobuf.struct_pb2rt   .ray.core.generated.export_dataset_metadata_pb2r   r)   r   r7   rW   r8   updater2   r   r   r*   r+   r,   OperatorStateValuer-   r0   rJ   r1   rE   rZ   rX   rV   rY   r   r   CopyFrom)rp   rq   rr   rt   ProtoDatasetMetadataProtoOperatorProtoSubStageProtoTopologyproto_dataset_metadataproto_topologyrL   r2   proto_operatordep_id	sub_stageproto_sub_stagerZ   s                    r'   dataset_metadata_to_protor      sG   " 211111            2133"]__N '1 8 8vxx 	!KK   &u!#!8!4-33BH==
 
 
 + 	= 	=F-44V<<<<  	> 	>I+m^<  O %,,_==== 	 ''7777 688L P/0@0MNNOOO11#.&#.!-B+>"/556F6LMMOOOOO	 	 	 #,,^<<<!!r&   panelc                 `    t          j        t          | j                  | j                  }|S )z+Convert Dashboard Panel to protobuf format.)r   title)r   DashboardPanelMetadatar#   r   r   )r   proto_panels     r'   rv   rv     s3     '=ux==k  K
 r&   DatasetMetadataExporterc                  4    t                                           S )znGet the dataset metadata exporter instance.

    Returns:
        The dataset metadata exporter instance.
    )LoggerDatasetMetadataExportercreate_if_enabledr%   r&   r'   get_dataset_metadata_exporterr   (  s     )::<<<r&   c            
       x    e Zd ZdZe	 	 d
dedededdfd            Zeede	d          fd	                        Z
dS )r   zAbstract base class for dataset metadata exporters.

    Implementations of this interface can export Ray Data metadata to various destinations
    like log files, databases, or monitoring systems.
    Trp   rq   rr   r;   Nc                     dS )a  Export dataset metadata to the destination.

        Args:
            dataset_metadata: DatasetMetadata object containing dataset information.
            include_data_context: If DataContext will be exported
            include_op_args: If operator args will be exported
        Nr%   )selfrp   rq   rr   s       r'   export_dataset_metadataz/DatasetMetadataExporter.export_dataset_metadata8  s	     	r&   c                     dS )zCreate an exporter instance if the export functionality is enabled.

        Returns:
            An exporter instance if enabled, None otherwise.
        Nr%   )clss    r'   r   z)DatasetMetadataExporter.create_if_enabledH  s	     	r&   TT)r   r    r!   r"   r   rU   boolr   classmethodr   r   r%   r&   r'   r   r   1  s           &* $	 ) # 	
 
   ^ (+D"E    ^ [  r&   c            	       n    e Zd ZdZdej        fdZ	 	 ddedededd	fd
Z	e
ded          fd            Zd	S )r   zDataset metadata exporter implementation that uses the Ray export event logger.

    This exporter writes dataset metadata to log files using Ray's export event system.
    loggerc                     || _         dS )zInitialize with a configured export event logger.

        Args:
            logger: The export event logger to use for writing events.
        N)_export_logger)r   r   s     r'   __init__z&LoggerDatasetMetadataExporter.__init__Y  s     %r&   Trp   rq   rr   r;   Nc                 \    t          |||          }| j                            |           dS )a$  Export dataset metadata using the export event logger.

        Args:
            dataset_metadata: DatasetMetadata object containing dataset information.
            include_data_context: If DataContext will be exported
            include_op_args: If operator args will be exported
        N)r   r   
send_event)r   rp   rq   rr   data_metadata_protos        r'   r   z5LoggerDatasetMetadataExporter.export_dataset_metadataa  s=     8 
 

 	&&':;;;;;r&   c                 t   ddl m} t          |j        j                  }|sdS t
          j                            t          j	        j
        j                                        d          }	 t          t          j        |          }t!          |          S # t"          $ r |                    d           Y dS w xY w)zCreate a logger-based exporter if the export API is enabled.

        Returns:
            A LoggerDatasetMetadataExporter instance if enabled, None otherwise.
        r   )ExportEventNlogszcUnable to initialize the export event logger, so no Dataset Metadata export events will be written.)#ray.core.generated.export_event_pb2r   r   
SourceTypeEXPORT_DATASET_METADATAospathjoinray_privateworker_global_nodeget_session_dir_pathr   r   DATASET_METADATAr   ri   	exception)r   r   &is_dataset_metadata_export_api_enabledlog_directoryr   s        r'   r   z/LoggerDatasetMetadataExporter.create_if_enabledu  s     	DCCCCC1I":2
 2
. 6 	4L,AACCV
 
	,- F 1888 	 	 	*   44	s   +(B B76B7r   )r   r    r!   r"   loggingLoggerr   rU   r   r   r   r   r   r%   r&   r'   r   r   S  s         
%w~ % % % % &* $	< <)< #< 	<
 
< < < <( (+J"K    [  r&   r   r   )r;   r   )9r"   r   r   abcr   r   dataclassesr   r   r   r   typingr	   r
   r   r   r   r   r   r   &ray._private.event.export_event_loggerr   r   r   rz   r   r   /ray.dashboard.modules.metrics.dashboards.commonr   >ray.dashboard.modules.metrics.dashboards.data_dashboard_panelsr   *ray.data._internal.execution.dataset_stater   ray.data.contextr   9ray.data._internal.execution.interfaces.physical_operatorr   	getLoggerr   r   rj   DEFAULT_TRUNCATION_LENGTHr   r)   r7   rU   r#   intr`   rE   r   r   r   rv   r   r   r   r%   r&   r'   <module>r      s   2 2  				 # # # # # # # # > > > > > > > > > > > > N N N N N N N N N N N N N N N N N N 



         
      B A A A A A      D C C C C C ( ( ( ( ( ( -      -,,,,,		8	$	$
    	 	 	 	 	 	 	 	 7 7 7 7 7 7 7 7> 1 1 1 1 1 1 1 1h        6 c c     .G G G G GD "& L" L"%L"L" L" 		L" L" L" L"^		0	 	 	 	= = = =    c   DB B B B B$; B B B B Br&   