o
    ;i2                     @   s   d dl m Z mZ d dlmZ d dlmZmZmZ d dlm	Z	 ddl
mZ ddlmZ G dd	 d	eZd
dd
d
dde dee  dedeee  dee deeeef  fddZd
S )    )datetimetimezone)Decimal)AnyOptional	TypedDict)api_pb2   )_Client)InvalidErrorc                   @   sF   e Zd ZU eed< eed< eed< eed< eed< eeef ed< dS )WorkspaceBillingReportItem	object_iddescriptionenvironment_nameinterval_startcosttagsN)__name__
__module____qualname__str__annotations__r   r   dict r   r   B/home/ubuntu/.local/lib/python3.10/site-packages/modal/_billing.pyr      s   
 r   Nd)end
resolution	tag_namesclientstartr   r   r   r   returnc           	         s  |du rt  I dH }|pg }|du rttj}| jdu r'| jtjd} n
| jtjkr1td|jdu r>|jtjd}n
|jtjkrHtdt	j
||d}|j|  |j| g }|jj|2 z%3 dH W }|j|j|j|j jtjdt|jt|jd}|| qd6 |S )a  Generate a tabular report of workspace usage by object and time.

    The result will be a list of dictionaries for each interval (determined by `resolution`)
    between the `start` and `end` limits. The dictionary represents a single Modal object
    that billing can be attributed to (e.g., an App) along with metadata (including user-defined
    tags) for identifying that object.

    The `start` and `end` parameters are required to either have a UTC timezone or to be
    timezone-naive (which will be interpreted as UTC times). The timestamps in the result will
    be in UTC. Cost will be reported for full intervals, even if the provided `start` or `end`
    parameters are partial: `start` will be rounded to the beginning of its interval, while
    partial `end` intervals will be excluded.

    Additional user-provided metadata can be included in the report if the objects have tags
    and `tag_names` (i.e., keys) are specified in the request. Alternatively, pass `tag_names=["*"]`
    to include all tags in the report. Note that tags will be attributed to the entire interval even
    if they were added or removed at some point within it. If the tag name was not in use during an
    interval, it will be absent from the tags dictionary in that output row.

    In most cases, billing data will be available in the database that this API queries within
    minutes, although there may be collection delays. If completeness is important for your use
    case, we recommend leaving a buffer after the end of the query interval.

    N)tzinfoz0Timezone-aware 'start' parameter must be in UTC.z.Timezone-aware 'end' parameter must be in UTC.)r   r   )r   r   r   r   r   r   )r
   from_envr   nowr   utcr"   replacer   r   WorkspaceBillingReportRequeststart_timestampFromDatetimeend_timestampstubWorkspaceBillingReportunary_streamr   r   r   interval
ToDatetimer   r   r   r   append)	r    r   r   r   r   requestrowspb_itemitemr   r   r   _workspace_billing_report   s@    

r5   )r   r   decimalr   typingr   r   r   modal_protor   r   r
   	exceptionr   r   r   listr   r5   r   r   r   r   <module>   s0   
