o
    譐^                     @   sl   d 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mZ ddl	m
Z
 ddlZG d	d
 d
eZdS )z#Helper class for InfluxDB for v0.8.    )absolute_import)division)print_function)unicode_literals)
namedtupledefaultdict)warnNc                       sR   e Zd ZdZdZ fddZdd Zeddd	Zed
d Z	edd Z
  ZS )SeriesHelpera  Define the SeriesHelper object for InfluxDB v0.8.

    Subclassing this helper eases writing data points in bulk.
    All data points are immutable, ensuring they do not get overwritten.
    Each subclass can write to its own database.
    The time series names can also be based on one or more defined fields.

    Annotated example::

        class MySeriesHelper(SeriesHelper):
            class Meta:
                # Meta class stores time series helper configuration.
                series_name = 'events.stats.{server_name}'
                # Series name must be a string, curly brackets for dynamic use.
                fields = ['time', 'server_name']
                # Defines all the fields in this time series.
                ### Following attributes are optional. ###
                client = TestSeriesHelper.client
                # Client should be an instance of InfluxDBClient.
                :warning: Only used if autocommit is True.
                bulk_size = 5
                # Defines the number of data points to write simultaneously.
                # Only applicable if autocommit is True.
                autocommit = True
                # If True and no bulk_size, then will set bulk_size to 1.

    Fc              	      sF  | j sd| _ zt| d}W n ty   td| jw dD ]}zt| d| t|| W q ty>   td|| jw t|dd| _t|d	d
| _| jr[| js[td| jzt|d| _| jdk ru| jrut	d| j d| _W n ty   d| _Y nw | jst	d| j t
t| _t| j| j| _tt| | S )zInitialize class attributes for subsequent constructor calls.

        :note: *args and **kwargs are not explicitly used in this function,
        but needed for Python 2 compatibility.
        TZMetazMissing Meta class in {0}.)series_nameZfields_zMissing {0} in {1} Meta class.Z
autocommitFclientNz8In {0}, autocommit is set to True, but no client is set.Z	bulk_size   z<Definition of bulk_size in {0} forced to 1, was less than 1.zIDefinition of bulk_size in {0} has no affect because autocommit is false.)__initialized__getattrAttributeErrorformat__name__setattr_autocommit_client
_bulk_sizer   r   list_datapointsr   _fields_typesuperr	   __new__)clsargskwargsZ_metaattr	__class__ </usr/lib/python3/dist-packages/influxdb/influxdb08/helper.pyr   .   sb   

zSeriesHelper.__new__c                 K   s   | j }t|jt| krtd|j| |j|jjdi | |j	di | |j
rFtdd |j D |jkrH|  dS dS dS )zCreate a new data point.

        All fields must be present.

        :note: Data points written when `bulk_size` is reached per Helper.
        :warning: Data points are *immutable* (`namedtuples`).
        zExpected {0}, got {1}.c                 s   s    | ]}t |V  qd S N)len).0Zseriesr$   r$   r%   	<genexpr>u   s    z(SeriesHelper.__init__.<locals>.<genexpr>Nr$   )r#   sortedr   keys	NameErrorr   r   Z_series_nameappendr   r   sumvaluesr   commit)selfkwr   r$   r$   r%   __init__b   s    *zSeriesHelper.__init__Nc                 C   s$   |s| j }||  }|   |S )a  Commit everything from datapoints via the client.

        :param client: InfluxDBClient instance for writing points to InfluxDB.
        :attention: any provided client will supersede the class client.
        :return: result of client.write_points.
        )r   Zwrite_points_json_body__reset_)r   r   Zrtnr$   r$   r%   r0   y   s
   zSeriesHelper.commitc                    sN   g } j s	   t jD ]\}}|| j fdd|D d q|S )z[Return JSON body of the datapoints.

        :return: JSON body of the datapoints.
        c                    s    g | ]  fd dj D qS )c                    s   g | ]}t  |qS r$   )r   )r(   kZpointr$   r%   
<listcomp>   s    z7SeriesHelper._json_body_.<locals>.<listcomp>.<listcomp>)r   )r(   r   r7   r%   r8      s    z,SeriesHelper._json_body_.<locals>.<listcomp>)namecolumnsZpoints)r   r5   sixZ	iteritemsr   r-   r   )r   Zjsonr
   datar$   r9   r%   r4      s   

zSeriesHelper._json_body_c                 C   s   t t| _dS )zReset data storage.N)r   r   r   r9   r$   r$   r%   r5      s   zSeriesHelper._reset_r&   )r   
__module____qualname____doc__r   r   r3   classmethodr0   r4   r5   __classcell__r$   r$   r"   r%   r	      s    4
r	   )r@   Z
__future__r   r   r   r   collectionsr   r   warningsr   r<   objectr	   r$   r$   r$   r%   <module>   s   