
    :j^@                         d 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
mZmZ ddlmZ  ed	      Zd
ededefdZd
ededefdZ G d d      Zy)z
Status Display Engine for Monitor Package

This module provides unified rendering for all status display needs.
It replaces the scattered display logic throughout the system.
    )List)
get_config)DisplayContextConfig)
get_logger   )
AlertLevelDisplayModeStatusContext)SystemStatusstatecontextmodereturnc           
         	 t               }|j                  j                  j                  | j                  i       }|j                  |j                        }|r|r|j                  d|j
                        |j                  d|j                        |j                  d|j                        |j                  d|j                        |j                  d|j                        |j                  d|j                        |j                  d|j                        d}t        di |S |S t        | |fi |S # t        $ r/}t        j!                  d	|        t        | |fi |cY d
}~S d
}~ww xY w)z4Get display configuration from unified config systemtitleshow_workersshow_alertsshow_performanceshow_newline_prefixwidthmax_alerts_per_levelr   r   r   r   r   r   r   z$Config load failed, using defaults: N )r   displaycontextsgetvaluer   r   r   r   r   r   r   r   _get_default_config	Exceptionloggerdebug)r   r   	overridesconfigcontext_configsmode_configconfig_dictes           4/root/.openclaw/workspace/harvester/state/display.pyget_display_configr)      sB   ? ..1155gmmRH%))$**5 ']]7K4E4EF$-MM.+BZBZ$[#,==@W@W#X(16H+JfJf(g+4==9NP[PoPo+p&]]7K4E4EF,5MM:PR]RrRr,s ,:k::"" 'wB	BB ?;A3?@"7D>I>>?s*   D#D5 &D5 (D5 5	E->$E("E-(E-c           
         t         j                  dt         j                  dt         j                  dt         j                  dt         j
                  di}|j                  | d      |t        j                  t        j                  fv|t        j                  t        j                  fv |t        j                  t        j                  fv |t        j                  t        j                  fv ddd	}i ||}t        d
i |S )z Get default configuration valueszSystem StatuszTask Manager StatuszPipeline MonitoringzApplication StatuszPipeline StatusStatusP      r   r   )r
   SYSTEMTASK_MANAGER
MONITORINGAPPLICATIONMAINr   r	   COMPACTSUMMARYDETAILEDr   )r   r   r"   titlesdefaultsfinal_configs         r(   r   r   6   s     	o""$9  "7!!#7-F GX.[%8%8+:M:M$NN 4 4k6L6LMM [%9%9;;Q;Q$RR#(<(<k>T>T'UU !H -h,),L/,//    c            
          e Zd ZdZdededededdf
dZdededdfd	Z	dededdfd
Z
dededdfdZdededdfdZdededededdf
dZdeddfdZdededee   fdZdedee   fdZdedee   fdZdedee   fdZdededee   fdZy)StatusDisplayEnginezx
    Unified display engine that renders SystemStatus in various formats.
    Replaces all scattered display logic.
    statusr   r   r#   r   Nc                    	 |t         j                  k(  r| j                  ||       y|t         j                  k(  r| j	                  ||       y|t         j
                  k(  r| j                  ||       y|t         j                  k(  r| j                  ||||       y| j                  ||       y# t        $ r3}t        j                  d|        | j                  |       Y d}~yd}~ww xY w)zMain rendering entry pointzDisplay rendering failed: N)r	   r3   _render_compactr4   _render_summaryr0   _render_monitoringr5   _render_detailed_render_standardr   r    error_render_fallback)selfr<   r   r   r#   r'   s         r(   renderzStatusDisplayEngine.renderU   s    	*{***$$VV4,,,$$VV4///''7---%%fgtVD%%ff5 	*LL5aS9:!!&))	*s.   %B/ %B/ %B/ 4'B/ B/ /	C+8)C&&C+c                    g }|j                   }|j                  d|z         |j                  |j                  d| d       |j                  d|z         |j                         r=|j                  j                         }|j                  d| d|j                  dd       |j                         ry|j                  j                         D ]\  \  }}|j                         }|j                  |dd	| d
|j                  j                  dd|j                  j                  d       ^ |j                  d|z         |D ]  }	t        j                  |	        y)z!Render compact single-line format=^ zQueues:  | Runtime: .1fs>10 [z	]: valid=z>3z, links=>4N)r   appendr   has_pipeline_datapipeline
queue_sizeruntimehas_provider_data	providersitemsabbreviationsresourcevalidlinksr    info)
rE   r<   r#   linesr   sizenameproviderabbrevlines
             r(   r>   z#StatusDisplayEngine._render_compactj   sN    S5[!QugPI.0S5[! ##%??--/DLL8D6fnnS5IKL ##%"("2"2"8"8": h!//1Cj6( +%..44R8 9%..44R8: 	S5[!  	DKK	r9   c           	         g }|j                   }|j                  d|z         |j                  |j                  d| d       |j                  d|z         |j                  d|j                  j                  j                          d|j
                  dd       |j                  j                  dkD  rX|j                  d	|j                  j                   d
|j                  j                   d|j                  j                  dd       |j                  j                  dkD  r@|j                  d|j                  j                   d|j                  j                   d       |j                         }|r<|D cg c]  }|j                   }}|j                  ddj                  |              |j                  d|z         |D ]  }t        j!                  |        yc c}w )zRender brief summary formatrH   rI   rJ   zState: rK   rL   rM   r   zTasks: /z (.1%z	 success)zKeys: z valid,  totalzActive Providers: z, N)r   rQ   r   r   r   rU   taskstotal	completedsuccess_raterZ   r[   active_providersr`   joinr    r]   )	rE   r<   r#   r^   r   rl   pprovider_namesrc   s	            r(   r?   z#StatusDisplayEngine._render_summary   s    S5[!QugPI.0S5[! 	wv||11779:,v~~VYFZZ[\]<<!LL&,,0016<<3E3E2FfV\\MfMfgjLkktu ??  1$LL6&//"7"7!8AVAV@WW]^_ "224.>?aff?N?LL-dii.G-HIJS5[!  	DKK	 @s   =G&c                 ,   g }|j                   }|j                  d|z         |j                  |j                  d| d       |j                  d|z         |j                         r6|j	                  | j                  ||             |j                  d|z         n%|j                  d       |j                  d|z         |j                         r!|j	                  | j                  |             n|j                  d       |j                  rM|j                  j                  dkD  r4|j                  d|z         |j	                  | j                  |             |j                  rE|j                         r5|j                  d|z         |j	                  | j                  ||             |j                  d|z         |D ]M  }|j                  r*|j!                  d      rt"        j%                  d|z          9t"        j%                  |       O y	)
z!Render standard multi-line formatrH   rI   rJ   -No pipeline data availableNo provider data availabler   
N)r   rQ   r   rR   extend_format_pipeline_sectionrV   _format_provider_sectionr   performance
throughput_format_performance_sectionr   
has_alerts_format_alerts_sectionr   
startswithr    r]   rE   r<   r#   r^   r   rc   s         r(   rB   z$StatusDisplayEngine._render_standard   s    S5[!QugPI.0S5[! ##%LL66vvFGLLu%LL56LLu% ##%LL66v>?LL56 ""v'9'9'D'Dq'HLLu%LL99&AB &"3"3"5LLu%LL44VVDES5[!  	"D))dooc.BD4K(D!		"r9   c           	         g }|j                   }|j                  d|z         |j                  |j                  d| d       |j                  d|z         |j                  d|j                  dd|j                  j
                  dd|j                  j                  d       |j                  r[|j                  d	|j                  j                   d
|j                  j                   d|j                  j                          d       |j                  d|z         |j                         r5|j                  | j                  ||             |j                  d|z         |j!                         r |j                  | j#                  |             |j%                         r5|j                  d|z         |j                  | j'                  ||             |j                  d|z         |D ]  }t(        j+                  |        y)z7Render monitoring-specific format with performance datarH   rI   rJ   	Runtime: rL   zs | Throughput: z tasks/sec | Success: rf   z	Workers: re   z active | Queues: rg   rq   N)r   rQ   r   rU   rx   ry   rk   r   workeractiveri   rS   rT   rR   ru   rv   rV   #_format_provider_monitoring_sectionr{   r|   r    r]   r~   s         r(   r@   z&StatusDisplayEngine._render_monitoring   s    S5[!QugPI.0S5[! 	s+ ,!--88= >**77<>	
 LLFMM0016==3F3F2G H!??5578@
 	S5[! ##%LL66vvFGLLu% ##%LLAA&IJ LLu%LL44VVDES5[!  	DKK	r9   c           
         t        |||j                  ddd|j                  |j                        }| j	                  ||       g }|j                  }|j                  d|z         |j                  d       |j                  d|j                         rdnd        |j                  d|j                  j                  d	       |j                  d
t        |j                                       |j                  j                  dkD  r)|j                  d|j                  j                  d	       |j                  d|z         |D ]  }t        j                  |        y)z+Render detailed format with all informationT)r   r   r   r   r   r   rq   zSystem Health:z  Overall Status: HealthyzIssues Detected  Error Rate: rf   z  Critical Alerts: r   z  Worker Utilization: rH   N)r)   r   r   r   rB   rQ   healthyrx   
error_ratelencritical_alertsr   ri   utilizationr    r]   )	rE   r<   r   r   r#   detailed_configr^   r   rc   s	            r(   rA   z$StatusDisplayEngine._render_detailed  s=   
 -,,! & : :,,	
 	fo6  S5[!%&)v~~7G)M^)_`a~f&8&8&C&CC%HIJ*3v/E/E/G+H*IJK =="LL1&--2K2KC1PQRS5[!  	DKK	r9   c           	          d}d|z  dd| dd|z  d|j                   j                   d|j                  dd	d
t        |j                         d|z  g}|D ]  }t
        j                  |        y)z1Render fallback display when main rendering fails<   rH   r+   rI   rJ   zSystem State: r   rL   z secondszProviders: N)r   r   rU   r   rW   r    r]   )rE   r<   r   r^   rc   s        r(   rD   z$StatusDisplayEngine._render_fallback)  s    %K% y!%KV\\//01s+84#f../01%K
  	DKK	r9   c                    g }|j                   j                  s|j                  d       |S |j                  rB|j                  ddddddddddddd	d	       |j                  d
|j                  z         |j                   j                  j                         D ]  \  }}|r|st        j                  d| d|        &|j                  }|j                  }|j                  }|j                  }	|j                  r%|j                  |dd|dd|dd|dd|	d	       |j                  |dd|dd|dd|         |S )z.Format pipeline section with table-like layoutrr   Stage<10 | Queue<8	ProcessedErrorsWorkersrq   z+Ignore inviliad stage metrics data, stage: z, metrics: rN   z: queue=z<4z, processed=<6z	, errors=)rS   stagesrQ   r   r   rX   r    r!   rT   total_processedtotal_errorsworkers)
rE   r<   r#   r^   r`   metricsrT   	processederrorsr   s
             r(   rv   z,StatusDisplayEngine._format_pipeline_section9  so   %%LL56L LLGC=GB<s;s:K3xXZm[^_hik^lmnLLv||+, $__3399; 	nMD'wJ4&P[\c[def ++J//I))FooG""SzZO3yoSQWXZP[[^_fgi^jklSz*RYWYNZcdjcklm	n r9   c                 F   g }|j                   s|j                  d       |S |j                  dddddddddd	ddd
ddddddd       |j                  d       |j                   j                         D ]  \  }}|j                         }|j                  |dd|j                  j
                  dd|j                  j                  dd|j                  j                  dd|j                  j                  dd|j                  j                  dd|d        |S )z.Format provider section with table-like layoutrs   Provider<12r   Validr   zNo Quotaz<9WaitInvalidr   LinksModer   zP--------------------------------------------------------------------------------)
rW   rQ   rX   rY   rZ   r[   no_quota
wait_checkinvalidr\   )rE   r<   r^   r`   ra   rb   s         r(   rw   z,StatusDisplayEngine._format_provider_sectionX  sK   LL56L 	#c'"SBs6"+SQZ[]P^^abijlammpqwx{p|}	
 	X %..446 	ND(++-FLL*C 1 1 7 7;3$$--b1X5F5F5Q5QRT4UUX$$,,R0H4E4E4K4KB3Os#, 	 r9   c                 T   g }|j                   j                         D ]  \  }}|j                         }|j                  j                  dz  }|j                  |dd|dd|j                  j                  dd|dd	|j                  j                  d
d|j                  d        |S )z/Format provider section with monitoring metricsd   r   rO   z>8z] | Valid: rP   z	 | Rate: z>5.1fz% | Links: z>5z | API: rf   )rW   rX   rY   rZ   rk   rQ   r[   r\   )rE   r<   r^   r`   ra   rb   rk   s          r(   r   z7StatusDisplayEngine._format_provider_monitoring_sectionr  s    $..446 
	ND(++-F#,,99C?LLL*Bvbk *"++11"5 6%e, -"++11"5 6 --c2	4	
	 r9   c                 *   dd|j                   j                  ddd|j                   j                  dd|j                   j                  dg}|j                   j                  dkD  r*|j                  d	|j                   j                  dd
       |S )z"Format performance metrics sectionzPerformance Metrics:z  Throughput: z.2fz
 tasks/secz  Success Rate: rf   r   r   z  Avg Response Time: rM   )rx   ry   rk   r   avg_response_timerQ   )rE   r<   r^   s      r(   rz   z/StatusDisplayEngine._format_performance_section  s     #V//::3?zJv11>>sCDV//::3?@	
 //!3LL01C1C1U1UVY0ZZ[\]r9   c                    dg}|j                   D cg c]"  }|j                  t        j                  k(  s!|$ }}|j                   D cg c]"  }|j                  t        j                  k(  s!|$ }}|j                   D cg c]"  }|j                  t        j
                  k(  s!|$ }}|j                   D cg c]"  }|j                  t        j                  k(  s!|$ }}|j                  }	t        d|	dz
        }
|rF|j                  dt        |       d       |d|	 D ]   }|j                  d|j                          " |rF|j                  dt        |       d       |d|	 D ]   }|j                  d|j                          " |rF|j                  dt        |       d       |d|	 D ]   }|j                  d|j                          " |rF|j                  d	t        |       d       |d|
 D ]   }|j                  d|j                          " |s|s|s|s|j                  d
       |S c c}w c c}w c c}w c c}w )zFormat alerts sectionzAlerts:r   z  CRITICAL (z):Nz    - z	  ERROR (z  WARNING (z  INFO (z  No alerts)alertslevelr   CRITICALERRORWARNINGINFOr   maxrQ   r   message)rE   r<   r#   r^   ar   error_alertswarning_alertsinfo_alerts
max_alertsmax_infoalerts               r(   r|   z*StatusDisplayEngine._format_alerts_section  s"   %; '-mmVqww*BUBU7U1VV#)==PaAGGz?O?O4OPP%+]]TaggASAS6S!TT"(--NQ177joo3MqNN00
q*q.)LL<O(<'=R@A(*5 7vemm_567 LL9S%6$7r:;%kz2 7vemm_567 LL;s>':&;2>?'4 7vemm_567 LL8C$4#5R89$Yh/ 7vemm_567  <>[LL'A WPTNs.   "H6H6	"H;,H; "I #I 7"II)__name__
__module____qualname____doc__r   r
   r	   r   rF   r>   r?   rB   r@   rA   rD   r   strrv   rw   r   rz   r|   r   r9   r(   r;   r;   O   sr   
*"*-:*BM*Wk*	**l <P UY >l <P UY B)"| )"=Q )"VZ )"V+ +?S +X\ +Z%"%-:%BM%Wk%	%N|   | EY ^bcf^g >| S	 4, 4PS9 $, 49 %\ %CW %\`ad\e %r9   r;   N)r   typingr   r#   r   config.schemasr   tools.loggerr   enumsr   r	   r
   modelsr   r    r)   r   r;   r   r9   r(   <module>r      sn      / # 9 9  	G	? ?[ ?Rf ?@0 0k 0Sg 02h hr9   