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_id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   )first_activitylast_activityc              	   S   sH   g | ]@}|d  pd|d t |d p$d tt |d p4d d ddqS )r   uncategorizedr   r   i  )categorycountduration_secondsduration_hours)floatround).0cat r   :/var/www/html/timesheet/backend/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)appr   duration	timestamp)	isoformat)r   sampler   r   r   r   W   s   )r	   Zdeveloper_existstotal_activities
date_rangeZlast_7_days_categoriesrecent_activitieszDebug check error: r   )	executer   fetchonefetchallr!   	Exceptionloggerr   str)r	   r
   Z	dev_checkactivity_count
categoriesZsampleser   r   r   debug_activity_check   sN    
	r/   )fastapir   r   sqlalchemy.ormr   
sqlalchemyr   databaser   r   r   logging	getLogger__name__r*   routergetr+   r/   r   r   r   r   <module>   s   
