o
    va                     @   s\   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	 ddl
mZ G dd	 d	eZdS )
z0InfluxDB (up to InfluxDB 1.7.x) interface class.    N)node)logger)GlancesExport)InfluxDBClient)InfluxDBClientErrorc                       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httpinfluxdb)hostportuserpassworddb)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__ B/usr/lib/python3/dist-packages/glances/exports/glances_influxdb.pyr   #   s    
zExport.__init__c              
   C   s   | j sdS | jdur| j dkrd}nd}zt| j| j|d| j| j| jd}dd |	 D }W n! t
yR } ztd| j|f  td	 W Y d}~nd}~ww | j|v rctd
|j |S td| j  td	 |S )z+Init the connection to the InfluxDB server.NZhttpsTF)r   r   sslZ
verify_sslZusernamer   Zdatabasec                 S   s   g | ]}|d  qS )namer"   ).0ir"   r"   r#   
<listcomp>V   s    zExport.init.<locals>.<listcomp>z-Cannot connect to InfluxDB database '%s' (%s)r   z-Stats will be exported to InfluxDB server: {}z6InfluxDB database '%s' did not exist. Please create it)r   r   lowerr   r   r   r   r   r   Zget_list_databaser   r   Zcriticalr   r   infoformatZ_baseurl)r   r$   r   Z
get_all_dber"   r"   r#   r   C   s8   


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 )
zWNormalize data for the InfluxDB's data model.
        Output is a list of measurements.c                 S   s$   g | ]}| d r|dd qS )z.keyr   r   )endswithr   r&   kr"   r"   r#   r(   n   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>u   s    z%Export._normalize.<locals>.<dictcomp>keyr   )r4   r   fields)dictziplenfloat	TypeError
ValueErrorstrZ
parse_tagsr   popr   append)	r   r%   columnspointsretZ	keys_listr7   r/   r   r"   r3   r#   
_normalized   sD   

zExport._normalizec              
   C   s   | j dur| j d | }t|dkrtd| dS z| jj| |||dd W n tyG } 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   Zwrite_pointsrD   	Exception)r   r%   rA   rB   r,   r"   r"   r#   export   s   

 zExport.export)NN)	__name__
__module____qualname____doc__r   r   rD   rH   __classcell__r"   r"   r    r#   r       s     !5r   )rL   r   platformr   Zglances.loggerr   Zglances.exports.glances_exportr   r
   r   Zinfluxdb.clientr   r   r"   r"   r"   r#   <module>   s   