o
    va                     @   sT   d 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	m
Z
 G dd deZdS )	z1InfluxDB (from to InfluxDB 1.8+) interface class.    N)node)logger)GlancesExport)InfluxDBClientWriteOptionsc                       s:   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Z  ZS )Exportz.This class manages the InfluxDB export module.Nc                    s   t t| j||d d| _d| _d| _d| _d| _d| _d| _	| j
dg dg dd| _| js4td t d	d
 | _	|  | _dS )zInit the InfluxDB export IF.)configargsNZhttpZ	influxdb2)hostportuserZpasswordorgbuckettoken)protocolprefixtags)Zmandatoriesoptions   .r   )superr   __init__r   r   r   r   r   r   hostnameZ	load_confexport_enablesysexitr   splitinitclient)selfr   r	   	__class__ C/usr/lib/python3/dist-packages/glances/exports/glances_influxdb2.pyr   "   s    
zExport.__init__c              
   C   s   | j sdS d| j| j| j}zt|dd| j| jd}W n  ty< } zt	
d||f  td W Y d}~nd}~ww t	d| j| j |jtdd	d
dddddd}|S )z+Init the connection to the InfluxDB server.Nz
{}://{}:{}F)urlZenable_gzipZ
verify_sslr   r   z+Cannot connect to InfluxDB server '%s' (%s)r   z,Connected to InfluxDB server version {} ({})i  i'  i  i     i0u  )Z
batch_sizeZflush_intervalZjitter_intervalZretry_intervalZmax_retriesZmax_retry_delayZexponential_base)Zwrite_options)r   formatr   r
   r   r   r   r   	Exceptionr   Zcriticalr   r   infoZhealthversionmessageZ	write_apir   )r   r$   r   eZwrite_clientr"   r"   r#   r   B   s6   

zExport.initc           	         s2  g }t t|| dd |D }t|dkrdg}|D ]{dur, fdd D }n }|D ]4}|| du r9q0z
t|| ||< W q0 ttfyd   z
t|| ||< W n ttfya   Y nw Y q0w | | j}d|v r|d |v rt||d  ||d < |	|d  | j
|d< ||||d	 q|S )
z-Normalize data for the InfluxDB's data model.c                 S   s$   g | ]}| d r|dd qS )z.keyr   r   )endswithr   .0kr"   r"   r#   
<listcomp>i   s   $ z%Export._normalize.<locals>.<listcomp>r   Nc                    s4   i | ]}| d r|d d | qS )z{}. )
startswithr&   replacer-   Z	data_dictmeasurementr"   r#   
<dictcomp>p   s    z%Export._normalize.<locals>.<dictcomp>keyr   )r5   r   fields)dictziplenfloat	TypeError
ValueErrorstrZ
parse_tagsr   popr   append)	r   namecolumnspointsretZ	keys_listr8   r/   r   r"   r4   r#   
_normalize`   sD   

zExport._normalizec              
   C   s   | j dur| j d | }t|dkrtd| dS z| jj| j| j| 	|||dd W n t
yK } ztd|| W Y d}~dS d}~ww td| dS )	z(Write the points to the InfluxDB server.Nr   r   z(Cannot export empty {} stats to InfluxDBs)Ztime_precisionz'Cannot export {} stats to InfluxDB ({})zExport {} stats to InfluxDB)r   r;   r   debugr&   r   writer   r   rF   r'   )r   rB   rC   rD   r+   r"   r"   r#   export   s   


 zExport.export)NN)	__name__
__module____qualname____doc__r   r   rF   rJ   __classcell__r"   r"   r    r#   r      s     4r   )rN   r   platformr   Zglances.loggerr   Zglances.exports.glances_exportr   Zinfluxdb_clientr   r   r   r"   r"   r"   r#   <module>   s   