U
    ؏i                     @   s   d Z ddlZddlZddlmZmZ ddlmZ ddlZej	dej
eje ddlmZ ejejdd eeZdd	 Zd
d Zedkre  e  dS )z>
Script to verify and show category distribution after update
    N)create_enginetext)tabulate)settingsz)%(asctime)s - %(levelname)s - %(message)s)levelformatc               	   C   s  t tj} |  }td td td td}|| }ddddd	g}tt||d
d td td td td}|| }dddg}tt||d
d td td td ddddg}|D ]}td|	  d td}||d|i }|D ]j}|d r$|d nd}|d r:|d nd}	|d rP|d nd}
td|  td |	 d!|
  qqtd td" td td#}|| }|rd$d%dg}tt||d
d ntd& W 5 Q R X d'S )(z#Show detailed category distributionQ
================================================================================zOVERALL CATEGORY DISTRIBUTIONP================================================================================a  
            SELECT 
                COALESCE(category, 'uncategorized') as category,
                COUNT(*) as activity_count,
                COUNT(DISTINCT developer_id) as developers,
                ROUND(SUM(duration) / 3600.0, 2) as total_hours,
                ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 2) as percentage
            FROM activity_records
            GROUP BY category
            ORDER BY activity_count DESC
        ZCategoryZ
ActivitiesZ
DevelopersZHoursZ
PercentagegridheadersZtablefmtzPRODUCTIVE SUBCATEGORIESa]  
            SELECT 
                COALESCE(subcategory, 'general') as subcategory,
                COUNT(*) as count,
                ROUND(SUM(duration) / 3600.0, 2) as hours
            FROM activity_records
            WHERE category = 'productive'
            GROUP BY subcategory
            ORDER BY count DESC
            LIMIT 15
        ZSubcategoryZCountzSAMPLE ACTIVITIES BY CATEGORYZ
productiveZbrowserZserverznon-workz
--- z ---a_  
                SELECT 
                    LEFT(window_title, 80) as title,
                    application_name as app,
                    subcategory
                FROM activity_records
                WHERE category = :category
                AND window_title IS NOT NULL
                ORDER BY RANDOM()
                LIMIT 5
            categoryr   zNo title   zNo app   Zgeneralu     • z	    App: z | Subcategory: z&UNCATEGORIZED ACTIVITIES (Need Review)aG  
            SELECT 
                LEFT(window_title, 60) as title,
                application_name as app,
                COUNT(*) as count
            FROM activity_records
            WHERE category IS NULL
            GROUP BY window_title, application_name
            ORDER BY count DESC
            LIMIT 10
        zWindow TitleZApplicationu#   ✓ All activities are categorized!N)
r   r   DATABASE_URLconnectprintr   executefetchallr   upper)engineconnqueryresultr   
categoriesr   rowtitleZappZsubcat r   ./verify_categories.pyverify_categories   sN    


r   c               	   C   sr   t tj} |  V}td td td td}|| }ddddd	d
g}tt||dd W 5 Q R X dS )z)Show productivity statistics by developerr   zPRODUCTIVITY BY DEVELOPERr	   a  
            WITH developer_stats AS (
                SELECT 
                    developer_id,
                    category,
                    SUM(duration) as category_duration
                FROM activity_records
                WHERE category IS NOT NULL
                GROUP BY developer_id, category
            ),
            developer_totals AS (
                SELECT 
                    developer_id,
                    SUM(duration) as total_duration
                FROM activity_records
                GROUP BY developer_id
            )
            SELECT 
                ds.developer_id,
                ROUND(dt.total_duration / 3600.0, 2) as total_hours,
                ROUND(SUM(CASE WHEN ds.category = 'productive' THEN ds.category_duration ELSE 0 END) / 3600.0, 2) as productive_hours,
                ROUND(SUM(CASE WHEN ds.category = 'browser' THEN ds.category_duration ELSE 0 END) / 3600.0, 2) as browser_hours,
                ROUND(SUM(CASE WHEN ds.category = 'server' THEN ds.category_duration ELSE 0 END) / 3600.0, 2) as server_hours,
                ROUND(100.0 * SUM(CASE WHEN ds.category = 'productive' THEN ds.category_duration ELSE 0 END) / dt.total_duration, 2) as productivity_percentage
            FROM developer_stats ds
            JOIN developer_totals dt ON ds.developer_id = dt.developer_id
            GROUP BY ds.developer_id, dt.total_duration
            ORDER BY productivity_percentage DESC
        Z	DeveloperzTotal HoursZ
ProductiveZBrowserZServerzProductivity %r
   r   N)	r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   show_productivity_by_developer   s    

r    __main__)__doc__sysosZ
sqlalchemyr   r   r   Zloggingpathinsertdirnameabspath__file__Zconfigr   ZbasicConfigINFOZ	getLogger__name__loggerr   r    r   r   r   r   <module>   s"   
j-