
    Ji_!                        d dl mZmZmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZmZmZ d dlmZ d dlmZmZ d dlZ ej,                  e      Z e       Zej5                  d	       ee      fd
efd       Zej5                  d      d ee      fdeded
efd       Zej5                  d       ee      fd
efd       Zy)    )	APIRouterDependsHTTPException)Session
joinedload)funcStringcast)List)datetimetimezone	timedelta)get_db)	DeveloperActivityRecordNz/api/developers-ormdbc           
        K   	 ddl m}  |d      }| j                  |      }|j                         }g }|D ]B  }|\	  }}}	}
}}}}}d}d}|r|}t	        |d      rEt        j                  t        j                        |j                  t        j                        z
  }n&t        j                  t        j                        |z
  }|j                         dk  rd	}n|j                         d
k  rd}|j                  i d|xs d| d|	d|	ddddd|dddd| dd|xs d| d|d|r|j                         n|r|j                         ndddddd |
d!|r|j                         ndd"|       E |d#t        |      t        j                  t        j                        j                         d$d%S # t        $ r3}t        j!                  d&|        t#        d't%        |      (      d}~ww xY ww))z:Get all developers using SQLAlchemy ORM with relationshipsr   texta  
            SELECT 
                d.id,
                d.developer_id,
                d.name,
                d.email,
                d.active,
                d.created_at,
                d.last_sync,
                COALESCE(ac.activity_count, 0) as activity_count,
                ac.last_activity
            FROM developers d
            LEFT JOIN (
                SELECT 
                    developer_id::VARCHAR as developer_id,
                    COUNT(*) as activity_count,
                    MAX(timestamp) as last_activity
                FROM activity_records
                WHERE developer_id IS NOT NULL
                GROUP BY developer_id
            ) ac ON d.developer_id::VARCHAR = ac.developer_id::VARCHAR
            WHERE d.active = true
        offlineNreplacetzinfo  onlineQ idleiddev_namehostnamehostunknownporti  statussourcedatabasedescriptionzDeveloper with z activities	device_idactivity_count	last_seenversionzN/Abucket_countemail
created_atactive
productionorm_relationships)
developersenvironmenttotal_countdiscovered_atmethodz"Error getting developers via ORM:   status_codedetail)
sqlalchemyr   executefetchallhasattrr   nowr   utcr   total_secondsappend	isoformatlen	Exceptionloggererrorr   str)r   r   queryresultdevelopers_with_statsdeveloper_listrowdev_iddeveloper_idr    r.   r0   r/   	last_syncr*   last_activityr%   r+   	time_diffes                       8E:\timesheet\timesheet_new\backend\developers_orm_api.pyget_developers_using_ormrV      s{    U<#  0 E" & 1(C FICV\4NM FI)	9i0 (X\\ :Y=N=NV^VbVb=N=c cI (X\\ :Y FI**,t3%F,,.6#F!! #l5VHo## D# 		#
 # &# *# 0@L# \<tF8_# !.# iY002.8J((*d# 5# #  !#" 
j224##$ &%# + )V )'~.%\\(,,7AAC)
 	
  <9!=>CF;;<s)   G<F8F= <G<=	G9.G44G99G<z(/api/developer/{developer_id}/activitiesd   rP   limitc                   K   	 |j                  t              j                  t        j                  | k(        j	                         }|st        dd      |j                  t        t        j                  j                  d            j                  t        t        j                  t        j                  k(  d      j                  t        j                  | k(        j                  t        j                  j                               j                  |      j                         }g }|D ]  \  }}|j                  |j                   |j                  ||j"                  |j$                  |j&                  xs d|j(                  |j                  r|j                  j+                         nd|j,                  |j.                  |j0                  d	        |j                  |j                  |j2                  d
|t5        |      dS # t
        $ r  t6        $ r3}t8        j;                  d|        t        dt=        |            d}~ww xY ww)z1Get activities for a specific developer using ORMi  zDeveloper not foundr9   developer_nameT)isouterOtherN)r   rP   rZ   application_namewindow_titlecategoryduration	timestampurl	file_pathproject_name)r   r    r.   )	developer
activitiestotal_activitiesz$Error getting developer activities: r8   )rJ   r   filterrP   firstr   r   r    labeljoinorder_byra   descrX   allrC   r   r]   r^   r_   r`   rD   rb   rc   rd   r.   rE   rF   rG   rH   rI   )	rP   rX   r   re   rf   activities_listactivityrZ   rT   s	            rU   get_developer_activitiesrq   i   s    5<HHY'..""l2

%' 	 C8MNN XXNN  !12
 $''9+A+AA  
 &''<7

($$))+

%,ssu 	 (2$Hn""kk ( 5 5"0$,$=$= ( 5 5$--8$--?G?Q?QX//99;W[||%// ( 5 5$  )3"  ,,!"
 * #O 4
 	
   <;A3?@CF;;<s)   IG?H II	.II		Iz/api/developers-with-statsc                   K   	 ddl m}  |d      }| j                  |      }|j                         }g }|D ]  }|\  }}}	}
}}}d}|rot	        j
                  t        j                        |j                  t        j                        z
  }|j                         dk  rd}n|j                         dk  rd	}|j                  |||	||xs dt        |xs d      t        t        |xs d      d
z  d      |r|j                         nd|
r|
j                         ndd	        |t        |      ddS # t        $ r3}t         j#                  d|        t%        dt'        |            d}~ww xY ww)z3Get developers with aggregated statistics using ORMr   r   a  
            SELECT 
                d.developer_id,
                d.name,
                d.email,
                d.created_at,
                COUNT(ar.id) as activity_count,
                MAX(ar.timestamp) as last_activity,
                COALESCE(SUM(ar.duration), 0) as total_duration
            FROM developers d
            LEFT JOIN activity_records ar ON d.developer_id::VARCHAR = ar.developer_id::VARCHAR
            WHERE d.active = true
            GROUP BY d.developer_id, d.name, d.email, d.created_at
        r   r   r   r   r   r   i     N)	r   r    r.   r%   r*   total_duration_secondstotal_duration_hoursrR   r/   orm_with_aggregation)r3   r5   r7   z%Error getting developers with stats: r8   r9   )r<   r   r=   r>   r   r@   r   rA   r   rB   rC   floatroundrD   rE   rF   rG   rH   r   rI   )r   r   rJ   rK   rL   rM   rN   rP   r    r.   r/   r*   rR   total_durationr%   rS   rT   s                    rU   get_developers_with_statsrz      s    :<#   E" & 1(C ?B<\4
]N F$LL69N9NV^VbVb9N9cc	**,t3%F,,.6#F!!" "0"5A*/0C!*D(-eN4Ga.H4.OQR(S>K!8!8!:QU8Bj224
# 
 )6 )~.,
 	
  <<QC@ACF;;<s)   E1D-D2 1E12	E.;.E))E..E1) fastapir   r   r   sqlalchemy.ormr   r   r<   r   r	   r
   typingr   r   r   r   r'   r   modelsr   r   logging	getLogger__name__rG   routergetrV   rI   intrq   rz        rU   <module>r      s    5 5 . ) )  2 2  , 			8	$	!"18 W<w W< #W<t 67 &/;<;<;< 	;< 8;<| ()29&/ << << *<<r   