U
    h                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m	Z	m
Z
 d dlZeeZe Zedeefeedd	d
ZdS )    )	APIRouterDepends)Session)text)get_db)datetimetimezoneNz(/api/debug/activity-check/{developer_id})developer_iddbc              
      s2  z| tdd| i }|s@d|  d| td dW S | tdd| i }| tdd| i }| td	d| i }| d
|r|d nd|d r|d  nd|d r|d  ndddd |D dd |D dW S  tk
r, } z$td|  dt|i W Y S d}~X Y nX dS )z5Debug endpoint to check activity data for a developerzKSELECT * FROM developers WHERE developer_id = :dev_id OR id::text = :dev_idZdev_idzDeveloper 'z' not foundz)SELECT developer_id, name FROM developers)errorZavailable_developersz
                SELECT COUNT(*), 
                       MIN(timestamp) as first_activity,
                       MAX(timestamp) as last_activity
                FROM activity_records 
                WHERE developer_id = :dev_id
            a8  
                SELECT category, COUNT(*) as count, SUM(duration) as total_duration
                FROM activity_records 
                WHERE developer_id = :dev_id
                  AND timestamp >= NOW() - INTERVAL '7 days'
                GROUP BY category
                ORDER BY count DESC
            z
                SELECT application_name, category, duration, timestamp
                FROM activity_records 
                WHERE developer_id = :dev_id
                ORDER BY timestamp DESC
                LIMIT 10
            Tr      N   )Zfirst_activityZlast_activityc              	   S   sH   g | ]@}|d  pd|d t |d p$d tt |d p4d d ddqS )r   Zuncategorizedr   r   i  )categorycountZduration_secondsZduration_hours)floatround).0cat r   ./debug_activity_endpoint.py
<listcomp>N   s   
z(debug_activity_check.<locals>.<listcomp>c                 S   s:   g | ]2}|d  |d |d |d r.|d   nddqS )r   r   r      N)Zappr   ZdurationZ	timestamp)	isoformat)r   Zsampler   r   r   r   W   s   )r	   Zdeveloper_existsZtotal_activitiesZ
date_rangeZlast_7_days_categoriesZrecent_activitieszDebug check error: r   )	Zexecuter   ZfetchoneZfetchallr   	Exceptionloggerr   str)r	   r
   Z	dev_checkZactivity_count
categoriesZsampleser   r   r   debug_activity_check   sN    
	r   )Zfastapir   r   Zsqlalchemy.ormr   Z
sqlalchemyr   Zdatabaser   r   r   ZloggingZ	getLogger__name__r   Zroutergetr   r   r   r   r   r   <module>   s   
