
    yj                        d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZ  ee          Z	 dddd	d
edededz  dedz  dedz  dej        fdZ	 dddd	d
edededz  dedz  dedz  dej        fdZdZddd	dededz  dedz  dedz  dee         f
dZdeee                  dej        fdZdedz  dej        dej        fdZdS )    )Sequence)	getLogger)Any)Selectcasefuncor_select)Row)AsyncSession)modelsschemasNobserverobserveddbworkspace_namesession_namer   r   returnc                   K   |r|nd}|r|nd}|r|nd}t          ||||          }|                     |           d{V }	|	                                }
t          |
          }t	          ||          S )a  
    Get the processing queue status, optionally filtered by observer, sender, and/or session.

    Only tracks user-facing task types: representation, summary, and dream.
    Internal infrastructure tasks (reconciler, webhook, deletion) are excluded.

    Note: completed_work_units reflects items since the last periodic queue
    cleanup, not lifetime totals.

    Args:
        db: Database session
        workspace_name: Name of the workspace
        session_name: Optional session name to filter by
        observer: Optional name of the observer to filter by
        observed: Optional name of the observed (message sender) to filter by
    Nr   )_build_queue_status_queryexecutefetchall_process_queue_rows_build_status_response)r   r   r   r   r   normalized_observernormalized_observednormalized_session_namestmtresultrowscountss               8/DATA/AppData/hermes/projects/honcho/src/crud/deriver.pyget_queue_statusr$      s      2 '/8((D&.8((D.:Dll$$$	  D ::d########F??D &&F!      c                :   K   t          | ||||           d{V S )z!Deprecated: use get_queue_status.r   r   r   r   r   N)r$   r'   s        r#   get_deriver_statusr(   ;   sL       "%!         r%   )representationsummarydreamc                r	   t           j        j        d         j        }t           j        j        d         j        }t           j        j        }t           j        j         t           j        j                            d          z  }t           j        j         t           j        j                            d          z  }t          t           j        j
        t          j                                                                        d          t          j        t          |df                                                                        d          t          j        t          |df                                                                        d          t          j        t          |df                                                                        d          t          j                                        t           j        j
        	                              d
          t          j        t          |df                                        t           j        j
        	                              d          t          j        t          |df                                        t           j        j
        	                              d          t          j        t          |df                                        t           j        j
        	                              d          	  	                            t           j                  }	|	                    t           j        t           j        j        t           j        j        k              }	|	                    t           j        j        | k              }	|	                    t           j        j                            t.                              }	|j|	                    t           j        t           j        j
        t           j        j        k              }	|	                    t           j        j        |k              }	g }
||
                    ||k               ||
                    ||k               |
r|	                    t9          |
           }	|	S )zABuild SQL query for queue status with validation and aggregation.r   r   Ntotal   	completedin_progresspending)partition_bysession_totalsession_completedsession_in_progresssession_pending)r   	QueueItempayloadastext	processedActiveQueueSessionidisnotis_r
   
session_idr   countoverlabelr   select_from	outerjoinwork_unit_keywherer   	task_typein__TRACKED_TASK_TYPESjoinSessionnameappendr	   )r   r   r   r   observer_name_exprobserved_name_expris_completedis_in_progress
is_pendingr   peer_conditionss              r#   r   r   R   s     )1*=D)1*=D #-L'11!$**400N #--!$((..J
 #
!!'**
4q)**++002288EE
4+,,--2244::=II
4Q(())..0066yAA
	6+6	7	7			
4q)**++	6+6	7	7	"	#	#
4+,,--	6+6	7	7	$	%	%
4Q(())	6+6	7	7	 	!	!' ( k&"##) 	, >>!&&*C*QQ D
 ::f&5GHHD ::f&0445HIIJJDyyNF,76>;LL
 
 zz&.-=>>O1X=>>>1X=>>> 1zz#/00Kr%   r!   c                 |   | st          j        ddddi           S | d         }i }t                      }| D ]Z}|j        rQ|j        |vrHt          j        |j        |j        |j                  ||j        <   |                    |j                   [t          j        |j	        |j
        |j        |j        |          S )z=Process query results that already contain aggregated counts.r   )r-   r/   r0   r1   sessions)r/   r0   r1   )r   QueueCountssetr?   SessionCountsr4   r5   r6   addr-   r/   r0   r1   )r!   	first_rowrU   seen_sessionsrows        r#   r   r      s     
"
 
 
 	
 QI 24H!eeM . .> 	.cnMAA'.'</3+( ( (HS^$
 cn---o%)!   r%   r"   c                    | r,t          j        |j        |j        |j        |j                  S i }|j                                        D ]G\  }}|j        |j        z   |j        z   }t          j        |||j        |j        |j                  ||<   Ht          j        |r|nd|j        |j        |j        |j                  S )z Build the final response object.)total_work_unitscompleted_work_unitsin_progress_work_unitspending_work_units)r?   r^   r_   r`   ra   N)rU   r^   r_   r`   ra   )	r   QueueStatusr-   r/   r0   r1   rU   itemsSessionQueueStatus)r   r"   rU   r?   datar-   s         r#   r   r      s      
"#\!'!1#)#5%~	
 
 
 	
 79H"O1133 
 

D!11DL@&9!"!%#'#3#| 
  
  
 %/4#-%1!>   r%   )N)collections.abcr   loggingr   typingr   
sqlalchemyr   r   r   r	   r
   sqlalchemy.enginer   sqlalchemy.ext.asyncior   srcr   r   __name__loggerstrrb   r$   r(   rI   r   rV   r   r    r%   r#   <module>rq      s   $ $ $ $ $ $             6 6 6 6 6 6 6 6 6 6 6 6 6 6 ! ! ! ! ! ! / / / / / /        	8		  $*
  * * *** **
 Dj* Dj* * * * *`  $
     *
 Dj Dj    ( =   C C CC*C Dj	C
 DjC C[C C C CL"hs3x0 "W5H " " " "J*      r%   