vnai 2.0.2__py3-none-any.whl → 2.0.3__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,124 @@
1
- _B='status'
2
- _A='healthy'
3
- import threading,time
1
+ ##
2
+
3
+ ##
4
+
5
+
6
+ import threading
7
+ import time
4
8
  from datetime import datetime
9
+
5
10
  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()
11
+ #--
12
+
13
+ _instance = None
14
+ _lock = threading.Lock()
15
+
16
+ def __new__(cls):
17
+ with cls._lock:
18
+ if cls._instance is None:
19
+ cls._instance = super(Monitor, cls).__new__(cls)
20
+ cls._instance._initialize()
21
+ return cls._instance
22
+
23
+ def _initialize(self):
24
+ #--
25
+ self.health_status = "healthy"
26
+ self.last_check = time.time()
27
+ self.check_interval = 300 ##
28
+
29
+ self.error_count = 0
30
+ self.warning_count = 0
31
+ self.status_history = []
32
+
33
+ ##
34
+
35
+ self._start_background_check()
36
+
37
+ def _start_background_check(self):
38
+ #--
39
+ def check_health():
40
+ while True:
41
+ try:
42
+ self.check_health()
43
+ except:
44
+ pass ##
45
+
46
+ time.sleep(self.check_interval)
47
+
48
+ thread = threading.Thread(target=check_health, daemon=True)
49
+ thread.start()
50
+
51
+ def check_health(self):
52
+ #--
53
+ from vnai.beam.metrics import collector
54
+ from vnai.beam.quota import guardian
55
+
56
+ ##
57
+
58
+ self.last_check = time.time()
59
+
60
+ ##
61
+
62
+ metrics_summary = collector.get_metrics_summary()
63
+ has_errors = metrics_summary.get("error", 0) > 0
64
+
65
+ ##
66
+
67
+ resource_usage = guardian.usage()
68
+ high_usage = resource_usage > 80 ##
69
+
70
+
71
+ ##
72
+
73
+ if has_errors and high_usage:
74
+ self.health_status = "critical"
75
+ self.error_count += 1
76
+ elif has_errors or high_usage:
77
+ self.health_status = "warning"
78
+ self.warning_count += 1
79
+ else:
80
+ self.health_status = "healthy"
81
+
82
+ ##
83
+
84
+ self.status_history.append({
85
+ "timestamp": datetime.now().isoformat(),
86
+ "status": self.health_status,
87
+ "metrics": metrics_summary,
88
+ "resource_usage": resource_usage
89
+ })
90
+
91
+ ##
92
+
93
+ if len(self.status_history) > 10:
94
+ self.status_history = self.status_history[-10:]
95
+
96
+ return self.health_status
97
+
98
+ def report(self):
99
+ #--
100
+ ##
101
+
102
+ if time.time() - self.last_check > self.check_interval:
103
+ self.check_health()
104
+
105
+ return {
106
+ "status": self.health_status,
107
+ "last_check": datetime.fromtimestamp(self.last_check).isoformat(),
108
+ "error_count": self.error_count,
109
+ "warning_count": self.warning_count,
110
+ "history": self.status_history[-3:], ##
111
+
112
+ }
113
+
114
+ def reset(self):
115
+ #--
116
+ self.health_status = "healthy"
117
+ self.error_count = 0
118
+ self.warning_count = 0
119
+ self.status_history = []
120
+ self.last_check = time.time()
121
+
122
+ ##
123
+
124
+ monitor = Monitor()