o
    -7_                     @   sx   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
mZ dd	lZG d
d deZd	S )zHelper class for InfluxDB.    )absolute_import)division)print_function)unicode_literals)
namedtupledefaultdict)datetime)warnNc                       s^   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
edd Z  ZS )SeriesHelpera  Subclass 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.
    The field "time" can be specified when creating a point, and may be any of
    the time types supported by the client (i.e. str, datetime, int).
    If the time is not specified, the current system time (utc) will be used.

    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.
                retention_policy = 'your_retention_policy'
                # Specify the retention policy for the data points
                time_precision = "h"|"m"|s"|"ms"|"u"|"ns"
                # Default is ns (nanoseconds)
                # Setting time precision while writing point
                # You should also make sure time is set in the given precision

    Fc              	      s  | 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
| _g d}| j|vrbtd| jd|t|dd
| _	t|dd
| _
| jr~| j
s~td| jzt|d| _| jdk r| jrtd| j d| _W n ty   d| _Y nw | jstd| j tt| _d| jv r| jd t| jdg| j | j | _dt| j | 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_namefieldstags_zMissing {0} in {1} Meta class.Z
autocommitFtime_precisionN)hmsmsunsNz8In {}, time_precision is set, but invalid use any of {}.,retention_policyclientz8In {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.timeN)__initialized__getattrAttributeErrorformat__name__setattr_autocommit_time_precisionjoin_retention_policy_client
_bulk_sizer	   r   list_datapoints_fieldsremover   _tags_typelen__new____defaults__superr
   )clsargskwargsZ_metaattrZallowed_time_precisions	__class__ 1/usr/lib/python3/dist-packages/influxdb/helper.pyr0   7   s|   



zSeriesHelper.__new__c                 K   s   | j }|d|  }t|j}t|j}t| }||ks*td|j| || |ks;td|| | |j	|j
jdi | |jdd|i| |jrjtdd |j	 D |jkrl|  dS dS dS )zCall to constructor creates a new data point.

        :note: Data points written when `bulk_size` is reached per Helper.
        :warning: Data points are *immutable* (`namedtuples`).
        r   z<Expected arguments to contain all tags {0}, instead got {1}.z(Got arguments not in tags or fields: {0}c                 s   s    | ]}t |V  qd S r   )r/   ).0Zseriesr9   r9   r:   	<genexpr>   s    z(SeriesHelper.__init__.<locals>.<genexpr>Nr9   )r8   pop_current_timestampsetr-   r+   keys	NameErrorr    r*   Z_series_nameappendr.   r#   sumvaluesr(   commit)selfkwr3   Z	timestampr   r   r@   r9   r9   r:   __init__y   s0   

zSeriesHelper.__init__Nc                 C   s.   |s| j }|j|  | j| jd}|   |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   r   )r'   Zwrite_points_json_body_r$   r&   _reset_)r3   r   Zrtnr9   r9   r:   rE      s   zSeriesHelper.commitc           	      C   s   g }| j s	|   t| jD ]<\}}|D ]5}|i i t|dd}| jD ]}t||}|dur5||d |< q$| jD ]}t|||d |< q9|| qq|S )zcReturn the JSON body of given datapoints.

        :return: JSON body of these datapoints.
        r   )Zmeasurementr   r   r   Nr   r   )	r   rJ   sixZ	iteritemsr*   r   r+   r-   rB   )	r3   Zjsonr   dataZpointZ
json_pointZfieldvaluetagr9   r9   r:   rI      s(   


zSeriesHelper._json_body_c                 C   s   t t| _dS )zReset data storage.N)r   r)   r*   )r3   r9   r9   r:   rJ      s   zSeriesHelper._reset_c                   C   s   t  S r   )r   Zutcnowr9   r9   r9   r:   r>      s   zSeriesHelper._current_timestampr   )r!   
__module____qualname____doc__r   r0   rH   classmethodrE   rI   rJ   staticmethodr>   __classcell__r9   r9   r7   r:   r
      s    $B

r
   )rQ   Z
__future__r   r   r   r   collectionsr   r   r   warningsr	   rK   objectr
   r9   r9   r9   r:   <module>   s   