o
    ;i                     @   sZ  d dl m Z  d dlmZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ dd	lmZ ejd
dddZdZejddde
jejddde dejddde ddejddddejdddddejddddejddd d!dejd"d#d$dejd"d%d&dfd'ee d(ee d)ee d*ed+ee d,ee d-ed.efd/dZdS )0    )datetime)dumps)OptionalN)_workspace_billing_report)synchronizer)
parse_dateparse_date_rangeresolve_timezone   )display_tablebillingz#View workspace billing information.T)namehelpno_args_is_helpz\Date (in UTC by default): ISO format (2025-01-01) or relative (yesterday, 3 days ago, etc.).report)r   z--startzStart date. )r   z--endz
End date. z Defaults to now.z--forzRConvenience range: today, yesterday, this week, last week, this month, last month.dz--resolutionz-rz-Time resolution: 'd' (daily) or 'h' (hourly).z--tzzlTimezone for date interpretation: 'local', offset (5, -4, +05:30), or IANA name. Requires hourly resolution.z--tag-namesz-tz-Comma-separated list of tag names to include.Fz--jsonzOutput as JSON.z--csvzOutput as CSV.startendfor_
resolutiontz	tag_namesjsoncsvc              
      s  r
 r
t dd|dur*zt|W n ty) } zt t|d}~ww |durY| dus6|dur;t dz
t|d\}	}
W nD tyX } zt t|d}~ww | durzt| d}	|rlt|dnd}
W n ttfy } zt t|d}~ww t d|rdd |dD nd}d	vrt d
durdkrt dt	|	|
|dI dH }g d}|r|
d dtdtf fdd}g }|D ]'}|d |d |d ||d t|d g}|r|
t|d  |
| qt|| d dS )an  Generate a billing report for the workspace.

    The report range can be provided by setting `--start` / `--end` dates (`--end` defaults to 'now')
    or by requesting a date range using `--for` (e.g., `--for today`, `--for 'last month'`).

    Note that, as with the Billing API, the start date is inclusive and the end date is exclusive.
    Data will be reported for full intervals only. Using `--for` is a convenient way to define a
    complete interval.

    Examples:

        modal billing report --start 2025-12-01 --end 2026-01-01

        modal billing report --for "last month" --tag-names team,project

        modal billing report --for today --resolution h

        modal billing report --for yesterday -r h --tz local

        modal billing report --for "last month" --csv > report.csv

        modal billing report --start 2025-12-01 --json > report.json

    z'--json and --csv are mutually exclusiveNz2--for is mutually exclusive with --start and --end)r   z#Either --for or --start is requiredc                 S   s   g | ]}|  qS  )strip).0tr   r   E/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/billing.py
<listcomp>b   s    zreport.<locals>.<listcomp>,)r   hz.Resolution must be 'd' (daily) or 'h' (hourly)r!   z--tz requires hourly resolution (--resolution h / -r h). Daily intervals are UTC-aligned and cannot be shifted to a custom timezone.)r   r   r   r   )z	Object IDDescriptionEnvironmentzInterval StartCostTagsdtreturnc                    sL   d ur
|  } n| jd d} s r|  S dkr!| dS | dS )N)tzinfor   z%Y-%m-%dz%Y-%m-%dT%H:%M)
astimezonereplace	isoformatstrftime)r&   r   r   r   resolved_tzr   r   format_interval|   s   

zreport.<locals>.format_interval	object_iddescriptionenvironment_nameinterval_startcosttags)r   r   )click
UsageErrorr	   
ValueErrorstrr   r   OverflowErrorsplitr   appendr   r   r   )r   r   r   r   r   r   r   r   excstart_dtend_dtr5   	rows_datacolumnsr/   rowsitemrowr   r-   r   r      st   0






)r   r   r   typingr   r6   typermodal._billingr   modal._utils.async_utilsr   modal._utils.time_utilsr   r   r	   utilsr   Typerbilling_cli	DATE_HELPcommandcreate_blockingOptionr9   boolr   r   r   r   r   <module>   s\   