o
    Á¿ia  ã                   @   sD   d dl mZ d dlmZ d dlmZmZmZ G dd„ deeeƒZdS )é    )ÚApiError)Úformat_points)ÚSearchableAPIResourceÚSendableAPIResourceÚListableAPIResourcec                       sb   e Zd ZdZdZdZdZdZe‡ fdd„ƒZ	e
dd	„ ƒZed‡ fdd„	ƒZe‡ fdd„ƒZ‡  ZS )ÚMetricz*
    A wrapper around Metric HTTP API
    NÚqueryÚseriesÚmetricsc                    sJ   | j | _z
t|ƒ}d|i}W n ty   tdƒ‚w tt| ƒjdi |¤ŽS )zÍ
        Get a list of active metrics since a given time (Unix Epoc)

        :param from_epoch: Start time in Unix Epoc (seconds)

        :returns: Dictionary containing a list of active metrics
        Úfromz)Parameter 'from_epoch' must be an integerN© )Ú_METRIC_LIST_ENDPOINTÚ_resource_nameÚintÚ
ValueErrorr   Úsuperr   Úget_all)ÚclsÚ
from_epochÚsecondsÚparams©Ú	__class__r   úG/home/ubuntu/.local/lib/python3.10/site-packages/datadog/api/metrics.pyÚlist   s   
ÿzMetric.listc                 C   s   d| v r|   d¡| d< dS dS )zû
        FIXME DROPME in 1.0:

        API documentation was illegitimately promoting usage of `metric_type` parameter
        instead of `type`.
        To be consistent and avoid 'backward incompatibilities', properly rename this parameter.
        Úmetric_typeÚtypeN)Úpop)Úmetricr   r   r   Ú_rename_metric_type)   s   	ÿzMetric._rename_metric_typeTFc                    s¤   | j | _z4|r#|D ]}t|tƒr|  |¡ t|d ƒ|d< q	d|i}n|  |¡ t|d ƒ|d< |g}d|i}W n tyC   tdƒ‚w tt| ƒj	d||dœ|¤ŽS )a  
        Submit a metric or a list of metrics to the metric API
        A metric dictionary should consist of 5 keys: metric, points, host, tags, type (some of which optional),
        see below:

        :param metric: the name of the time series
        :type metric: string

        :param compress_payload: compress the payload using zlib
        :type compress_payload: bool

        :param metrics: a list of dictionaries, each item being a metric to send
        :type metrics: list

        :param points: a (timestamp, value) pair or list of (timestamp, value) pairs
        :type points: list

        :param host: host name that produced the metric
        :type host: string

        :param tags:  list of tags associated with the metric.
        :type tags: string list

        :param type: type of the metric
        :type type: 'gauge' or 'count' or 'rate' string

        >>> api.Metric.send(metric='my.series', points=[(now, 15), (future_10s, 16)])

        >>> metrics = [{'metric': 'my.series', 'type': 'gauge', 'points': [(now, 15), (future_10s, 16)]},
                {'metric': 'my.series2', 'type': 'gauge', 'points': [(now, 15), (future_10s, 16)]}]
        >>> api.Metric.send(metrics=metrics)

        :returns: Dictionary representing the API's JSON response
        Úpointsr	   z'points' parameter is required)Úattach_host_nameÚcompress_payloadNr   )
Ú_METRIC_SUBMIT_ENDPOINTr   Ú
isinstanceÚdictr   r   ÚKeyErrorr   r   Úsend)r   r
   r!   r"   Úsingle_metricr   Úmetrics_dictr   r   r   r'   5   s,   %

€

€ÿÿÿzMetric.sendc              
      sl   | j | _z| d¡|d< | d¡|d< W n ty* } z
td |jd ¡ƒ‚d}~ww tt| ƒj	di |¤ŽS )	a]  
        Query metrics from Datadog

        :param start: query start timestamp
        :type start: POSIX timestamp

        :param end: query end timestamp
        :type end: POSIX timestamp

        :param query: metric query
        :type query: string query

        :returns: Dictionary representing the API's JSON response

        *start* and *end* should be less than 24 hours apart.
        It is *not* meant to retrieve metric data in bulk.

        >>> api.Metric.query(start=int(time.time()) - 3600, end=int(time.time()),
                             query='avg:system.cpu.idle{*}')
        Ústartr   ÚendÚtozThe parameter '{0}' is requiredr   Nr   )
Ú_METRIC_QUERY_ENDPOINTr   r   r&   r   ÚformatÚargsr   r   Ú_search)r   r   Úer   r   r   r   q   s   €ÿzMetric.query)NTF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r#   r   Úclassmethodr   Ústaticmethodr   r'   r   Ú__classcell__r   r   r   r   r   
   s    
;r   N)	Údatadog.api.exceptionsr   Údatadog.api.formatr   Údatadog.api.resourcesr   r   r   r   r   r   r   r   Ú<module>   s   