vnai 2.1.1__py3-none-any.whl → 2.1.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/__init__.py +78 -78
- vnai/beam/__init__.py +2 -2
- vnai/beam/metrics.py +72 -72
- vnai/beam/pulse.py +30 -30
- vnai/beam/quota.py +104 -104
- vnai/flow/__init__.py +1 -1
- vnai/flow/queue.py +54 -54
- vnai/flow/relay.py +160 -160
- vnai/scope/__init__.py +3 -3
- vnai/scope/profile.py +222 -222
- vnai/scope/promo.py +112 -117
- vnai/scope/state.py +73 -73
- {vnai-2.1.1.dist-info → vnai-2.1.3.dist-info}/METADATA +20 -20
- vnai-2.1.3.dist-info/RECORD +16 -0
- vnai-2.1.1.dist-info/RECORD +0 -16
- {vnai-2.1.1.dist-info → vnai-2.1.3.dist-info}/WHEEL +0 -0
- {vnai-2.1.1.dist-info → vnai-2.1.3.dist-info}/top_level.txt +0 -0
vnai/__init__.py
CHANGED
@@ -1,79 +1,79 @@
|
|
1
|
-
_L='default'
|
2
|
-
_K='standard'
|
3
|
-
_J='accepted_agreement'
|
4
|
-
_I='environment.json'
|
5
|
-
_H='terms_agreement.txt'
|
6
|
-
_G='timestamp'
|
7
|
-
_F=False
|
8
|
-
_E='id'
|
9
|
-
_D='.vnstock'
|
10
|
-
_C='machine_id'
|
11
|
-
_B=None
|
12
|
-
_A=True
|
13
|
-
import os,pathlib,json,time,threading,functools
|
14
|
-
from datetime import datetime
|
15
|
-
from vnai.beam.quota import guardian,optimize
|
16
|
-
from vnai.beam.metrics import collector,capture
|
17
|
-
from vnai.beam.pulse import monitor
|
18
|
-
from vnai.flow.relay import conduit,configure
|
19
|
-
from vnai.flow.queue import buffer
|
20
|
-
from vnai.scope.profile import inspector
|
21
|
-
from vnai.scope.state import tracker,record
|
22
|
-
import vnai.scope.promo
|
23
|
-
from vnai.scope.promo import present
|
24
|
-
TC_VAR='ACCEPT_TC'
|
25
|
-
TC_VAL='tôi đồng ý'
|
26
|
-
TC_PATH=pathlib.Path.home()/_D/_E/_H
|
27
|
-
TERMS_AND_CONDITIONS='\nKhi tiếp tục sử dụng Vnstock, bạn xác nhận rằng bạn đã đọc, hiểu và đồng ý với Chính sách quyền riêng tư và Điều khoản, điều kiện về giấy phép sử dụng Vnstock.\n\nChi tiết:\n- Giấy phép sử dụng phần mềm: https://vnstocks.com/docs/tai-lieu/giay-phep-su-dung\n- Chính sách quyền riêng tư: https://vnstocks.com/docs/tai-lieu/chinh-sach-quyen-rieng-tu\n'
|
28
|
-
class Core:
|
29
|
-
def __init__(self):self.initialized=_F;self.webhook_url=_B;self.init_time=datetime.now().isoformat();self.home_dir=pathlib.Path.home();self.project_dir=self.home_dir/_D;self.id_dir=self.project_dir/_E;self.terms_file_path=TC_PATH;self.system_info=_B;self.project_dir.mkdir(exist_ok=_A);self.id_dir.mkdir(exist_ok=_A);self.initialize()
|
30
|
-
def initialize(self,webhook_url=_B):
|
31
|
-
if self.initialized:return _A
|
32
|
-
if not self._check_terms():self._accept_terms()
|
33
|
-
from vnai.scope.profile import inspector;inspector.setup_vnstock_environment();present()
|
34
|
-
if webhook_url:self.webhook_url=webhook_url;configure(webhook_url)
|
35
|
-
record('initialization',{_G:datetime.now().isoformat()});self.system_info=inspector.examine();conduit.queue({'type':'system_info','data':{'commercial':inspector.detect_commercial_usage(),'packages':inspector.scan_packages()}},priority='high');self.initialized=_A;return _A
|
36
|
-
def _check_terms(self):return os.path.exists(self.terms_file_path)
|
37
|
-
def _accept_terms(self):
|
38
|
-
system_info=inspector.examine()
|
39
|
-
if TC_VAR in os.environ and os.environ[TC_VAR]==TC_VAL:response=TC_VAL
|
40
|
-
else:response=TC_VAL;os.environ[TC_VAR]=TC_VAL
|
41
|
-
now=datetime.now();signed_agreement=f"""Người dùng có mã nhận dạng {system_info[_C]} đã chấp nhận điều khoản & điều kiện sử dụng Vnstock lúc {now}
|
42
|
-
---
|
43
|
-
|
44
|
-
THÔNG TIN THIẾT BỊ: {json.dumps(system_info,indent=2)}
|
45
|
-
|
46
|
-
Đính kèm bản sao nội dung bạn đã đọc, hiểu rõ và đồng ý dưới đây:
|
47
|
-
{TERMS_AND_CONDITIONS}"""
|
48
|
-
with open(self.terms_file_path,'w',encoding='utf-8')as f:f.write(signed_agreement)
|
49
|
-
env_file=self.id_dir/_I;env_data={_J:_A,_G:now.isoformat(),_C:system_info[_C]}
|
50
|
-
with open(env_file,'w')as f:json.dump(env_data,f)
|
51
|
-
return _A
|
52
|
-
def status(self):return{'initialized':self.initialized,'health':monitor.report(),'metrics':tracker.get_metrics()}
|
53
|
-
def configure_privacy(self,level=_K):from vnai.scope.state import tracker;return tracker.setup_privacy(level)
|
54
|
-
core=Core()
|
55
|
-
def tc_init(webhook_url=_B):return core.initialize(webhook_url)
|
56
|
-
def setup(webhook_url=_B):return core.initialize(webhook_url)
|
57
|
-
def optimize_execution(resource_type=_L):return optimize(resource_type)
|
58
|
-
def agg_execution(resource_type=_L):return optimize(resource_type,ad_cooldown=1500,content_trigger_threshold=100000)
|
59
|
-
def measure_performance(module_type='function'):return capture(module_type)
|
60
|
-
def accept_license_terms(terms_text=_B):
|
61
|
-
if terms_text is _B:terms_text=TERMS_AND_CONDITIONS
|
62
|
-
system_info=inspector.examine();terms_file=pathlib.Path.home()/_D/_E/_H;os.makedirs(os.path.dirname(terms_file),exist_ok=_A)
|
63
|
-
with open(terms_file,'w',encoding='utf-8')as f:f.write(f"Terms accepted at {datetime.now().isoformat()}\n");f.write(f"System: {json.dumps(system_info)}\n\n");f.write(terms_text)
|
64
|
-
return _A
|
65
|
-
def accept_vnstock_terms():
|
66
|
-
from vnai.scope.profile import inspector;system_info=inspector.examine();home_dir=pathlib.Path.home();project_dir=home_dir/_D;project_dir.mkdir(exist_ok=_A);id_dir=project_dir/_E;id_dir.mkdir(exist_ok=_A);env_file=id_dir/_I;env_data={_J:_A,_G:datetime.now().isoformat(),_C:system_info[_C]}
|
67
|
-
try:
|
68
|
-
with open(env_file,'w')as f:json.dump(env_data,f)
|
69
|
-
print('Vnstock terms accepted successfully.');return _A
|
70
|
-
except Exception as e:print(f"Error accepting terms: {e}");return _F
|
71
|
-
def setup_for_colab():from vnai.scope.profile import inspector;inspector.detect_colab_with_delayed_auth(immediate=_A);inspector.setup_vnstock_environment();return'Environment set up for Google Colab'
|
72
|
-
def display_content():return present()
|
73
|
-
def configure_privacy(level=_K):from vnai.scope.state import tracker;return tracker.setup_privacy(level)
|
74
|
-
def check_commercial_usage():from vnai.scope.profile import inspector;return inspector.detect_commercial_usage()
|
75
|
-
def authenticate_for_persistence():from vnai.scope.profile import inspector;return inspector.get_or_create_user_id()
|
76
|
-
def configure_webhook(webhook_id='80b8832b694a75c8ddc811ac7882a3de'):
|
77
|
-
if not webhook_id:return _F
|
78
|
-
from vnai.flow.relay import configure;webhook_url=f"https://botbuilder.larksuite.com/api/trigger-webhook/{webhook_id}";return configure(webhook_url)
|
1
|
+
_L='default'
|
2
|
+
_K='standard'
|
3
|
+
_J='accepted_agreement'
|
4
|
+
_I='environment.json'
|
5
|
+
_H='terms_agreement.txt'
|
6
|
+
_G='timestamp'
|
7
|
+
_F=False
|
8
|
+
_E='id'
|
9
|
+
_D='.vnstock'
|
10
|
+
_C='machine_id'
|
11
|
+
_B=None
|
12
|
+
_A=True
|
13
|
+
import os,pathlib,json,time,threading,functools
|
14
|
+
from datetime import datetime
|
15
|
+
from vnai.beam.quota import guardian,optimize
|
16
|
+
from vnai.beam.metrics import collector,capture
|
17
|
+
from vnai.beam.pulse import monitor
|
18
|
+
from vnai.flow.relay import conduit,configure
|
19
|
+
from vnai.flow.queue import buffer
|
20
|
+
from vnai.scope.profile import inspector
|
21
|
+
from vnai.scope.state import tracker,record
|
22
|
+
import vnai.scope.promo
|
23
|
+
from vnai.scope.promo import present
|
24
|
+
TC_VAR='ACCEPT_TC'
|
25
|
+
TC_VAL='tôi đồng ý'
|
26
|
+
TC_PATH=pathlib.Path.home()/_D/_E/_H
|
27
|
+
TERMS_AND_CONDITIONS='\nKhi tiếp tục sử dụng Vnstock, bạn xác nhận rằng bạn đã đọc, hiểu và đồng ý với Chính sách quyền riêng tư và Điều khoản, điều kiện về giấy phép sử dụng Vnstock.\n\nChi tiết:\n- Giấy phép sử dụng phần mềm: https://vnstocks.com/docs/tai-lieu/giay-phep-su-dung\n- Chính sách quyền riêng tư: https://vnstocks.com/docs/tai-lieu/chinh-sach-quyen-rieng-tu\n'
|
28
|
+
class Core:
|
29
|
+
def __init__(self):self.initialized=_F;self.webhook_url=_B;self.init_time=datetime.now().isoformat();self.home_dir=pathlib.Path.home();self.project_dir=self.home_dir/_D;self.id_dir=self.project_dir/_E;self.terms_file_path=TC_PATH;self.system_info=_B;self.project_dir.mkdir(exist_ok=_A);self.id_dir.mkdir(exist_ok=_A);self.initialize()
|
30
|
+
def initialize(self,webhook_url=_B):
|
31
|
+
if self.initialized:return _A
|
32
|
+
if not self._check_terms():self._accept_terms()
|
33
|
+
from vnai.scope.profile import inspector;inspector.setup_vnstock_environment();present()
|
34
|
+
if webhook_url:self.webhook_url=webhook_url;configure(webhook_url)
|
35
|
+
record('initialization',{_G:datetime.now().isoformat()});self.system_info=inspector.examine();conduit.queue({'type':'system_info','data':{'commercial':inspector.detect_commercial_usage(),'packages':inspector.scan_packages()}},priority='high');self.initialized=_A;return _A
|
36
|
+
def _check_terms(self):return os.path.exists(self.terms_file_path)
|
37
|
+
def _accept_terms(self):
|
38
|
+
system_info=inspector.examine()
|
39
|
+
if TC_VAR in os.environ and os.environ[TC_VAR]==TC_VAL:response=TC_VAL
|
40
|
+
else:response=TC_VAL;os.environ[TC_VAR]=TC_VAL
|
41
|
+
now=datetime.now();signed_agreement=f"""Người dùng có mã nhận dạng {system_info[_C]} đã chấp nhận điều khoản & điều kiện sử dụng Vnstock lúc {now}
|
42
|
+
---
|
43
|
+
|
44
|
+
THÔNG TIN THIẾT BỊ: {json.dumps(system_info,indent=2)}
|
45
|
+
|
46
|
+
Đính kèm bản sao nội dung bạn đã đọc, hiểu rõ và đồng ý dưới đây:
|
47
|
+
{TERMS_AND_CONDITIONS}"""
|
48
|
+
with open(self.terms_file_path,'w',encoding='utf-8')as f:f.write(signed_agreement)
|
49
|
+
env_file=self.id_dir/_I;env_data={_J:_A,_G:now.isoformat(),_C:system_info[_C]}
|
50
|
+
with open(env_file,'w')as f:json.dump(env_data,f)
|
51
|
+
return _A
|
52
|
+
def status(self):return{'initialized':self.initialized,'health':monitor.report(),'metrics':tracker.get_metrics()}
|
53
|
+
def configure_privacy(self,level=_K):from vnai.scope.state import tracker;return tracker.setup_privacy(level)
|
54
|
+
core=Core()
|
55
|
+
def tc_init(webhook_url=_B):return core.initialize(webhook_url)
|
56
|
+
def setup(webhook_url=_B):return core.initialize(webhook_url)
|
57
|
+
def optimize_execution(resource_type=_L):return optimize(resource_type)
|
58
|
+
def agg_execution(resource_type=_L):return optimize(resource_type,ad_cooldown=1500,content_trigger_threshold=100000)
|
59
|
+
def measure_performance(module_type='function'):return capture(module_type)
|
60
|
+
def accept_license_terms(terms_text=_B):
|
61
|
+
if terms_text is _B:terms_text=TERMS_AND_CONDITIONS
|
62
|
+
system_info=inspector.examine();terms_file=pathlib.Path.home()/_D/_E/_H;os.makedirs(os.path.dirname(terms_file),exist_ok=_A)
|
63
|
+
with open(terms_file,'w',encoding='utf-8')as f:f.write(f"Terms accepted at {datetime.now().isoformat()}\n");f.write(f"System: {json.dumps(system_info)}\n\n");f.write(terms_text)
|
64
|
+
return _A
|
65
|
+
def accept_vnstock_terms():
|
66
|
+
from vnai.scope.profile import inspector;system_info=inspector.examine();home_dir=pathlib.Path.home();project_dir=home_dir/_D;project_dir.mkdir(exist_ok=_A);id_dir=project_dir/_E;id_dir.mkdir(exist_ok=_A);env_file=id_dir/_I;env_data={_J:_A,_G:datetime.now().isoformat(),_C:system_info[_C]}
|
67
|
+
try:
|
68
|
+
with open(env_file,'w')as f:json.dump(env_data,f)
|
69
|
+
print('Vnstock terms accepted successfully.');return _A
|
70
|
+
except Exception as e:print(f"Error accepting terms: {e}");return _F
|
71
|
+
def setup_for_colab():from vnai.scope.profile import inspector;inspector.detect_colab_with_delayed_auth(immediate=_A);inspector.setup_vnstock_environment();return'Environment set up for Google Colab'
|
72
|
+
def display_content():return present()
|
73
|
+
def configure_privacy(level=_K):from vnai.scope.state import tracker;return tracker.setup_privacy(level)
|
74
|
+
def check_commercial_usage():from vnai.scope.profile import inspector;return inspector.detect_commercial_usage()
|
75
|
+
def authenticate_for_persistence():from vnai.scope.profile import inspector;return inspector.get_or_create_user_id()
|
76
|
+
def configure_webhook(webhook_id='80b8832b694a75c8ddc811ac7882a3de'):
|
77
|
+
if not webhook_id:return _F
|
78
|
+
from vnai.flow.relay import configure;webhook_url=f"https://botbuilder.larksuite.com/api/trigger-webhook/{webhook_id}";return configure(webhook_url)
|
79
79
|
configure_webhook()
|
vnai/beam/__init__.py
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
from vnai.beam.quota import guardian,optimize
|
2
|
-
from vnai.beam.metrics import collector,capture
|
1
|
+
from vnai.beam.quota import guardian,optimize
|
2
|
+
from vnai.beam.metrics import collector,capture
|
3
3
|
from vnai.beam.pulse import monitor
|
vnai/beam/metrics.py
CHANGED
@@ -1,73 +1,73 @@
|
|
1
|
-
_L='success'
|
2
|
-
_K='buffer_size'
|
3
|
-
_J='request'
|
4
|
-
_I='rate_limit'
|
5
|
-
_H='args'
|
6
|
-
_G='execution_time'
|
7
|
-
_F='timestamp'
|
8
|
-
_E=True
|
9
|
-
_D=False
|
10
|
-
_C='error'
|
11
|
-
_B=None
|
12
|
-
_A='function'
|
13
|
-
import sys,time,threading
|
14
|
-
from datetime import datetime
|
15
|
-
import hashlib,json
|
16
|
-
class Collector:
|
17
|
-
_instance=_B;_lock=threading.Lock()
|
18
|
-
def __new__(cls):
|
19
|
-
with cls._lock:
|
20
|
-
if cls._instance is _B:cls._instance=super(Collector,cls).__new__(cls);cls._instance._initialize()
|
21
|
-
return cls._instance
|
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
|
23
|
-
def record(self,metric_type,data,priority=_B):
|
24
|
-
A='high'
|
25
|
-
if not isinstance(data,dict):data={'value':str(data)}
|
26
|
-
if _F not in data:data[_F]=datetime.now().isoformat()
|
27
|
-
if metric_type!='system_info':data.pop('system',_B);from vnai.scope.profile import inspector;data['machine_id']=inspector.fingerprint()
|
28
|
-
now=time.time();last_time=self._last_record_time.get(metric_type,0)
|
29
|
-
if now-last_time<self.min_interval_per_type and priority!=A:return
|
30
|
-
self._last_record_time[metric_type]=now;data_hash=hashlib.md5(json.dumps(data,sort_keys=_E).encode()).hexdigest()
|
31
|
-
if data_hash in self._recent_hashes and priority!=A:return
|
32
|
-
self._recent_hashes.append(data_hash)
|
33
|
-
if metric_type in self.metrics:
|
34
|
-
self.metrics[metric_type].append(data)
|
35
|
-
if len(self.metrics[metric_type])>self.max_metric_length:self.metrics[metric_type]=self.metrics[metric_type][-self.max_metric_length:]
|
36
|
-
else:self.metrics[_A].append(data)
|
37
|
-
if metric_type==_A:
|
38
|
-
self.function_count+=1
|
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()
|
42
|
-
def _trigger_colab_auth(self):
|
43
|
-
try:from vnai.scope.profile import inspector;inspector.get_or_create_user_id()
|
44
|
-
except:pass
|
45
|
-
def _send_metrics(self):
|
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
|
53
|
-
for(metric_type,data_list)in self.metrics.items():
|
54
|
-
if not data_list:continue
|
55
|
-
for data in data_list:
|
56
|
-
try:
|
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))
|
60
|
-
except Exception as e:continue
|
61
|
-
self.metrics[metric_type]=[]
|
62
|
-
self._sending_metrics=_D
|
63
|
-
def get_metrics_summary(self):return{metric_type:len(data_list)for(metric_type,data_list)in self.metrics.items()}
|
64
|
-
collector=Collector()
|
65
|
-
def capture(module_type=_A):
|
66
|
-
def decorator(func):
|
67
|
-
def wrapper(*args,**kwargs):
|
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})
|
72
|
-
return wrapper
|
1
|
+
_L='success'
|
2
|
+
_K='buffer_size'
|
3
|
+
_J='request'
|
4
|
+
_I='rate_limit'
|
5
|
+
_H='args'
|
6
|
+
_G='execution_time'
|
7
|
+
_F='timestamp'
|
8
|
+
_E=True
|
9
|
+
_D=False
|
10
|
+
_C='error'
|
11
|
+
_B=None
|
12
|
+
_A='function'
|
13
|
+
import sys,time,threading
|
14
|
+
from datetime import datetime
|
15
|
+
import hashlib,json
|
16
|
+
class Collector:
|
17
|
+
_instance=_B;_lock=threading.Lock()
|
18
|
+
def __new__(cls):
|
19
|
+
with cls._lock:
|
20
|
+
if cls._instance is _B:cls._instance=super(Collector,cls).__new__(cls);cls._instance._initialize()
|
21
|
+
return cls._instance
|
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
|
23
|
+
def record(self,metric_type,data,priority=_B):
|
24
|
+
A='high'
|
25
|
+
if not isinstance(data,dict):data={'value':str(data)}
|
26
|
+
if _F not in data:data[_F]=datetime.now().isoformat()
|
27
|
+
if metric_type!='system_info':data.pop('system',_B);from vnai.scope.profile import inspector;data['machine_id']=inspector.fingerprint()
|
28
|
+
now=time.time();last_time=self._last_record_time.get(metric_type,0)
|
29
|
+
if now-last_time<self.min_interval_per_type and priority!=A:return
|
30
|
+
self._last_record_time[metric_type]=now;data_hash=hashlib.md5(json.dumps(data,sort_keys=_E).encode()).hexdigest()
|
31
|
+
if data_hash in self._recent_hashes and priority!=A:return
|
32
|
+
self._recent_hashes.append(data_hash)
|
33
|
+
if metric_type in self.metrics:
|
34
|
+
self.metrics[metric_type].append(data)
|
35
|
+
if len(self.metrics[metric_type])>self.max_metric_length:self.metrics[metric_type]=self.metrics[metric_type][-self.max_metric_length:]
|
36
|
+
else:self.metrics[_A].append(data)
|
37
|
+
if metric_type==_A:
|
38
|
+
self.function_count+=1
|
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()
|
42
|
+
def _trigger_colab_auth(self):
|
43
|
+
try:from vnai.scope.profile import inspector;inspector.get_or_create_user_id()
|
44
|
+
except:pass
|
45
|
+
def _send_metrics(self):
|
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
|
53
|
+
for(metric_type,data_list)in self.metrics.items():
|
54
|
+
if not data_list:continue
|
55
|
+
for data in data_list:
|
56
|
+
try:
|
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))
|
60
|
+
except Exception as e:continue
|
61
|
+
self.metrics[metric_type]=[]
|
62
|
+
self._sending_metrics=_D
|
63
|
+
def get_metrics_summary(self):return{metric_type:len(data_list)for(metric_type,data_list)in self.metrics.items()}
|
64
|
+
collector=Collector()
|
65
|
+
def capture(module_type=_A):
|
66
|
+
def decorator(func):
|
67
|
+
def wrapper(*args,**kwargs):
|
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})
|
72
|
+
return wrapper
|
73
73
|
return decorator
|
vnai/beam/pulse.py
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
_B='status'
|
2
|
-
_A='healthy'
|
3
|
-
import threading,time
|
4
|
-
from datetime import datetime
|
5
|
-
class Monitor:
|
6
|
-
_instance=None;_lock=threading.Lock()
|
7
|
-
def __new__(cls):
|
8
|
-
with cls._lock:
|
9
|
-
if cls._instance is None:cls._instance=super(Monitor,cls).__new__(cls);cls._instance._initialize()
|
10
|
-
return cls._instance
|
11
|
-
def _initialize(self):self.health_status=_A;self.last_check=time.time();self.check_interval=300;self.error_count=0;self.warning_count=0;self.status_history=[];self._start_background_check()
|
12
|
-
def _start_background_check(self):
|
13
|
-
def check_health():
|
14
|
-
while True:
|
15
|
-
try:self.check_health()
|
16
|
-
except:pass
|
17
|
-
time.sleep(self.check_interval)
|
18
|
-
thread=threading.Thread(target=check_health,daemon=True);thread.start()
|
19
|
-
def check_health(self):
|
20
|
-
from vnai.beam.metrics import collector;from vnai.beam.quota import guardian;self.last_check=time.time();metrics_summary=collector.get_metrics_summary();has_errors=metrics_summary.get('error',0)>0;resource_usage=guardian.usage();high_usage=resource_usage>80
|
21
|
-
if has_errors and high_usage:self.health_status='critical';self.error_count+=1
|
22
|
-
elif has_errors or high_usage:self.health_status='warning';self.warning_count+=1
|
23
|
-
else:self.health_status=_A
|
24
|
-
self.status_history.append({'timestamp':datetime.now().isoformat(),_B:self.health_status,'metrics':metrics_summary,'resource_usage':resource_usage})
|
25
|
-
if len(self.status_history)>10:self.status_history=self.status_history[-10:]
|
26
|
-
return self.health_status
|
27
|
-
def report(self):
|
28
|
-
if time.time()-self.last_check>self.check_interval:self.check_health()
|
29
|
-
return{_B:self.health_status,'last_check':datetime.fromtimestamp(self.last_check).isoformat(),'error_count':self.error_count,'warning_count':self.warning_count,'history':self.status_history[-3:]}
|
30
|
-
def reset(self):self.health_status=_A;self.error_count=0;self.warning_count=0;self.status_history=[];self.last_check=time.time()
|
1
|
+
_B='status'
|
2
|
+
_A='healthy'
|
3
|
+
import threading,time
|
4
|
+
from datetime import datetime
|
5
|
+
class Monitor:
|
6
|
+
_instance=None;_lock=threading.Lock()
|
7
|
+
def __new__(cls):
|
8
|
+
with cls._lock:
|
9
|
+
if cls._instance is None:cls._instance=super(Monitor,cls).__new__(cls);cls._instance._initialize()
|
10
|
+
return cls._instance
|
11
|
+
def _initialize(self):self.health_status=_A;self.last_check=time.time();self.check_interval=300;self.error_count=0;self.warning_count=0;self.status_history=[];self._start_background_check()
|
12
|
+
def _start_background_check(self):
|
13
|
+
def check_health():
|
14
|
+
while True:
|
15
|
+
try:self.check_health()
|
16
|
+
except:pass
|
17
|
+
time.sleep(self.check_interval)
|
18
|
+
thread=threading.Thread(target=check_health,daemon=True);thread.start()
|
19
|
+
def check_health(self):
|
20
|
+
from vnai.beam.metrics import collector;from vnai.beam.quota import guardian;self.last_check=time.time();metrics_summary=collector.get_metrics_summary();has_errors=metrics_summary.get('error',0)>0;resource_usage=guardian.usage();high_usage=resource_usage>80
|
21
|
+
if has_errors and high_usage:self.health_status='critical';self.error_count+=1
|
22
|
+
elif has_errors or high_usage:self.health_status='warning';self.warning_count+=1
|
23
|
+
else:self.health_status=_A
|
24
|
+
self.status_history.append({'timestamp':datetime.now().isoformat(),_B:self.health_status,'metrics':metrics_summary,'resource_usage':resource_usage})
|
25
|
+
if len(self.status_history)>10:self.status_history=self.status_history[-10:]
|
26
|
+
return self.health_status
|
27
|
+
def report(self):
|
28
|
+
if time.time()-self.last_check>self.check_interval:self.check_health()
|
29
|
+
return{_B:self.health_status,'last_check':datetime.fromtimestamp(self.last_check).isoformat(),'error_count':self.error_count,'warning_count':self.warning_count,'history':self.status_history[-3:]}
|
30
|
+
def reset(self):self.health_status=_A;self.error_count=0;self.warning_count=0;self.status_history=[];self.last_check=time.time()
|
31
31
|
monitor=Monitor()
|
vnai/beam/quota.py
CHANGED
@@ -1,105 +1,105 @@
|
|
1
|
-
_G='resource_type'
|
2
|
-
_F=False
|
3
|
-
_E=True
|
4
|
-
_D=None
|
5
|
-
_C='default'
|
6
|
-
_B='hour'
|
7
|
-
_A='min'
|
8
|
-
import time,functools,threading
|
9
|
-
from collections import defaultdict
|
10
|
-
from datetime import datetime
|
11
|
-
class RateLimitExceeded(Exception):
|
12
|
-
def __init__(self,resource_type,limit_type=_A,current_usage=_D,limit_value=_D,retry_after=_D):
|
13
|
-
self.resource_type=resource_type;self.limit_type=limit_type;self.current_usage=current_usage;self.limit_value=limit_value;self.retry_after=retry_after;message=f"Bạn đã gửi quá nhiều request tới {resource_type}. "
|
14
|
-
if retry_after:message+=f"Vui lòng thử lại sau {round(retry_after)} giây."
|
15
|
-
else:message+='Vui lòng thêm thời gian chờ giữa các lần gửi request.'
|
16
|
-
super().__init__(message)
|
17
|
-
class Guardian:
|
18
|
-
_instance=_D;_lock=threading.Lock()
|
19
|
-
def __new__(cls):
|
20
|
-
with cls._lock:
|
21
|
-
if cls._instance is _D:cls._instance=super(Guardian,cls).__new__(cls);cls._instance._initialize()
|
22
|
-
return cls._instance
|
23
|
-
def _initialize(self):self.resource_limits=defaultdict(lambda:defaultdict(int));self.usage_counters=defaultdict(lambda:defaultdict(list));self.resource_limits[_C]={_A:60,_B:3000};self.resource_limits['TCBS']={_A:60,_B:3000};self.resource_limits['VCI']={_A:60,_B:3000};self.resource_limits['MBK']={_A:600,_B:36000};self.resource_limits['MAS.ext']={_A:600,_B:36000};self.resource_limits['VCI.ext']={_A:600,_B:36000};self.resource_limits['FMK.ext']={_A:600,_B:36000};self.resource_limits['VND.ext']={_A:600,_B:36000};self.resource_limits['CAF.ext']={_A:600,_B:36000};self.resource_limits['SPL.ext']={_A:600,_B:36000};self.resource_limits['VDS.ext']={_A:600,_B:36000};self.resource_limits['FAD.ext']={_A:600,_B:36000}
|
24
|
-
def verify(self,operation_id,resource_type=_C):
|
25
|
-
E='is_exceeded';D='current_usage';C='limit_value';B='limit_type';A='rate_limit';current_time=time.time();limits=self.resource_limits.get(resource_type,self.resource_limits[_C]);minute_cutoff=current_time-60;self.usage_counters[resource_type][_A]=[t for t in self.usage_counters[resource_type][_A]if t>minute_cutoff];minute_usage=len(self.usage_counters[resource_type][_A]);minute_exceeded=minute_usage>=limits[_A]
|
26
|
-
if minute_exceeded:from vnai.beam.metrics import collector;collector.record(A,{_G:resource_type,B:_A,C:limits[_A],D:minute_usage,E:_E},priority='high');raise RateLimitExceeded(resource_type=resource_type,limit_type=_A,current_usage=minute_usage,limit_value=limits[_A],retry_after=60-current_time%60)
|
27
|
-
hour_cutoff=current_time-3600;self.usage_counters[resource_type][_B]=[t for t in self.usage_counters[resource_type][_B]if t>hour_cutoff];hour_usage=len(self.usage_counters[resource_type][_B]);hour_exceeded=hour_usage>=limits[_B];from vnai.beam.metrics import collector;collector.record(A,{_G:resource_type,B:_B if hour_exceeded else _A,C:limits[_B]if hour_exceeded else limits[_A],D:hour_usage if hour_exceeded else minute_usage,E:hour_exceeded})
|
28
|
-
if hour_exceeded:raise RateLimitExceeded(resource_type=resource_type,limit_type=_B,current_usage=hour_usage,limit_value=limits[_B],retry_after=3600-current_time%3600)
|
29
|
-
self.usage_counters[resource_type][_A].append(current_time);self.usage_counters[resource_type][_B].append(current_time);return _E
|
30
|
-
def usage(self,resource_type=_C):current_time=time.time();limits=self.resource_limits.get(resource_type,self.resource_limits[_C]);minute_cutoff=current_time-60;hour_cutoff=current_time-3600;self.usage_counters[resource_type][_A]=[t for t in self.usage_counters[resource_type][_A]if t>minute_cutoff];self.usage_counters[resource_type][_B]=[t for t in self.usage_counters[resource_type][_B]if t>hour_cutoff];minute_usage=len(self.usage_counters[resource_type][_A]);hour_usage=len(self.usage_counters[resource_type][_B]);minute_percentage=minute_usage/limits[_A]*100 if limits[_A]>0 else 0;hour_percentage=hour_usage/limits[_B]*100 if limits[_B]>0 else 0;return max(minute_percentage,hour_percentage)
|
31
|
-
def get_limit_status(self,resource_type=_C):E='reset_in_seconds';D='remaining';C='percentage';B='limit';A='usage';current_time=time.time();limits=self.resource_limits.get(resource_type,self.resource_limits[_C]);minute_cutoff=current_time-60;hour_cutoff=current_time-3600;minute_usage=len([t for t in self.usage_counters[resource_type][_A]if t>minute_cutoff]);hour_usage=len([t for t in self.usage_counters[resource_type][_B]if t>hour_cutoff]);return{_G:resource_type,'minute_limit':{A:minute_usage,B:limits[_A],C:minute_usage/limits[_A]*100 if limits[_A]>0 else 0,D:max(0,limits[_A]-minute_usage),E:60-current_time%60},'hour_limit':{A:hour_usage,B:limits[_B],C:hour_usage/limits[_B]*100 if limits[_B]>0 else 0,D:max(0,limits[_B]-hour_usage),E:3600-current_time%3600}}
|
32
|
-
guardian=Guardian()
|
33
|
-
class CleanErrorContext:
|
34
|
-
_last_message_time=0;_message_cooldown=5
|
35
|
-
def __enter__(self):return self
|
36
|
-
def __exit__(self,exc_type,exc_val,exc_tb):
|
37
|
-
if exc_type is RateLimitExceeded:
|
38
|
-
current_time=time.time()
|
39
|
-
if current_time-CleanErrorContext._last_message_time>=CleanErrorContext._message_cooldown:print(f"\n⚠️ {str(exc_val)}\n");CleanErrorContext._last_message_time=current_time
|
40
|
-
import sys;sys.exit(f"Rate limit exceeded. {str(exc_val)} Process terminated.");return _F
|
41
|
-
return _F
|
42
|
-
def optimize(resource_type=_C,loop_threshold=10,time_window=5,ad_cooldown=150,content_trigger_threshold=3,max_retries=2,backoff_factor=2,debug=_F):
|
43
|
-
if callable(resource_type):func=resource_type;return _create_wrapper(func,_C,loop_threshold,time_window,ad_cooldown,content_trigger_threshold,max_retries,backoff_factor,debug)
|
44
|
-
if loop_threshold<2:raise ValueError(f"loop_threshold must be at least 2, got {loop_threshold}")
|
45
|
-
if time_window<=0:raise ValueError(f"time_window must be positive, got {time_window}")
|
46
|
-
if content_trigger_threshold<1:raise ValueError(f"content_trigger_threshold must be at least 1, got {content_trigger_threshold}")
|
47
|
-
if max_retries<0:raise ValueError(f"max_retries must be non-negative, got {max_retries}")
|
48
|
-
if backoff_factor<=0:raise ValueError(f"backoff_factor must be positive, got {backoff_factor}")
|
49
|
-
def decorator(func):return _create_wrapper(func,resource_type,loop_threshold,time_window,ad_cooldown,content_trigger_threshold,max_retries,backoff_factor,debug)
|
50
|
-
return decorator
|
51
|
-
def _create_wrapper(func,resource_type,loop_threshold,time_window,ad_cooldown,content_trigger_threshold,max_retries,backoff_factor,debug):
|
52
|
-
call_history=[];last_ad_time=0;consecutive_loop_detections=0;session_displayed=_F;session_start_time=time.time();session_timeout=1800
|
53
|
-
@functools.wraps(func)
|
54
|
-
def wrapper(*args,**kwargs):
|
55
|
-
E='timestamp';D='environment';C='error';B='function';A='loop';nonlocal last_ad_time,consecutive_loop_detections,session_displayed,session_start_time;current_time=time.time();content_triggered=_F
|
56
|
-
if current_time-session_start_time>session_timeout:session_displayed=_F;session_start_time=current_time
|
57
|
-
retries=0
|
58
|
-
while _E:
|
59
|
-
call_history.append(current_time)
|
60
|
-
while call_history and current_time-call_history[0]>time_window:call_history.pop(0)
|
61
|
-
loop_detected=len(call_history)>=loop_threshold
|
62
|
-
if debug and loop_detected:print(f"[OPTIMIZE] Đã phát hiện vòng lặp cho {func.__name__}: {len(call_history)} lần gọi trong {time_window}s")
|
63
|
-
if loop_detected:
|
64
|
-
consecutive_loop_detections+=1
|
65
|
-
if debug:print(f"[OPTIMIZE] Số lần phát hiện vòng lặp liên tiếp: {consecutive_loop_detections}/{content_trigger_threshold}")
|
66
|
-
else:consecutive_loop_detections=0
|
67
|
-
should_show_content=consecutive_loop_detections>=content_trigger_threshold and current_time-last_ad_time>=ad_cooldown and not session_displayed
|
68
|
-
if should_show_content:
|
69
|
-
last_ad_time=current_time;consecutive_loop_detections=0;content_triggered=_E;session_displayed=_E
|
70
|
-
if debug:print(f"[OPTIMIZE] Đã kích hoạt nội dung cho {func.__name__}")
|
71
|
-
try:
|
72
|
-
from vnai.scope.promo import manager
|
73
|
-
try:from vnai.scope.profile import inspector;environment=inspector.examine().get(D,_D);manager.present_content(environment=environment,context=A)
|
74
|
-
except ImportError:manager.present_content(context=A)
|
75
|
-
except ImportError:print(f"Phát hiện vòng lặp: Hàm '{func.__name__}' đang được gọi trong một vòng lặp")
|
76
|
-
except Exception as e:
|
77
|
-
if debug:print(f"[OPTIMIZE] Lỗi khi hiển thị nội dung: {str(e)}")
|
78
|
-
try:
|
79
|
-
with CleanErrorContext():guardian.verify(func.__name__,resource_type)
|
80
|
-
except RateLimitExceeded as e:
|
81
|
-
from vnai.beam.metrics import collector;collector.record(C,{B:func.__name__,C:str(e),'context':'resource_verification',_G:resource_type,'retry_attempt':retries},priority='high')
|
82
|
-
if not session_displayed:
|
83
|
-
try:
|
84
|
-
from vnai.scope.promo import manager
|
85
|
-
try:from vnai.scope.profile import inspector;environment=inspector.examine().get(D,_D);manager.present_content(environment=environment,context=A);session_displayed=_E;last_ad_time=current_time
|
86
|
-
except ImportError:manager.present_content(context=A);session_displayed=_E;last_ad_time=current_time
|
87
|
-
except Exception:pass
|
88
|
-
if retries<max_retries:
|
89
|
-
wait_time=backoff_factor**retries;retries+=1
|
90
|
-
if hasattr(e,'retry_after')and e.retry_after:wait_time=min(wait_time,e.retry_after)
|
91
|
-
if debug:print(f"[OPTIMIZE] Đã đạt giới hạn tốc độ cho {func.__name__}, thử lại sau {wait_time} giây (lần thử {retries}/{max_retries})")
|
92
|
-
time.sleep(wait_time);continue
|
93
|
-
else:raise
|
94
|
-
start_time=time.time();success=_F;error=_D
|
95
|
-
try:result=func(*args,**kwargs);success=_E;return result
|
96
|
-
except Exception as e:error=str(e);raise
|
97
|
-
finally:
|
98
|
-
execution_time=time.time()-start_time
|
99
|
-
try:
|
100
|
-
from vnai.beam.metrics import collector;collector.record(B,{B:func.__name__,_G:resource_type,'execution_time':execution_time,'success':success,C:error,'in_loop':loop_detected,'loop_depth':len(call_history),'content_triggered':content_triggered,E:datetime.now().isoformat(),'retry_count':retries if retries>0 else _D})
|
101
|
-
if content_triggered:collector.record('ad_opportunity',{B:func.__name__,_G:resource_type,'call_frequency':len(call_history)/time_window,'consecutive_loops':consecutive_loop_detections,E:datetime.now().isoformat()})
|
102
|
-
except ImportError:pass
|
103
|
-
break
|
104
|
-
return wrapper
|
1
|
+
_G='resource_type'
|
2
|
+
_F=False
|
3
|
+
_E=True
|
4
|
+
_D=None
|
5
|
+
_C='default'
|
6
|
+
_B='hour'
|
7
|
+
_A='min'
|
8
|
+
import time,functools,threading
|
9
|
+
from collections import defaultdict
|
10
|
+
from datetime import datetime
|
11
|
+
class RateLimitExceeded(Exception):
|
12
|
+
def __init__(self,resource_type,limit_type=_A,current_usage=_D,limit_value=_D,retry_after=_D):
|
13
|
+
self.resource_type=resource_type;self.limit_type=limit_type;self.current_usage=current_usage;self.limit_value=limit_value;self.retry_after=retry_after;message=f"Bạn đã gửi quá nhiều request tới {resource_type}. "
|
14
|
+
if retry_after:message+=f"Vui lòng thử lại sau {round(retry_after)} giây."
|
15
|
+
else:message+='Vui lòng thêm thời gian chờ giữa các lần gửi request.'
|
16
|
+
super().__init__(message)
|
17
|
+
class Guardian:
|
18
|
+
_instance=_D;_lock=threading.Lock()
|
19
|
+
def __new__(cls):
|
20
|
+
with cls._lock:
|
21
|
+
if cls._instance is _D:cls._instance=super(Guardian,cls).__new__(cls);cls._instance._initialize()
|
22
|
+
return cls._instance
|
23
|
+
def _initialize(self):self.resource_limits=defaultdict(lambda:defaultdict(int));self.usage_counters=defaultdict(lambda:defaultdict(list));self.resource_limits[_C]={_A:60,_B:3000};self.resource_limits['TCBS']={_A:60,_B:3000};self.resource_limits['VCI']={_A:60,_B:3000};self.resource_limits['MBK']={_A:600,_B:36000};self.resource_limits['MAS.ext']={_A:600,_B:36000};self.resource_limits['VCI.ext']={_A:600,_B:36000};self.resource_limits['FMK.ext']={_A:600,_B:36000};self.resource_limits['VND.ext']={_A:600,_B:36000};self.resource_limits['CAF.ext']={_A:600,_B:36000};self.resource_limits['SPL.ext']={_A:600,_B:36000};self.resource_limits['VDS.ext']={_A:600,_B:36000};self.resource_limits['FAD.ext']={_A:600,_B:36000}
|
24
|
+
def verify(self,operation_id,resource_type=_C):
|
25
|
+
E='is_exceeded';D='current_usage';C='limit_value';B='limit_type';A='rate_limit';current_time=time.time();limits=self.resource_limits.get(resource_type,self.resource_limits[_C]);minute_cutoff=current_time-60;self.usage_counters[resource_type][_A]=[t for t in self.usage_counters[resource_type][_A]if t>minute_cutoff];minute_usage=len(self.usage_counters[resource_type][_A]);minute_exceeded=minute_usage>=limits[_A]
|
26
|
+
if minute_exceeded:from vnai.beam.metrics import collector;collector.record(A,{_G:resource_type,B:_A,C:limits[_A],D:minute_usage,E:_E},priority='high');raise RateLimitExceeded(resource_type=resource_type,limit_type=_A,current_usage=minute_usage,limit_value=limits[_A],retry_after=60-current_time%60)
|
27
|
+
hour_cutoff=current_time-3600;self.usage_counters[resource_type][_B]=[t for t in self.usage_counters[resource_type][_B]if t>hour_cutoff];hour_usage=len(self.usage_counters[resource_type][_B]);hour_exceeded=hour_usage>=limits[_B];from vnai.beam.metrics import collector;collector.record(A,{_G:resource_type,B:_B if hour_exceeded else _A,C:limits[_B]if hour_exceeded else limits[_A],D:hour_usage if hour_exceeded else minute_usage,E:hour_exceeded})
|
28
|
+
if hour_exceeded:raise RateLimitExceeded(resource_type=resource_type,limit_type=_B,current_usage=hour_usage,limit_value=limits[_B],retry_after=3600-current_time%3600)
|
29
|
+
self.usage_counters[resource_type][_A].append(current_time);self.usage_counters[resource_type][_B].append(current_time);return _E
|
30
|
+
def usage(self,resource_type=_C):current_time=time.time();limits=self.resource_limits.get(resource_type,self.resource_limits[_C]);minute_cutoff=current_time-60;hour_cutoff=current_time-3600;self.usage_counters[resource_type][_A]=[t for t in self.usage_counters[resource_type][_A]if t>minute_cutoff];self.usage_counters[resource_type][_B]=[t for t in self.usage_counters[resource_type][_B]if t>hour_cutoff];minute_usage=len(self.usage_counters[resource_type][_A]);hour_usage=len(self.usage_counters[resource_type][_B]);minute_percentage=minute_usage/limits[_A]*100 if limits[_A]>0 else 0;hour_percentage=hour_usage/limits[_B]*100 if limits[_B]>0 else 0;return max(minute_percentage,hour_percentage)
|
31
|
+
def get_limit_status(self,resource_type=_C):E='reset_in_seconds';D='remaining';C='percentage';B='limit';A='usage';current_time=time.time();limits=self.resource_limits.get(resource_type,self.resource_limits[_C]);minute_cutoff=current_time-60;hour_cutoff=current_time-3600;minute_usage=len([t for t in self.usage_counters[resource_type][_A]if t>minute_cutoff]);hour_usage=len([t for t in self.usage_counters[resource_type][_B]if t>hour_cutoff]);return{_G:resource_type,'minute_limit':{A:minute_usage,B:limits[_A],C:minute_usage/limits[_A]*100 if limits[_A]>0 else 0,D:max(0,limits[_A]-minute_usage),E:60-current_time%60},'hour_limit':{A:hour_usage,B:limits[_B],C:hour_usage/limits[_B]*100 if limits[_B]>0 else 0,D:max(0,limits[_B]-hour_usage),E:3600-current_time%3600}}
|
32
|
+
guardian=Guardian()
|
33
|
+
class CleanErrorContext:
|
34
|
+
_last_message_time=0;_message_cooldown=5
|
35
|
+
def __enter__(self):return self
|
36
|
+
def __exit__(self,exc_type,exc_val,exc_tb):
|
37
|
+
if exc_type is RateLimitExceeded:
|
38
|
+
current_time=time.time()
|
39
|
+
if current_time-CleanErrorContext._last_message_time>=CleanErrorContext._message_cooldown:print(f"\n⚠️ {str(exc_val)}\n");CleanErrorContext._last_message_time=current_time
|
40
|
+
import sys;sys.exit(f"Rate limit exceeded. {str(exc_val)} Process terminated.");return _F
|
41
|
+
return _F
|
42
|
+
def optimize(resource_type=_C,loop_threshold=10,time_window=5,ad_cooldown=150,content_trigger_threshold=3,max_retries=2,backoff_factor=2,debug=_F):
|
43
|
+
if callable(resource_type):func=resource_type;return _create_wrapper(func,_C,loop_threshold,time_window,ad_cooldown,content_trigger_threshold,max_retries,backoff_factor,debug)
|
44
|
+
if loop_threshold<2:raise ValueError(f"loop_threshold must be at least 2, got {loop_threshold}")
|
45
|
+
if time_window<=0:raise ValueError(f"time_window must be positive, got {time_window}")
|
46
|
+
if content_trigger_threshold<1:raise ValueError(f"content_trigger_threshold must be at least 1, got {content_trigger_threshold}")
|
47
|
+
if max_retries<0:raise ValueError(f"max_retries must be non-negative, got {max_retries}")
|
48
|
+
if backoff_factor<=0:raise ValueError(f"backoff_factor must be positive, got {backoff_factor}")
|
49
|
+
def decorator(func):return _create_wrapper(func,resource_type,loop_threshold,time_window,ad_cooldown,content_trigger_threshold,max_retries,backoff_factor,debug)
|
50
|
+
return decorator
|
51
|
+
def _create_wrapper(func,resource_type,loop_threshold,time_window,ad_cooldown,content_trigger_threshold,max_retries,backoff_factor,debug):
|
52
|
+
call_history=[];last_ad_time=0;consecutive_loop_detections=0;session_displayed=_F;session_start_time=time.time();session_timeout=1800
|
53
|
+
@functools.wraps(func)
|
54
|
+
def wrapper(*args,**kwargs):
|
55
|
+
E='timestamp';D='environment';C='error';B='function';A='loop';nonlocal last_ad_time,consecutive_loop_detections,session_displayed,session_start_time;current_time=time.time();content_triggered=_F
|
56
|
+
if current_time-session_start_time>session_timeout:session_displayed=_F;session_start_time=current_time
|
57
|
+
retries=0
|
58
|
+
while _E:
|
59
|
+
call_history.append(current_time)
|
60
|
+
while call_history and current_time-call_history[0]>time_window:call_history.pop(0)
|
61
|
+
loop_detected=len(call_history)>=loop_threshold
|
62
|
+
if debug and loop_detected:print(f"[OPTIMIZE] Đã phát hiện vòng lặp cho {func.__name__}: {len(call_history)} lần gọi trong {time_window}s")
|
63
|
+
if loop_detected:
|
64
|
+
consecutive_loop_detections+=1
|
65
|
+
if debug:print(f"[OPTIMIZE] Số lần phát hiện vòng lặp liên tiếp: {consecutive_loop_detections}/{content_trigger_threshold}")
|
66
|
+
else:consecutive_loop_detections=0
|
67
|
+
should_show_content=consecutive_loop_detections>=content_trigger_threshold and current_time-last_ad_time>=ad_cooldown and not session_displayed
|
68
|
+
if should_show_content:
|
69
|
+
last_ad_time=current_time;consecutive_loop_detections=0;content_triggered=_E;session_displayed=_E
|
70
|
+
if debug:print(f"[OPTIMIZE] Đã kích hoạt nội dung cho {func.__name__}")
|
71
|
+
try:
|
72
|
+
from vnai.scope.promo import manager
|
73
|
+
try:from vnai.scope.profile import inspector;environment=inspector.examine().get(D,_D);manager.present_content(environment=environment,context=A)
|
74
|
+
except ImportError:manager.present_content(context=A)
|
75
|
+
except ImportError:print(f"Phát hiện vòng lặp: Hàm '{func.__name__}' đang được gọi trong một vòng lặp")
|
76
|
+
except Exception as e:
|
77
|
+
if debug:print(f"[OPTIMIZE] Lỗi khi hiển thị nội dung: {str(e)}")
|
78
|
+
try:
|
79
|
+
with CleanErrorContext():guardian.verify(func.__name__,resource_type)
|
80
|
+
except RateLimitExceeded as e:
|
81
|
+
from vnai.beam.metrics import collector;collector.record(C,{B:func.__name__,C:str(e),'context':'resource_verification',_G:resource_type,'retry_attempt':retries},priority='high')
|
82
|
+
if not session_displayed:
|
83
|
+
try:
|
84
|
+
from vnai.scope.promo import manager
|
85
|
+
try:from vnai.scope.profile import inspector;environment=inspector.examine().get(D,_D);manager.present_content(environment=environment,context=A);session_displayed=_E;last_ad_time=current_time
|
86
|
+
except ImportError:manager.present_content(context=A);session_displayed=_E;last_ad_time=current_time
|
87
|
+
except Exception:pass
|
88
|
+
if retries<max_retries:
|
89
|
+
wait_time=backoff_factor**retries;retries+=1
|
90
|
+
if hasattr(e,'retry_after')and e.retry_after:wait_time=min(wait_time,e.retry_after)
|
91
|
+
if debug:print(f"[OPTIMIZE] Đã đạt giới hạn tốc độ cho {func.__name__}, thử lại sau {wait_time} giây (lần thử {retries}/{max_retries})")
|
92
|
+
time.sleep(wait_time);continue
|
93
|
+
else:raise
|
94
|
+
start_time=time.time();success=_F;error=_D
|
95
|
+
try:result=func(*args,**kwargs);success=_E;return result
|
96
|
+
except Exception as e:error=str(e);raise
|
97
|
+
finally:
|
98
|
+
execution_time=time.time()-start_time
|
99
|
+
try:
|
100
|
+
from vnai.beam.metrics import collector;collector.record(B,{B:func.__name__,_G:resource_type,'execution_time':execution_time,'success':success,C:error,'in_loop':loop_detected,'loop_depth':len(call_history),'content_triggered':content_triggered,E:datetime.now().isoformat(),'retry_count':retries if retries>0 else _D})
|
101
|
+
if content_triggered:collector.record('ad_opportunity',{B:func.__name__,_G:resource_type,'call_frequency':len(call_history)/time_window,'consecutive_loops':consecutive_loop_detections,E:datetime.now().isoformat()})
|
102
|
+
except ImportError:pass
|
103
|
+
break
|
104
|
+
return wrapper
|
105
105
|
def rate_limit_status(resource_type=_C):return guardian.get_limit_status(resource_type)
|
vnai/flow/__init__.py
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
from vnai.flow.relay import conduit,configure
|
1
|
+
from vnai.flow.relay import conduit,configure
|
2
2
|
from vnai.flow.queue import buffer
|