o
    #jy$                     @   s   d 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 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 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 )z/
This module provides a client class for TSDB.
    N)bce_client_configuration)utils)bce_v1_signer)BceBaseClient)bce_http_client)handler)http_content_types)http_headers)http_methods)tsdb_handlerc                   @   s   e Zd ZdZd!ddZd"ddZdd	 Zd
d Zdd Zd#ddZ	dd Z
							d$ddZ						d%ddZ							d&ddZdd Zdd Z					d'dd ZdS )(
TsdbClientz
    sdk client
    Nc                 C   s   || _ t| | d S N)databaser   __init__)selfconfigr    r   c/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/baidubce/services/tsdb/tsdb_client.pyr   +   s   zTsdbClient.__init__Tc                 C   sT   d}t d|id}tjtji}|r| |}d|tj< | j	t
j|||tjdS )z
        write datapoints

        :param datapoints: a list of datapoint dict
        :type datapoints: list
        :param use_gzip: open gzip compress
        :type use_gzip: boolean
           /v1/datapoint
datapointszutf-8s   gzip)pathbodyheadersbody_parser)jsondumpsencoder	   CONTENT_TYPEr   JSON_gzip_compressZCONTENT_ENCODING_send_requestr
   POSTr   
parse_json)r   r   Zuse_gzipr   r   r   r   r   r   write_datapoints/   s   


zTsdbClient.write_datapointsc                 C   s   d}| j tj|tjdS )zs
        list metrics

        :return: a list of metric
        :rtype: baidubce.bce_response.BceResponse
        s
   /v1/metricr   r   r    r
   GETr   r"   )r   r   r   r   r   get_metricsG   s   zTsdbClient.get_metricsc                 C   *   t |}d| d }| jtj|tjdS )z
        get fields

        :type metric: string
        :param metric:

        :return: field dict. {field1:{type: 'Number'},field2:{type: 'String'}}
        :rtype: baidubce.bce_response.BceResponse
           /v1/metric/s   /fieldr$   r   Zconvert_to_standard_stringr    r
   r&   r   r"   r   Zmetricr   r   r   r   
get_fieldsR      

zTsdbClient.get_fieldsc                 C   r(   )z
        get tags

        :type metric: string
        :param metric:

        :return: {tagk1:[tagk11,tagk21,..],tagk2:[tagk21,tagk22,..]..}
        :rtype: baidubce.bce_response.BceResponse
        r)   s   /tagr$   r*   r+   r   r   r   get_tags`   r-   zTsdbClient.get_tagsFc                 C   s4   d}d|d}t d|i}| jtj|||tjdS )aB  
        query datapoints

        :param query_list: a list of query dict
        :type query_list: list
        :param disable_presampling: open of close presampling result query
        :type disable_presampling: boolean

        :return: a list of result dict
        :rtype: baidubce.bce_response.BceResponse
        r    queryZdisablePresamplingqueries)r   paramsr   r   )r   r   r    r
   PUTr   r"   )r   
query_listdisable_presamplingr   r3   r   r   r   r   get_datapointsn   s   
zTsdbClient.get_datapointsc                 C   s"   d}d|i}| j tj||tjdS )z
        get_rows_with_sql

        :param statement: sql statement
        :type statement: string

        :return: {rows:[[],[],...], columns: []}
        :rtype: baidubce.bce_response.BceResponse
           /v1/rowsql)r   r3   r   r%   )r   	statementr   r3   r   r   r   get_rows_with_sql   s
   zTsdbClient.get_rows_with_sqlr     c	              
   C   s0   d}	t d|i|d}
| |	|||
||||S )4  
        Get an authorization url with expire time

        :type timestamp: int
        :param timestamp: None

        :type expiration_in_seconds: int
        :param expiration_in_seconds: None

        :type options: dict
        :param options: None

        :return:
            **URL string**
        r   r2   r0   )r   r   _generate_pre_signed_url)r   r5   	timestampexpiration_in_secondsr6   r   headers_to_signprotocolr   r   r3   r   r   r   generate_pre_signed_url   s   

z"TsdbClient.generate_pre_signed_urlc           
   
   C   s$   d}d|i}	|  ||||	||||S )a,  
        Get an authorization url with sql

        :type timestamp: int
        :param timestamp: None

        :type expiration_in_seconds: int
        :param expiration_in_seconds: None

        :type options: dict
        :param options: None

        :return:
            **URL string**
        r8   r9   )r>   )
r   r:   r?   r@   r   rA   rB   r   r   r3   r   r   r    generate_pre_signed_url_with_sql   s
   

z+TsdbClient.generate_pre_signed_url_with_sqlc	              
   C   s   |  |}|pi }|pi }t|j|j\}	}
}|p|	}|
}||jjkr,|dt| 7 }||tj< t	
|jtj|||||||tj < d|j| | t|d f S )r=      :z%s://%s%s?%sF)_merge_configr   Zparse_host_portZendpointrB   default_portstrr	   ZHOSTr   signcredentialsr
   r&   ZAUTHORIZATIONlowernamedecodeZget_canonical_querystring)r   r   r?   r@   r3   r   rA   rB   r   Zendpoint_protocolZendpoint_hostZendpoint_portZ	full_hostr   r   r   r>      s4   



z#TsdbClient._generate_pre_signed_urlc                 C   sN   t  }tj|dd}|| W d    | S 1 sw   Y  | S )Nw)fileobjmode)ioBytesIOgzipGzipFilewritegetvalue)r   rH   outfr   r   r   r     s   
zTsdbClient._gzip_compressc                 C   s(   |d u r| j S t| j }|| |S r   )r   copyZmerge_non_none_values)r   r   Z
new_configr   r   r   rF     s
   
zTsdbClient._merge_configc              
   C   sr   |  |}|d u rtjtji}|d u rtj}| jd ur)|d u r!i }|d| ji t	
|tjtj|g|||||S )Ns   database)rF   r	   r   r   r   r   r"   r   updater   send_requestr   rI   Zparse_error)r   Zhttp_methodr   r   r   r3   r   r   r   r   r   r      s   


zTsdbClient._send_requestr   )T)F)r   r<   FNNNN)r   r<   NNNN)r   r<   NNNNN)NNNNN)__name__
__module____qualname____doc__r   r#   r'   r,   r.   r7   r;   rC   rD   r>   r   rF   r    r   r   r   r   r   '   sP    



#

3
r   )r_   rQ   rY   r   loggingrS   Zbaidubcer   r   Zbaidubce.authr   Zbaidubce.bce_base_clientr   Zbaidubce.httpr   r   r   r	   r
   Zbaidubce.services.tsdbr   	getLoggerr\   _loggerr   r   r   r   r   <module>   s$   
