o
    NDiX#                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZm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eZG dd	 d	eZdS )
    )CounterN)EventThread)HostDistance)ms_timestamp_from_datetime)insights_registry)initialize_registryc                   @   sL   e Zd Zdd Zdd ZG dd deZdd Zd	d
 Zdd Z	dd Z
dS )MonitorReporterc                 C   s>   t j| dd tt ||| _| _t | _d| _| 	  dS )z
        takes an int indicating interval between requests, a function returning
        the connection to be used, and the timeout per request
        monitor_reporter)nameTN)
r   __init__r   r   	_interval_sessionr   _shutdown_eventdaemonstart)selfinterval_secsession r   ]/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/datastax/insights/reporter.pyr   &   s   zMonitorReporter.__init__c                 C   s~   |  |   | j| jtdd  | j s=t }|  | 	  t | }| jt
| j| d | j rd S d S )Ng?   g{Gz?)_send_via_rpc_get_startup_datar   waitr   randomuniformis_settime_get_status_datamax)r   
start_timeelapsedr   r   r   run6   s   
zMonitorReporter.runc                   @   s   e Zd ZdS )z!MonitorReporter.ShutDownExceptionN)__name__
__module____qualname__r   r   r   r   ShutDownExceptionE   s    r'   c              
   C   st   z| j dt|f td| W d S  ty9 } ztd| td| W Y d }~d S d }~ww )Nz"CALL InsightsRpc.reportInsight(%s)zInsights RPC data: {}z Insights RPC send failed with {})r   executejsondumpslogdebugformat	Exception)r   dataer   r   r   r   H   s   zMonitorReporter._send_via_rpcc                 C   sp   | j jj}dd | j   D }dddttj ddidt| j jj	t| j j
|jr1|jjnd |d	d
S )Nc                 S   s&   i | ]\}}|j |d  |d dqS )
open_count
in_flights)connectionsinFlightQueries)address).0hoststater   r   r   
<dictcomp>U   s    z4MonitorReporter._get_status_data.<locals>.<dictcomp>zdriver.statusv1EVENTlanguagepythonr   insightMappingIdinsightType	timestamptags)clientId	sessionIdcontrolConnectionconnectedNodesmetadatar/   )r   clustercontrol_connectionget_pool_stateitemsr   datetimeutcnowstr	client_id
session_id_connectionr7   )r   ccconnected_nodesr   r   r   r   R   s"   

z MonitorReporter._get_status_datac                    s   j jj}z
|jj d }W n ty- } zd }td	|j| W Y d }~nd }~ww t
 }t fdd j jD }|tj |tj |tj d}z|jj}W n ty_   d}Y nw d }z; j jjrt j jjtjr{ j jjjtjk}n!ddlm}	  j jj |	jk}n j jjr j jjdtjk}W n ty } ztd		| W Y d }~nd }~ww t ! }
d
ddt"t#j#$ ddidi dddt%j&d j'dt( j jj)dt( j j*d j jj+pdd j jj+ d j jj,d j jj-dt.t/ fdd j jj01 D d|jr|jj2nd d j jj3d|d|d t45 j jj6d!|d" j jj7d#|rC|8 ndt45 j jj9t: j jjpW j jj|d$d% j jj;ri j jj;j<j=nd ii |
j>|
j?|
j@d&tAB t C d't%jD j jjEj=d(d) jFd*d+S ),Nr   z+Unable to get local socket addr from {}: {}c                 3   s     | ]} j jj|V  qd S N)r   rI   profile_managerdistance)r6   r7   r   r   r   	<genexpr>   s
    
z4MonitorReporter._get_startup_data.<locals>.<genexpr>)localremoteignoredNONE)SSL	cert_reqsz%Unable to get the cert validation: {}zdriver.startupr:   r;   r<   r=   r>   
driverNamezDataStax Python DriverdriverVersion	cassandrarC   rD   applicationNameapplicationNameWasGeneratedapplicationVersioncontactPointsdataCentersc                 3   s2    | ]}|j r jjj|tjkr|j V  qd S rU   )
datacenterr   rI   rV   rW   r   LOCAL)r6   hrX   r   r   rY      s    initialControlConnectionprotocolVersionlocalAddresshostNameexecutionProfilesconfiguredConnectionLengthheartbeatIntervalcompression)enabledcertValidationtype)r   versionarch)lengthmodel)r=   
event_loop)oscpusruntime)reconnectionPolicysslConfiguredauthProviderotherOptionsplatformInfoperiodicStatusIntervalrG   )Gr   rI   rJ   rR   _socketgetsocknamer.   r+   r,   r-   socketgetfqdnr   hostsr   ri   REMOTEIGNORED_compression_typeAttributeErrorssl_context
isinstancessl
SSLContextverify_modeCERT_REQUIREDOpenSSLr^   get_verify_modeVERIFY_NONEssl_optionsgetplatformunamer   rM   rN   sysmodules__version__rO   rP   rQ   application_nameapplication_version_endpoint_map_for_insightslistsetrH   	all_hostsr7   protocol_versionr   	serializerV   idle_heartbeat_intervalupperreconnection_policyboolauth_provider	__class__r$   systemreleasemachinemultiprocessing	cpu_count	processorrv   connection_classr   )r   rS   local_ipaddrr0   hostnamehost_distances_counterhost_distances_dictcompression_typecert_validationr^   
uname_infor   rX   r   r   v   s   
 

	

$	


z!MonitorReporter._get_startup_datac                 C   s    t d | j  |   d S )NzShutting down Monitor Reporter)r+   r,   r   r   joinrX   r   r   r   stop   s   

zMonitorReporter.stopN)r$   r%   r&   r   r#   r.   r'   r   r   r   r   r   r   r   r   r	   $   s    
$dr	   )collectionsr   rM   r)   loggingr   r   r   r   r   r   	threadingr   r   r   cassandra.policiesr   cassandra.utilr   $cassandra.datastax.insights.registryr   'cassandra.datastax.insights.serializersr   	getLoggerr$   r+   r	   r   r   r   r   <module>   s$   
