vnai 2.1.3__tar.gz → 2.1.4__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.
- {vnai-2.1.3 → vnai-2.1.4}/PKG-INFO +2 -1
- {vnai-2.1.3 → vnai-2.1.4}/pyproject.toml +3 -2
- vnai-2.1.4/vnai/__init__.py +164 -0
- vnai-2.1.4/vnai/beam/__init__.py +3 -0
- vnai-2.1.4/vnai/beam/metrics.py +167 -0
- vnai-2.1.4/vnai/beam/pulse.py +79 -0
- vnai-2.1.4/vnai/beam/quota.py +333 -0
- vnai-2.1.4/vnai/flow/__init__.py +2 -0
- vnai-2.1.4/vnai/flow/queue.py +100 -0
- vnai-2.1.4/vnai/flow/relay.py +356 -0
- vnai-2.1.4/vnai/scope/__init__.py +4 -0
- vnai-2.1.4/vnai/scope/profile.py +579 -0
- vnai-2.1.4/vnai/scope/promo.py +278 -0
- vnai-2.1.4/vnai/scope/state.py +155 -0
- {vnai-2.1.3 → vnai-2.1.4}/vnai.egg-info/PKG-INFO +2 -1
- {vnai-2.1.3 → vnai-2.1.4}/vnai.egg-info/requires.txt +1 -0
- vnai-2.1.3/vnai/__init__.py +0 -79
- vnai-2.1.3/vnai/beam/__init__.py +0 -3
- vnai-2.1.3/vnai/beam/metrics.py +0 -73
- vnai-2.1.3/vnai/beam/pulse.py +0 -31
- vnai-2.1.3/vnai/beam/quota.py +0 -105
- vnai-2.1.3/vnai/flow/__init__.py +0 -2
- vnai-2.1.3/vnai/flow/queue.py +0 -55
- vnai-2.1.3/vnai/flow/relay.py +0 -161
- vnai-2.1.3/vnai/scope/__init__.py +0 -4
- vnai-2.1.3/vnai/scope/profile.py +0 -223
- vnai-2.1.3/vnai/scope/promo.py +0 -112
- vnai-2.1.3/vnai/scope/state.py +0 -74
- {vnai-2.1.3 → vnai-2.1.4}/README.md +0 -0
- {vnai-2.1.3 → vnai-2.1.4}/setup.cfg +0 -0
- {vnai-2.1.3 → vnai-2.1.4}/vnai.egg-info/SOURCES.txt +0 -0
- {vnai-2.1.3 → vnai-2.1.4}/vnai.egg-info/dependency_links.txt +0 -0
- {vnai-2.1.3 → vnai-2.1.4}/vnai.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: vnai
|
3
|
-
Version: 2.1.
|
3
|
+
Version: 2.1.4
|
4
4
|
Summary: Vnstock Analytics Interface
|
5
5
|
Author-email: Vnstock Analytics Team <support@vnstocks.com>
|
6
6
|
License: proprietary
|
@@ -14,6 +14,7 @@ Requires-Python: >=3.7
|
|
14
14
|
Description-Content-Type: text/markdown
|
15
15
|
Requires-Dist: requests>=2.25.0
|
16
16
|
Requires-Dist: psutil>=5.8.0
|
17
|
+
Requires-Dist: vnii>=0.0.9
|
17
18
|
Provides-Extra: dev
|
18
19
|
Requires-Dist: pytest>=6.0.0; extra == "dev"
|
19
20
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "vnai"
|
7
|
-
version = "2.1.
|
7
|
+
version = "2.1.4"
|
8
8
|
description = "Vnstock Analytics Interface"
|
9
9
|
readme = "README.md"
|
10
10
|
requires-python = ">=3.7"
|
@@ -15,7 +15,8 @@ license = {text = "proprietary"}
|
|
15
15
|
urls = {"Homepage" = "https://vnstocks.com"}
|
16
16
|
dependencies = [
|
17
17
|
"requests>=2.25.0",
|
18
|
-
"psutil>=5.8.0"
|
18
|
+
"psutil>=5.8.0",
|
19
|
+
"vnii>=0.0.9"
|
19
20
|
]
|
20
21
|
classifiers = [
|
21
22
|
"Programming Language :: Python :: 3",
|
@@ -0,0 +1,164 @@
|
|
1
|
+
import os
|
2
|
+
import pathlib
|
3
|
+
import json
|
4
|
+
import time
|
5
|
+
import threading
|
6
|
+
import functools
|
7
|
+
from datetime import datetime
|
8
|
+
from vnai.beam.quota import guardian, optimize
|
9
|
+
from vnai.beam.metrics import collector, capture
|
10
|
+
from vnai.beam.pulse import monitor
|
11
|
+
from vnai.flow.relay import conduit, configure
|
12
|
+
from vnai.flow.queue import buffer
|
13
|
+
from vnai.scope.profile import inspector
|
14
|
+
from vnai.scope.state import tracker, record
|
15
|
+
import vnai.scope.promo
|
16
|
+
from vnai.scope.promo import present
|
17
|
+
TC_VAR ="ACCEPT_TC"
|
18
|
+
TC_VAL ="tôi đồng ý"
|
19
|
+
TC_PATH = pathlib.Path.home() /".vnstock" /"id" /"terms_agreement.txt"
|
20
|
+
TERMS_AND_CONDITIONS ="""
|
21
|
+
Khi 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.
|
22
|
+
Chi tiết:
|
23
|
+
- Giấy phép sử dụng phần mềm: https://vnstocks.com/docs/tai-lieu/giay-phep-su-dung
|
24
|
+
- Chính sách quyền riêng tư: https://vnstocks.com/docs/tai-lieu/chinh-sach-quyen-rieng-tu
|
25
|
+
"""
|
26
|
+
|
27
|
+
class Core:
|
28
|
+
def __init__(self):
|
29
|
+
self.initialized = False
|
30
|
+
self.webhook_url = None
|
31
|
+
self.init_time = datetime.now().isoformat()
|
32
|
+
self.home_dir = pathlib.Path.home()
|
33
|
+
self.project_dir = self.home_dir /".vnstock"
|
34
|
+
self.id_dir = self.project_dir /'id'
|
35
|
+
self.terms_file_path = TC_PATH
|
36
|
+
self.system_info = None
|
37
|
+
self.project_dir.mkdir(exist_ok=True)
|
38
|
+
self.id_dir.mkdir(exist_ok=True)
|
39
|
+
self.initialize()
|
40
|
+
|
41
|
+
def initialize(self, webhook_url=None):
|
42
|
+
if self.initialized:
|
43
|
+
return True
|
44
|
+
if not self._check_terms():
|
45
|
+
self._accept_terms()
|
46
|
+
from vnai.scope.profile import inspector
|
47
|
+
inspector.setup_vnstock_environment()
|
48
|
+
present()
|
49
|
+
if webhook_url:
|
50
|
+
self.webhook_url = webhook_url
|
51
|
+
configure(webhook_url)
|
52
|
+
record("initialization", {"timestamp": datetime.now().isoformat()})
|
53
|
+
self.system_info = inspector.examine()
|
54
|
+
conduit.queue({
|
55
|
+
"type":"system_info",
|
56
|
+
"data": {
|
57
|
+
"commercial": inspector.detect_commercial_usage(),
|
58
|
+
"packages": inspector.scan_packages()
|
59
|
+
}
|
60
|
+
}, priority="high")
|
61
|
+
self.initialized = True
|
62
|
+
return True
|
63
|
+
|
64
|
+
def _check_terms(self):
|
65
|
+
return os.path.exists(self.terms_file_path)
|
66
|
+
|
67
|
+
def _accept_terms(self):
|
68
|
+
system_info = inspector.examine()
|
69
|
+
if TC_VAR in os.environ and os.environ[TC_VAR] == TC_VAL:
|
70
|
+
response = TC_VAL
|
71
|
+
else:
|
72
|
+
response = TC_VAL
|
73
|
+
os.environ[TC_VAR] = TC_VAL
|
74
|
+
now = datetime.now()
|
75
|
+
signed_agreement = (
|
76
|
+
f"Người dùng có mã nhận dạng {system_info['machine_id']} đã chấp nhận "
|
77
|
+
f"điều khoản & điều kiện sử dụng Vnstock lúc {now}\n"
|
78
|
+
f"---\n\n"
|
79
|
+
f"THÔNG TIN THIẾT BỊ: {json.dumps(system_info, indent=2)}\n\n"
|
80
|
+
f"Đính kèm bản sao nội dung bạn đã đọc, hiểu rõ và đồng ý dưới đây:\n"
|
81
|
+
f"{TERMS_AND_CONDITIONS}"
|
82
|
+
)
|
83
|
+
with open(self.terms_file_path,"w", encoding="utf-8") as f:
|
84
|
+
f.write(signed_agreement)
|
85
|
+
env_file = self.id_dir /"environment.json"
|
86
|
+
env_data = {
|
87
|
+
"accepted_agreement": True,
|
88
|
+
"timestamp": now.isoformat(),
|
89
|
+
"machine_id": system_info['machine_id']
|
90
|
+
}
|
91
|
+
with open(env_file,"w") as f:
|
92
|
+
json.dump(env_data, f)
|
93
|
+
return True
|
94
|
+
|
95
|
+
def status(self):
|
96
|
+
return {
|
97
|
+
"initialized": self.initialized,
|
98
|
+
"health": monitor.report(),
|
99
|
+
"metrics": tracker.get_metrics()
|
100
|
+
}
|
101
|
+
|
102
|
+
def configure_privacy(self, level="standard"):
|
103
|
+
from vnai.scope.state import tracker
|
104
|
+
return tracker.setup_privacy(level)
|
105
|
+
core = Core()
|
106
|
+
|
107
|
+
def tc_init(webhook_url=None):
|
108
|
+
return core.initialize(webhook_url)
|
109
|
+
|
110
|
+
def setup(webhook_url=None):
|
111
|
+
return core.initialize(webhook_url)
|
112
|
+
|
113
|
+
def optimize_execution(resource_type="default"):
|
114
|
+
return optimize(resource_type)
|
115
|
+
|
116
|
+
def accept_license_terms(terms_text=None):
|
117
|
+
if terms_text is None:
|
118
|
+
terms_text = TERMS_AND_CONDITIONS
|
119
|
+
system_info = inspector.examine()
|
120
|
+
terms_file = pathlib.Path.home() /".vnstock" /"id" /"terms_agreement.txt"
|
121
|
+
os.makedirs(os.path.dirname(terms_file), exist_ok=True)
|
122
|
+
with open(terms_file,"w", encoding="utf-8") as f:
|
123
|
+
f.write(f"Terms accepted at {datetime.now().isoformat()}\n")
|
124
|
+
f.write(f"System: {json.dumps(system_info)}\n\n")
|
125
|
+
f.write(terms_text)
|
126
|
+
return True
|
127
|
+
|
128
|
+
def accept_vnstock_terms():
|
129
|
+
from vnai.scope.profile import inspector
|
130
|
+
system_info = inspector.examine()
|
131
|
+
home_dir = pathlib.Path.home()
|
132
|
+
project_dir = home_dir /".vnstock"
|
133
|
+
project_dir.mkdir(exist_ok=True)
|
134
|
+
id_dir = project_dir /'id'
|
135
|
+
id_dir.mkdir(exist_ok=True)
|
136
|
+
env_file = id_dir /"environment.json"
|
137
|
+
env_data = {
|
138
|
+
"accepted_agreement": True,
|
139
|
+
"timestamp": datetime.now().isoformat(),
|
140
|
+
"machine_id": system_info['machine_id']
|
141
|
+
}
|
142
|
+
try:
|
143
|
+
with open(env_file,"w") as f:
|
144
|
+
json.dump(env_data, f)
|
145
|
+
print("Vnstock terms accepted successfully.")
|
146
|
+
return True
|
147
|
+
except Exception as e:
|
148
|
+
print(f"Error accepting terms: {e}")
|
149
|
+
return False
|
150
|
+
|
151
|
+
def display_content():
|
152
|
+
return present()
|
153
|
+
|
154
|
+
def check_commercial_usage():
|
155
|
+
from vnai.scope.profile import inspector
|
156
|
+
return inspector.detect_commercial_usage()
|
157
|
+
|
158
|
+
def configure_webhook(webhook_id=''):
|
159
|
+
if not webhook_id:
|
160
|
+
return False
|
161
|
+
from vnai.flow.relay import configure
|
162
|
+
webhook_url =f'https://botbuilder.larksuite.com/api/trigger-webhook/{webhook_id}'
|
163
|
+
return configure(webhook_url)
|
164
|
+
configure_webhook()
|
@@ -0,0 +1,167 @@
|
|
1
|
+
import sys
|
2
|
+
import time
|
3
|
+
import threading
|
4
|
+
from datetime import datetime
|
5
|
+
import hashlib
|
6
|
+
import json
|
7
|
+
|
8
|
+
class Collector:
|
9
|
+
_instance = None
|
10
|
+
_lock = threading.Lock()
|
11
|
+
|
12
|
+
def __new__(cls):
|
13
|
+
with cls._lock:
|
14
|
+
if cls._instance is None:
|
15
|
+
cls._instance = super(Collector, cls).__new__(cls)
|
16
|
+
cls._instance._initialize()
|
17
|
+
return cls._instance
|
18
|
+
|
19
|
+
def _initialize(self):
|
20
|
+
self.metrics = {
|
21
|
+
"function": [],
|
22
|
+
"rate_limit": [],
|
23
|
+
"request": [],
|
24
|
+
"error": []
|
25
|
+
}
|
26
|
+
self.thresholds = {
|
27
|
+
"buffer_size": 50,
|
28
|
+
"error_threshold": 0.1,
|
29
|
+
"performance_threshold": 5.0
|
30
|
+
}
|
31
|
+
self.function_count = 0
|
32
|
+
self.colab_auth_triggered = False
|
33
|
+
self.max_metric_length = 200
|
34
|
+
self._last_record_time = {}
|
35
|
+
self.min_interval_per_type = 0.5
|
36
|
+
self._recent_hashes = []
|
37
|
+
self._sending_metrics = False
|
38
|
+
|
39
|
+
def record(self, metric_type, data, priority=None):
|
40
|
+
if not isinstance(data, dict):
|
41
|
+
data = {"value": str(data)}
|
42
|
+
if"timestamp" not in data:
|
43
|
+
data["timestamp"] = datetime.now().isoformat()
|
44
|
+
if metric_type !="system_info":
|
45
|
+
data.pop("system", None)
|
46
|
+
from vnai.scope.profile import inspector
|
47
|
+
data["machine_id"] = inspector.fingerprint()
|
48
|
+
now = time.time()
|
49
|
+
last_time = self._last_record_time.get(metric_type, 0)
|
50
|
+
if now - last_time < self.min_interval_per_type and priority !="high":
|
51
|
+
return
|
52
|
+
self._last_record_time[metric_type] = now
|
53
|
+
data_hash = hashlib.md5(json.dumps(data, sort_keys=True).encode()).hexdigest()
|
54
|
+
if data_hash in self._recent_hashes and priority !="high":
|
55
|
+
return
|
56
|
+
self._recent_hashes.append(data_hash)
|
57
|
+
if metric_type in self.metrics:
|
58
|
+
self.metrics[metric_type].append(data)
|
59
|
+
if len(self.metrics[metric_type]) > self.max_metric_length:
|
60
|
+
self.metrics[metric_type] = self.metrics[metric_type][-self.max_metric_length:]
|
61
|
+
else:
|
62
|
+
self.metrics["function"].append(data)
|
63
|
+
if metric_type =="function":
|
64
|
+
self.function_count += 1
|
65
|
+
if self.function_count > 10 and not self.colab_auth_triggered and'google.colab' in sys.modules:
|
66
|
+
self.colab_auth_triggered = True
|
67
|
+
threading.Thread(target=self._trigger_colab_auth, daemon=True).start()
|
68
|
+
if sum(len(metric_list) for metric_list in self.metrics.values()) >= self.thresholds["buffer_size"]:
|
69
|
+
self._send_metrics()
|
70
|
+
if priority =="high" or metric_type =="error":
|
71
|
+
self._send_metrics()
|
72
|
+
|
73
|
+
def _trigger_colab_auth(self):
|
74
|
+
try:
|
75
|
+
from vnai.scope.profile import inspector
|
76
|
+
inspector.get_or_create_user_id()
|
77
|
+
except:
|
78
|
+
pass
|
79
|
+
|
80
|
+
def _send_metrics(self):
|
81
|
+
if self._sending_metrics:
|
82
|
+
return
|
83
|
+
self._sending_metrics = True
|
84
|
+
try:
|
85
|
+
from vnai.flow.relay import track_function_call, track_rate_limit, track_api_request
|
86
|
+
except ImportError:
|
87
|
+
for metric_type in self.metrics:
|
88
|
+
self.metrics[metric_type] = []
|
89
|
+
self._sending_metrics = False
|
90
|
+
return
|
91
|
+
for metric_type, data_list in self.metrics.items():
|
92
|
+
if not data_list:
|
93
|
+
continue
|
94
|
+
for data in data_list:
|
95
|
+
try:
|
96
|
+
if metric_type =="function":
|
97
|
+
track_function_call(
|
98
|
+
function_name=data.get("function","unknown"),
|
99
|
+
source=data.get("source","vnai"),
|
100
|
+
execution_time=data.get("execution_time", 0),
|
101
|
+
success=data.get("success", True),
|
102
|
+
error=data.get("error"),
|
103
|
+
args=data.get("args")
|
104
|
+
)
|
105
|
+
elif metric_type =="rate_limit":
|
106
|
+
track_rate_limit(
|
107
|
+
source=data.get("source","vnai"),
|
108
|
+
limit_type=data.get("limit_type","unknown"),
|
109
|
+
limit_value=data.get("limit_value", 0),
|
110
|
+
current_usage=data.get("current_usage", 0),
|
111
|
+
is_exceeded=data.get("is_exceeded", False)
|
112
|
+
)
|
113
|
+
elif metric_type =="request":
|
114
|
+
track_api_request(
|
115
|
+
endpoint=data.get("endpoint","unknown"),
|
116
|
+
source=data.get("source","vnai"),
|
117
|
+
method=data.get("method","GET"),
|
118
|
+
status_code=data.get("status_code", 200),
|
119
|
+
execution_time=data.get("execution_time", 0),
|
120
|
+
request_size=data.get("request_size", 0),
|
121
|
+
response_size=data.get("response_size", 0)
|
122
|
+
)
|
123
|
+
except Exception as e:
|
124
|
+
continue
|
125
|
+
self.metrics[metric_type] = []
|
126
|
+
self._sending_metrics = False
|
127
|
+
|
128
|
+
def get_metrics_summary(self):
|
129
|
+
return {
|
130
|
+
metric_type: len(data_list)
|
131
|
+
for metric_type, data_list in self.metrics.items()
|
132
|
+
}
|
133
|
+
collector = Collector()
|
134
|
+
|
135
|
+
def capture(module_type="function"):
|
136
|
+
def decorator(func):
|
137
|
+
def wrapper(*args, **kwargs):
|
138
|
+
start_time = time.time()
|
139
|
+
success = False
|
140
|
+
error = None
|
141
|
+
try:
|
142
|
+
result = func(*args, **kwargs)
|
143
|
+
success = True
|
144
|
+
return result
|
145
|
+
except Exception as e:
|
146
|
+
error = str(e)
|
147
|
+
collector.record("error", {
|
148
|
+
"function": func.__name__,
|
149
|
+
"error": error,
|
150
|
+
"args": str(args)[:100] if args else None
|
151
|
+
})
|
152
|
+
raise
|
153
|
+
finally:
|
154
|
+
execution_time = time.time() - start_time
|
155
|
+
collector.record(
|
156
|
+
module_type,
|
157
|
+
{
|
158
|
+
"function": func.__name__,
|
159
|
+
"execution_time": execution_time,
|
160
|
+
"success": success,
|
161
|
+
"error": error,
|
162
|
+
"timestamp": datetime.now().isoformat(),
|
163
|
+
"args": str(args)[:100] if args else None
|
164
|
+
}
|
165
|
+
)
|
166
|
+
return wrapper
|
167
|
+
return decorator
|
@@ -0,0 +1,79 @@
|
|
1
|
+
import threading
|
2
|
+
import time
|
3
|
+
from datetime import datetime
|
4
|
+
|
5
|
+
class Monitor:
|
6
|
+
_instance = None
|
7
|
+
_lock = threading.Lock()
|
8
|
+
|
9
|
+
def __new__(cls):
|
10
|
+
with cls._lock:
|
11
|
+
if cls._instance is None:
|
12
|
+
cls._instance = super(Monitor, cls).__new__(cls)
|
13
|
+
cls._instance._initialize()
|
14
|
+
return cls._instance
|
15
|
+
|
16
|
+
def _initialize(self):
|
17
|
+
self.health_status ="healthy"
|
18
|
+
self.last_check = time.time()
|
19
|
+
self.check_interval = 300
|
20
|
+
self.error_count = 0
|
21
|
+
self.warning_count = 0
|
22
|
+
self.status_history = []
|
23
|
+
self._start_background_check()
|
24
|
+
|
25
|
+
def _start_background_check(self):
|
26
|
+
def check_health():
|
27
|
+
while True:
|
28
|
+
try:
|
29
|
+
self.check_health()
|
30
|
+
except:
|
31
|
+
pass
|
32
|
+
time.sleep(self.check_interval)
|
33
|
+
thread = threading.Thread(target=check_health, daemon=True)
|
34
|
+
thread.start()
|
35
|
+
|
36
|
+
def check_health(self):
|
37
|
+
from vnai.beam.metrics import collector
|
38
|
+
from vnai.beam.quota import guardian
|
39
|
+
self.last_check = time.time()
|
40
|
+
metrics_summary = collector.get_metrics_summary()
|
41
|
+
has_errors = metrics_summary.get("error", 0) > 0
|
42
|
+
resource_usage = guardian.usage()
|
43
|
+
high_usage = resource_usage > 80
|
44
|
+
if has_errors and high_usage:
|
45
|
+
self.health_status ="critical"
|
46
|
+
self.error_count += 1
|
47
|
+
elif has_errors or high_usage:
|
48
|
+
self.health_status ="warning"
|
49
|
+
self.warning_count += 1
|
50
|
+
else:
|
51
|
+
self.health_status ="healthy"
|
52
|
+
self.status_history.append({
|
53
|
+
"timestamp": datetime.now().isoformat(),
|
54
|
+
"status": self.health_status,
|
55
|
+
"metrics": metrics_summary,
|
56
|
+
"resource_usage": resource_usage
|
57
|
+
})
|
58
|
+
if len(self.status_history) > 10:
|
59
|
+
self.status_history = self.status_history[-10:]
|
60
|
+
return self.health_status
|
61
|
+
|
62
|
+
def report(self):
|
63
|
+
if time.time() - self.last_check > self.check_interval:
|
64
|
+
self.check_health()
|
65
|
+
return {
|
66
|
+
"status": self.health_status,
|
67
|
+
"last_check": datetime.fromtimestamp(self.last_check).isoformat(),
|
68
|
+
"error_count": self.error_count,
|
69
|
+
"warning_count": self.warning_count,
|
70
|
+
"history": self.status_history[-3:],
|
71
|
+
}
|
72
|
+
|
73
|
+
def reset(self):
|
74
|
+
self.health_status ="healthy"
|
75
|
+
self.error_count = 0
|
76
|
+
self.warning_count = 0
|
77
|
+
self.status_history = []
|
78
|
+
self.last_check = time.time()
|
79
|
+
monitor = Monitor()
|