vnai 2.0.2__py3-none-any.whl → 2.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
vnai/beam/pulse.py CHANGED
@@ -1,31 +1,109 @@
1
- _B='status'
2
- _A='healthy'
3
- import threading,time
1
+ # vnai/beam/pulse.py
2
+ # System health and performance monitoring
3
+
4
+ import threading
5
+ import time
4
6
  from datetime import datetime
7
+
5
8
  class Monitor:
6
- _instance=None;_lock=threading.Lock()
7
- def __new__(A):
8
- with A._lock:
9
- if A._instance is None:A._instance=super(Monitor,A).__new__(A);A._instance._initialize()
10
- return A._instance
11
- def _initialize(A):A.health_status=_A;A.last_check=time.time();A.check_interval=300;A.error_count=0;A.warning_count=0;A.status_history=[];A._start_background_check()
12
- def _start_background_check(A):
13
- def B():
14
- while True:
15
- try:A.check_health()
16
- except:pass
17
- time.sleep(A.check_interval)
18
- C=threading.Thread(target=B,daemon=True);C.start()
19
- def check_health(A):
20
- from vnai.beam.metrics import collector as F;from vnai.beam.quota import guardian as G;A.last_check=time.time();B=F.get_metrics_summary();C=B.get('error',0)>0;D=G.usage();E=D>80
21
- if C and E:A.health_status='critical';A.error_count+=1
22
- elif C or E:A.health_status='warning';A.warning_count+=1
23
- else:A.health_status=_A
24
- A.status_history.append({'timestamp':datetime.now().isoformat(),_B:A.health_status,'metrics':B,'resource_usage':D})
25
- if len(A.status_history)>10:A.status_history=A.status_history[-10:]
26
- return A.health_status
27
- def report(A):
28
- if time.time()-A.last_check>A.check_interval:A.check_health()
29
- return{_B:A.health_status,'last_check':datetime.fromtimestamp(A.last_check).isoformat(),'error_count':A.error_count,'warning_count':A.warning_count,'history':A.status_history[-3:]}
30
- def reset(A):A.health_status=_A;A.error_count=0;A.warning_count=0;A.status_history=[];A.last_check=time.time()
31
- monitor=Monitor()
9
+ """Monitors system health and performance"""
10
+
11
+ _instance = None
12
+ _lock = threading.Lock()
13
+
14
+ def __new__(cls):
15
+ with cls._lock:
16
+ if cls._instance is None:
17
+ cls._instance = super(Monitor, cls).__new__(cls)
18
+ cls._instance._initialize()
19
+ return cls._instance
20
+
21
+ def _initialize(self):
22
+ """Initialize monitor"""
23
+ self.health_status = "healthy"
24
+ self.last_check = time.time()
25
+ self.check_interval = 300 # seconds
26
+ self.error_count = 0
27
+ self.warning_count = 0
28
+ self.status_history = []
29
+
30
+ # Start background health check thread
31
+ self._start_background_check()
32
+
33
+ def _start_background_check(self):
34
+ """Start background health check thread"""
35
+ def check_health():
36
+ while True:
37
+ try:
38
+ self.check_health()
39
+ except:
40
+ pass # Don't let errors stop the monitor
41
+ time.sleep(self.check_interval)
42
+
43
+ thread = threading.Thread(target=check_health, daemon=True)
44
+ thread.start()
45
+
46
+ def check_health(self):
47
+ """Check system health status"""
48
+ from vnai.beam.metrics import collector
49
+ from vnai.beam.quota import guardian
50
+
51
+ # Record check time
52
+ self.last_check = time.time()
53
+
54
+ # Check metrics collector health
55
+ metrics_summary = collector.get_metrics_summary()
56
+ has_errors = metrics_summary.get("error", 0) > 0
57
+
58
+ # Check resource usage
59
+ resource_usage = guardian.usage()
60
+ high_usage = resource_usage > 80 # Over 80% of rate limits
61
+
62
+ # Determine health status
63
+ if has_errors and high_usage:
64
+ self.health_status = "critical"
65
+ self.error_count += 1
66
+ elif has_errors or high_usage:
67
+ self.health_status = "warning"
68
+ self.warning_count += 1
69
+ else:
70
+ self.health_status = "healthy"
71
+
72
+ # Record health status
73
+ self.status_history.append({
74
+ "timestamp": datetime.now().isoformat(),
75
+ "status": self.health_status,
76
+ "metrics": metrics_summary,
77
+ "resource_usage": resource_usage
78
+ })
79
+
80
+ # Keep history limited to last 10 entries
81
+ if len(self.status_history) > 10:
82
+ self.status_history = self.status_history[-10:]
83
+
84
+ return self.health_status
85
+
86
+ def report(self):
87
+ """Get health report"""
88
+ # Ensure we have a fresh check if last one is old
89
+ if time.time() - self.last_check > self.check_interval:
90
+ self.check_health()
91
+
92
+ return {
93
+ "status": self.health_status,
94
+ "last_check": datetime.fromtimestamp(self.last_check).isoformat(),
95
+ "error_count": self.error_count,
96
+ "warning_count": self.warning_count,
97
+ "history": self.status_history[-3:], # Last 3 entries
98
+ }
99
+
100
+ def reset(self):
101
+ """Reset health monitor"""
102
+ self.health_status = "healthy"
103
+ self.error_count = 0
104
+ self.warning_count = 0
105
+ self.status_history = []
106
+ self.last_check = time.time()
107
+
108
+ # Create singleton instance
109
+ monitor = Monitor()