o
    AhW                     @   s   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 Z
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_id&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/moveengine/app/v1/services/saas/dashboard.pyget_overview_metrics   s(   
r0   
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)   r1   r2   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   r3   r   r   r5   r6   z	$due_dater7   r   r   )r   daily_revenuer<   r   r:   r   r>   )r(   r)   r1   r2   r   r,   revenue_trendsr.   r.   r/   get_financial_trends1   s   

rC   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_countrJ   )findrE   rF   r%   r#   )r(   rD   rE   rF   queryaccounts_cursorrJ   rK   r.   r.   r/   search_accountsC   s   
rO   c                    s   | d}|stdt }| d |dd}| d |dd}| d |dd}| d |d	d
}||||| dS )zCReturn workforce, fleets, vendors, and users counts for the tenant.r   r   	workforcer	   r
   fleetsvendorsr   T)r   	is_active)workforce_countfleets_countvendors_countr   r    )r!   r"   r   r$   r#   r'   )r(   r)   r   r*   rT   rU   rV   r   r.   r.   r/   &get_workforce_fleet_vendor_user_countsO   s6   
rW   c           	         s   | d}| d |d||dd}| d |d||dd}| d |d||dd}| d |d	||dd
}||||dS )Nr   rP   r	   r3   )r   r   created_daterQ   rR   r   T)r   rS   rX   )rT   rU   rV   r   )r!   r#   )	r(   r)   r1   r2   r   rT   rU   rV   r   r.   r.   r/   get_workforce_fleet_summaryt   s6   
rY   Nc           
         s   t  }|du r|tdd }|du r|}t| |I dH }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_trendsrB   workforce_fleet_trends
date_range)r   r$   r   r0   r@   rC   rW   r'   )
r(   r)   r1   r2   r*   r\   r?   revenueworkforce_fleet	dashboardr.   r.   r/   get_dashboard_metrics_service   s"   rc   )NN)r   r   pymongo.collectionr   r0   r@   rC   strintrO   rW   rY   rc   r.   r.   r.   r/   <module>   s    %&