Coverage for src/copytrading_engine/diagnostics/telemetry/config.py: 83%

30 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Diagnostics settings: where the private journal lives and how long it is kept.""" 

2 

3import os 

4from dataclasses import dataclass 

5from pathlib import Path 

6 

7_DEFAULT_QUEUE_CAPACITY = 32 

8_MAX_QUEUE_CAPACITY = 256 

9DEFAULT_RETENTION_DAYS = 7 

10DEFAULT_MAX_BYTES = 256 * 1_024 * 1_024 

11MIN_MAX_BYTES = 8 * 1_024 * 1_024 

12MAX_MAX_BYTES = 16 * 1_024 * 1_024 * 1_024 

13 

14 

15@dataclass(frozen=True) 

16class TelemetryConfig: 

17 """Finite local diagnostics settings; nothing here leaves the Mac.""" 

18 

19 journal_directory: Path 

20 retention_age_days: int = DEFAULT_RETENTION_DAYS 

21 max_bytes: int = DEFAULT_MAX_BYTES 

22 queue_capacity: int = _DEFAULT_QUEUE_CAPACITY 

23 

24 def __post_init__(self) -> None: 

25 if not 1 <= self.queue_capacity <= _MAX_QUEUE_CAPACITY: 25 ↛ 26line 25 didn't jump to line 26 because the condition on line 25 was never true

26 raise ValueError("telemetry queue capacity is outside the supported bounds") 

27 if not 1 <= self.retention_age_days <= 3_650: 27 ↛ 28line 27 didn't jump to line 28 because the condition on line 27 was never true

28 raise ValueError("diagnostics retention age is outside the supported bounds") 

29 if not MIN_MAX_BYTES <= self.max_bytes <= MAX_MAX_BYTES: 29 ↛ 30line 29 didn't jump to line 30 because the condition on line 29 was never true

30 raise ValueError("diagnostics storage limit is outside the supported bounds") 

31 

32 @classmethod 

33 def from_environment(cls, journal_directory: Path) -> TelemetryConfig: 

34 """Read the app's retention choice; an unreadable value keeps the default.""" 

35 return cls( 

36 journal_directory=journal_directory, 

37 retention_age_days=_bounded_int( 

38 "COPYTRADING_DESKTOP_DIAGNOSTICS_RETENTION_DAYS", DEFAULT_RETENTION_DAYS, 1, 3_650 

39 ), 

40 max_bytes=_bounded_int( 

41 "COPYTRADING_DESKTOP_DIAGNOSTICS_MAX_BYTES", 

42 DEFAULT_MAX_BYTES, 

43 MIN_MAX_BYTES, 

44 MAX_MAX_BYTES, 

45 ), 

46 ) 

47 

48 

49def _bounded_int(name: str, default: int, low: int, high: int) -> int: 

50 try: 

51 value = int(os.environ.get(name, str(default))) 

52 except ValueError: 

53 return default 

54 return value if low <= value <= high else default