
    :jf                         d 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
mZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ  ed      Z G d de      Zy)a	  
Status Manager - Unified scheduling entry point for status monitoring

This module provides the single entry point for all status monitoring and display operations.
Inherits PeriodicTaskManager to serve as the new scheduling entry point for the monitoring system.
    )Optional)DisplayConfig)StatusCollector)StatusDisplayEngineget_display_config)DisplayModeStatusContext)SystemStatus)TaskDataProvider)
get_logger)handle_exceptions   )PeriodicTaskManagermanagerc                       e Zd ZdZ	 	 ddedee   def fdZddZ	e
j                  ej                  d	dfd
e
dedededdf
dZ ed	d      defd       Z eg d      defd       ZddZddZd
e
defdZd
e
deddfdZ xZS )StatusManagerae  
    Unified Status Manager - Scheduling entry point for all status operations

    This class serves as the new monitoring entry point, replacing scattered display methods:
    - monitoring.print_status()
    - application.print_status()
    - task_manager._print_progress()
    - main.print_stats()
    - StatusDisplayService.display_status()

    Inherits PeriodicTaskManager to provide scheduling capabilities.

    Usage Examples:
        # Standard system status
        status_manager.show_status()

        # Compact monitoring status
        status_manager.show_status(StatusContext.MONITORING, DisplayMode.COMPACT)

        # Detailed application status
        status_manager.show_status(StatusContext.APPLICATION, DisplayMode.DETAILED)

        # Task manager progress
        status_manager.show_status(StatusContext.TASK_MANAGER, DisplayMode.COMPACT)
    N	collectortask_providerdisplay_intervalc                     t         |   d|       || _        || _        t	               | _        d| _        d| _        t        j                  d       y)a,  Initialize with StatusCollector and optional task provider

        Args:
            collector: StatusCollector instance for data aggregation
            task_provider: Optional task data provider for direct task stats access
            display_interval: Display update interval in seconds
        r   N        z3StatusManager initialized as scheduling entry point)
super__init__r   r   r   display_engine_last_status_last_updateloggerdebug)selfr   r   r   	__class__s       5/root/.openclaw/workspace/harvester/manager/status.pyr   zStatusManager.__init__5   sP     	*:; #*13 !JK    returnc                 J   	 | j                   rU| j                  j                  r?| j                   j                         }| j                  j                  j	                  |       | j                  d       y# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z=Execute periodic status display - main scheduling entry pointT)refreshstatusz"Error in periodic status display: N)	r   r   
monitoringstatsingestshow_status	Exceptionr   error)r   r&   es      r!   _execute_periodic_taskz$StatusManager._execute_periodic_taskP   s    
	C!!dnn&?&?++113))008 T&9 	CLL=aSABB	Cs   A4A7 7	B" BB"Fcontextmoder%   r&   c                 x   	 	 |rt        |t              s| j                  j                  |      }	  | j                  ||fi |}	 | j                  j                  ||||       || _
        |j                  | _        y# t        $ r}t
        j                  d|         d}~ww xY w# t        $ r}t
        j                  d|         d}~ww xY w# t        $ r}t
        j                  d|         d}~ww xY w# t        $ r4}t
        j                  d|        | j                  ||       Y d}~yd}~ww xY w)aN  
        Single entry point for all status display operations

        Args:
            context: Display context (system, monitoring, application, etc.)
            mode: Display mode (compact, standard, detailed, etc.)
            refresh: Force data refresh (bypass cache)
            **options: Additional display options
        r%   zError collecting status: NzError getting display config: zError rendering status: zStatus display failed: )
isinstancer
   r   r&   r+   r   r,   _get_display_configr   renderr   	timestampr   _show_emergency_status)r   r/   r0   r%   r&   optionsr-   display_configs           r!   r*   zStatusManager.show_status^   s5   "	7Z%E!^^2272CF!9!9!9'4!S7!S##**67D.Q !'D & 0 0D)  8<=  =aSAB  7s;<  	7LL21#67''66	7sp   .A> B( C %C< >	B%B  B%%C< (	C1C

CC< 	C9C44C99C< <	D9*D44D9r,   )default_result	log_levelc                 Z    | j                   j                  d      }|j                         S )z#Check if system is in healthy stateFr2   )r   r&   healthyr   r&   s     r!   is_system_healthyzStatusManager.is_system_healthy   s(     &&u&5~~r"   c                 Z    | j                   j                  d      }|j                         S )zGet list of critical alertsFr2   )r   r&   critical_alertsr>   s     r!   get_critical_alertsz!StatusManager.get_critical_alerts   s*     &&u&5%%''r"   c                 N    t         j                  d       | j                          y)z/Handle task completion event from task providerzAReceived task completion event, marking StatusManager as finishedN)r   infomark_finishedr   s    r!   _on_task_completionz!StatusManager._on_task_completion   s    WXr"   c                 ~    | j                   j                          d| _        d| _        t        j                  d       y)zClear all cached dataNr   zStatusManager cache cleared)r   clear_cacher   r   r   r   rF   s    r!   rI   zStatusManager.clear_cache   s/    ""$ 23r"   c                     	 t        ||fi |S # t        $ r/}t        j                  d|        t        ||fi |cY d}~S d}~ww xY w)z8Get display configuration for the given context and modezError creating display config: N)r   r+   r   r   )r   r/   r0   r8   r-   s        r!   r4   z!StatusManager._get_display_config   sR    	@%gt?w?? 	@LL:1#>?%gt?w??	@s    	A$AAAc                    	 t         j                  t         j                  j                  t         j                  t         j
                        t         j                  d|j                   d|j                   t         j                  t         j                  g}| j                  j                  r |j                  t         j                         n|j                  t         j                         | j                  r |j                  t         j                         n|j                  t         j                          |j                  t         j                         |D ]  }t"        j%                  |        y# t&        $ r}t"        j)                  d|        t"        j%                  t         j*                         t"        j%                  t         j,                         t"        j%                  t         j*                         Y d}~yd}~ww xY w)z-Show emergency status when main display fails)titlewidthz	Context: zMode: z&Emergency status display also failed: N)r   SEPARATOR_MAINTITLE_CENTER_FORMATformatEMERGENCY_TITLEDEFAULT_WIDTHvalueEMERGENCY_ERROR_MSGEMERGENCY_INFO_HEADERr   r'   appendMONITORING_AVAILABLEMONITORING_NOT_AVAILABLEr   TASK_PROVIDER_AVAILABLETASK_PROVIDER_NOT_AVAILABLEr   rD   r+   r,   SEPARATOR_EMERGENCYCRITICAL_FAILURE_MSG)r   r/   r0   linesliner-   s         r!   r7   z$StatusManager._show_emergency_status   sf   !	;,,1188'77}?Z?Z 9  ,,GMM?+%1133
E ~~((]??@]CCD!!]BBC]FFGLL556 "D!"  	;LLA!EFKK99:KK::;KK99::		;s   E1E4 4	H=BHH)Ng      @)r#   N)__name__
__module____qualname____doc__r   r   r   floatr   r.   r	   SYSTEMr   STANDARDboolr
   r*   r   r?   listrB   rG   rI   r4   r7   __classcell__)r    s   @r!   r   r      s   : 59"%	L"L   01L  	L6C  "/!5!5'00#.7.7 .7 	.7
 .7 
.7` ew? 4   @ 
 bG<(T ( =(

4@= @ @#;m #;; #;SW #;r"   r   N)rb   typingr   constant.monitoringr   state.collectorr   state.displayr   r   state.enumsr   r	   state.modelsr
   state.typesr   tools.loggerr   tools.utilsr   baser   r   r    r"   r!   <module>rt      sC     - + A 2 % ( # ) %	I	x;' x;r"   