vnai 2.0.9__tar.gz → 2.1.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: vnai
3
- Version: 2.0.9
3
+ Version: 2.1.1
4
4
  Summary: System optimization and resource management toolkit
5
5
  Author-email: Vnstock HQ <support@vnstocks.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "vnai"
7
- version = "2.0.9"
7
+ version = "2.1.1"
8
8
  description = "System optimization and resource management toolkit"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.7"
@@ -1,23 +1,25 @@
1
- _K='success'
2
- _J='buffer_size'
3
- _I='request'
4
- _H='rate_limit'
1
+ _L='success'
2
+ _K='buffer_size'
3
+ _J='request'
4
+ _I='rate_limit'
5
+ _H='args'
5
6
  _G='execution_time'
6
7
  _F='timestamp'
7
- _E=False
8
- _D='error'
9
- _C=True
8
+ _E=True
9
+ _D=False
10
+ _C='error'
10
11
  _B=None
11
12
  _A='function'
12
13
  import sys,time,threading
13
14
  from datetime import datetime
15
+ import hashlib,json
14
16
  class Collector:
15
17
  _instance=_B;_lock=threading.Lock()
16
18
  def __new__(cls):
17
19
  with cls._lock:
18
20
  if cls._instance is _B:cls._instance=super(Collector,cls).__new__(cls);cls._instance._initialize()
19
21
  return cls._instance
20
- def _initialize(self):self.metrics={_A:[],_H:[],_I:[],_D:[]};self.thresholds={_J:50,'error_threshold':.1,'performance_threshold':5.};self.function_count=0;self.colab_auth_triggered=_E;self.max_metric_length=200;self._last_record_time={}
22
+ def _initialize(self):self.metrics={_A:[],_I:[],_J:[],_C:[]};self.thresholds={_K:50,'error_threshold':.1,'performance_threshold':5.};self.function_count=0;self.colab_auth_triggered=_D;self.max_metric_length=200;self._last_record_time={};self.min_interval_per_type=.5;self._recent_hashes=[];self._sending_metrics=_D
21
23
  def record(self,metric_type,data,priority=_B):
22
24
  A='high'
23
25
  if not isinstance(data,dict):data={'value':str(data)}
@@ -25,7 +27,7 @@ class Collector:
25
27
  if metric_type!='system_info':data.pop('system',_B);from vnai.scope.profile import inspector;data['machine_id']=inspector.fingerprint()
26
28
  now=time.time();last_time=self._last_record_time.get(metric_type,0)
27
29
  if now-last_time<self.min_interval_per_type and priority!=A:return
28
- self._last_record_time[metric_type]=now;data_hash=hashlib.md5(json.dumps(data,sort_keys=_C).encode()).hexdigest()
30
+ self._last_record_time[metric_type]=now;data_hash=hashlib.md5(json.dumps(data,sort_keys=_E).encode()).hexdigest()
29
31
  if data_hash in self._recent_hashes and priority!=A:return
30
32
  self._recent_hashes.append(data_hash)
31
33
  if metric_type in self.metrics:
@@ -34,31 +36,38 @@ class Collector:
34
36
  else:self.metrics[_A].append(data)
35
37
  if metric_type==_A:
36
38
  self.function_count+=1
37
- if self.function_count>10 and not self.colab_auth_triggered and'google.colab'in sys.modules:self.colab_auth_triggered=_C;threading.Thread(target=self._trigger_colab_auth,daemon=_C).start()
38
- if sum(len(metric_list)for metric_list in self.metrics.values())>=self.thresholds[_J]:self._send_metrics()
39
- if priority==A or metric_type==_D:self._send_metrics()
39
+ if self.function_count>10 and not self.colab_auth_triggered and'google.colab'in sys.modules:self.colab_auth_triggered=_E;threading.Thread(target=self._trigger_colab_auth,daemon=_E).start()
40
+ if sum(len(metric_list)for metric_list in self.metrics.values())>=self.thresholds[_K]:self._send_metrics()
41
+ if priority==A or metric_type==_C:self._send_metrics()
40
42
  def _trigger_colab_auth(self):
41
43
  try:from vnai.scope.profile import inspector;inspector.get_or_create_user_id()
42
44
  except:pass
43
45
  def _send_metrics(self):
44
- C='vnai';B='source';A='unknown';from vnai.flow.relay import track_function_call,track_rate_limit,track_api_request
46
+ C='vnai';B='source';A='unknown'
47
+ if self._sending_metrics:return
48
+ self._sending_metrics=_E
49
+ try:from vnai.flow.relay import track_function_call,track_rate_limit,track_api_request
50
+ except ImportError:
51
+ for metric_type in self.metrics:self.metrics[metric_type]=[]
52
+ self._sending_metrics=_D;return
45
53
  for(metric_type,data_list)in self.metrics.items():
46
54
  if not data_list:continue
47
55
  for data in data_list:
48
56
  try:
49
- if metric_type==_A:track_function_call(function_name=data.get(_A,A),source=data.get(B,C),execution_time=data.get(_G,0),success=data.get(_K,_C),error=data.get(_D),args=data.get('args'))
50
- elif metric_type==_H:track_rate_limit(source=data.get(B,C),limit_type=data.get('limit_type',A),limit_value=data.get('limit_value',0),current_usage=data.get('current_usage',0),is_exceeded=data.get('is_exceeded',_E))
51
- elif metric_type==_I:track_api_request(endpoint=data.get('endpoint',A),source=data.get(B,C),method=data.get('method','GET'),status_code=data.get('status_code',200),execution_time=data.get(_G,0),request_size=data.get('request_size',0),response_size=data.get('response_size',0))
57
+ if metric_type==_A:track_function_call(function_name=data.get(_A,A),source=data.get(B,C),execution_time=data.get(_G,0),success=data.get(_L,_E),error=data.get(_C),args=data.get(_H))
58
+ elif metric_type==_I:track_rate_limit(source=data.get(B,C),limit_type=data.get('limit_type',A),limit_value=data.get('limit_value',0),current_usage=data.get('current_usage',0),is_exceeded=data.get('is_exceeded',_D))
59
+ elif metric_type==_J:track_api_request(endpoint=data.get('endpoint',A),source=data.get(B,C),method=data.get('method','GET'),status_code=data.get('status_code',200),execution_time=data.get(_G,0),request_size=data.get('request_size',0),response_size=data.get('response_size',0))
52
60
  except Exception as e:continue
53
61
  self.metrics[metric_type]=[]
62
+ self._sending_metrics=_D
54
63
  def get_metrics_summary(self):return{metric_type:len(data_list)for(metric_type,data_list)in self.metrics.items()}
55
64
  collector=Collector()
56
65
  def capture(module_type=_A):
57
66
  def decorator(func):
58
67
  def wrapper(*args,**kwargs):
59
- start_time=time.time();success=_E;error=_B
60
- try:result=func(*args,**kwargs);success=_C;return result
61
- except Exception as e:error=str(e);raise
62
- finally:execution_time=time.time()-start_time;collector.record(module_type,{_A:func.__name__,_G:execution_time,_K:success,_D:error,_F:datetime.now().isoformat(),'args':str(args)[:100]if args else _B})
68
+ start_time=time.time();success=_D;error=_B
69
+ try:result=func(*args,**kwargs);success=_E;return result
70
+ except Exception as e:error=str(e);collector.record(_C,{_A:func.__name__,_C:error,_H:str(args)[:100]if args else _B});raise
71
+ finally:execution_time=time.time()-start_time;collector.record(module_type,{_A:func.__name__,_G:execution_time,_L:success,_C:error,_F:datetime.now().isoformat(),_H:str(args)[:100]if args else _B})
63
72
  return wrapper
64
73
  return decorator
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: vnai
3
- Version: 2.0.9
3
+ Version: 2.1.1
4
4
  Summary: System optimization and resource management toolkit
5
5
  Author-email: Vnstock HQ <support@vnstocks.com>
6
6
  License: MIT
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes