o
    ̿Si                     @   sv   d dl m Z mZ d dlmZ dd Zde de fddZde de fd	d
ZdededefddZ	dde de fddZ
dS )    )datetime	timedelta)
Collectionc                    s   | d}|std| d d|i}| d |dd}| d |dd}t }|td	d
 }d|dd|ididdddidig}t| d |}	|	rX|	d d nd}
||||
| dS )z)Return high-level metrics for the tenant.
account_idz&Account ID not found for current user.userssubscriptionsactive)r   statusprojectsACTIVE   days$matchpaid$gter   r	   due_date$groupN$sum$amount)_idtotalRevenuepaymentsr   r   )users_countactive_subscriptionsactive_projectstotal_revenuelast_updated)	get
ValueErrorcount_documentsr   utcnowr   list	aggregate	isoformat)dbcurrent_userr   r   r   r   nowone_month_agopipelinerevenue_resultr    r,   </var/www/html/Trade-python/app/v1/services/saas/dashboard.pyget_overview_metrics   s(   
r.   
start_dateend_datec                    s\   | d}d|||ddiddddd	id
dididddiig}t| d |}|S )z>Return daily usage metrics (e.g., user logins) grouped by day.r   r   r   z$lte)r   
login_dater   $dateToString%Y-%m-%dz$login_dateformatdater      )r   daily_logins$sortr   user_loginsr   r#   r$   )r&   r'   r/   r0   r   r*   usager,   r,   r-   get_usage_trends    s   

r>   c                    s^   | d}d|d||ddidddd	d
iddididddiig}t| d |}|S )z%Return revenue trends grouped by day.r   r   r   r1   r   r   r3   r4   z	$due_dater5   r   r   )r   daily_revenuer:   r   r8   r   r<   )r&   r'   r/   r0   r   r*   revenue_trendsr,   r,   r-   get_financial_trends1   s   

rA   search_queryskiplimitc                    s\   dd|ddid|ddigi}| d  |||}t|}| d |}||dS )z'Perform account search with pagination.z$oraccount_namei)z$regexz$optionsemailaccounts)total_countrH   )findrC   rD   r#   r!   )r&   rB   rC   rD   queryaccounts_cursorrH   rI   r,   r,   r-   search_accountsC   s   
rM   Nc           	         s   t  }|du r|tdd }|du r|}t| |I dH }t| |||I dH }t| |||I dH }|||| | dd}|S )zIAggregate dashboard data including overview, usage, and financial trends.Nr   r   )startend)overviewusage_trendsr@   
date_range)r   r"   r   r.   r>   rA   r%   )	r&   r'   r/   r0   r(   rP   r=   revenue	dashboardr,   r,   r-   get_dashboard_metrics_serviceO   s   rU   )NN)r   r   pymongo.collectionr   r.   r>   rA   strintrM   rU   r,   r,   r,   r-   <module>   s    