Coverage for src/lexigram/web/admin/handlers/server_status.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 04:37 +0800

1"""Server status widget handler for web admin dashboard.""" 

2 

3from __future__ import annotations 

4 

5import platform 

6import threading 

7import time 

8 

9from lexigram.contracts.admin import Stat, StatContent, Tone, WidgetParams 

10from lexigram.contracts.admin.errors import AdminError 

11from lexigram.result import Ok, Result 

12 

13_PROCESS_START = time.monotonic() 

14 

15 

16class ServerStatusWidgetHandler: 

17 """Handler for the server_status widget. 

18 

19 Reports real process information: Python version, process uptime, and 

20 thread count. 

21 """ 

22 

23 async def get_data(self, params: WidgetParams) -> Result[StatContent, AdminError]: 

24 """Fetch server status data. 

25 

26 Args: 

27 params: Widget request parameters (unused for this widget). 

28 

29 Returns: 

30 Result containing StatContent with process information. 

31 """ 

32 return Ok( 

33 StatContent( 

34 stats=( 

35 Stat(label="Python", value=platform.python_version()), 

36 Stat( 

37 label="Process Uptime (s)", 

38 value=f"{int(self._uptime_seconds()):,}", 

39 ), 

40 Stat( 

41 label="Threads", 

42 value=str(threading.active_count()), 

43 tone=Tone.INFO, 

44 ), 

45 ) 

46 ) 

47 ) 

48 

49 @staticmethod 

50 def _uptime_seconds() -> float: 

51 return time.monotonic() - _PROCESS_START 

52 

53 

54__all__ = ["ServerStatusWidgetHandler"]