-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
368 lines (285 loc) · 12 KB
/
utils.py
File metadata and controls
368 lines (285 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import logging
import time
import functools
import json
import os
from typing import Callable, Any, Dict, Optional
from datetime import datetime, timezone
def retry_with_backoff(max_retries: int = 3, backoff_factor: float = 2.0,
exceptions: tuple = (Exception,)) -> Callable:
"""Decorator to retry functions with exponential backoff"""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(*args, **kwargs) -> Any:
logger = logging.getLogger(func.__module__)
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except exceptions as e:
if attempt == max_retries - 1:
logger.error(f"Function {func.__name__} failed after {max_retries} attempts: {str(e)}")
raise
wait_time = backoff_factor ** attempt
logger.warning(f"Function {func.__name__} failed (attempt {attempt + 1}/{max_retries}), "
f"retrying in {wait_time:.1f}s: {str(e)}")
time.sleep(wait_time)
return None
return wrapper
return decorator
def safe_execute(func: Callable, *args, **kwargs) -> tuple[bool, Any]:
"""Safely execute a function and return (success, result)"""
try:
result = func(*args, **kwargs)
return True, result
except Exception as e:
logger = logging.getLogger(__name__)
logger.error(f"Error executing {func.__name__}: {str(e)}")
return False, str(e)
class DataStore:
"""Simple JSON-based data store for persistence"""
def __init__(self, filename: str):
self.filename = filename
self.logger = logging.getLogger(__name__)
def save(self, data: Dict) -> bool:
"""Save data to file"""
try:
# Add timestamp
data['_timestamp'] = datetime.now(timezone.utc).isoformat()
with open(self.filename, 'w') as f:
json.dump(data, f, indent=2)
return True
except Exception as e:
self.logger.error(f"Error saving data to {self.filename}: {str(e)}")
return False
def load(self) -> Optional[Dict]:
"""Load data from file"""
try:
if not os.path.exists(self.filename):
return None
with open(self.filename, 'r') as f:
data = json.load(f)
return data
except Exception as e:
self.logger.error(f"Error loading data from {self.filename}: {str(e)}")
return None
def exists(self) -> bool:
"""Check if data file exists"""
return os.path.exists(self.filename)
class RateLimiter:
"""Simple rate limiter for API calls"""
def __init__(self, max_calls: int, time_window: float):
self.max_calls = max_calls
self.time_window = time_window
self.calls = []
def is_allowed(self) -> bool:
"""Check if a call is allowed under rate limits"""
now = time.time()
# Remove old calls outside the time window
self.calls = [call_time for call_time in self.calls if now - call_time < self.time_window]
# Check if we can make another call
if len(self.calls) < self.max_calls:
self.calls.append(now)
return True
return False
def wait_time(self) -> float:
"""Get the time to wait before next call is allowed"""
if not self.calls:
return 0.0
oldest_call = min(self.calls)
wait_time = self.time_window - (time.time() - oldest_call)
return max(0.0, wait_time)
class HealthChecker:
"""System health monitoring"""
def __init__(self, config: Dict):
self.config = config
self.logger = logging.getLogger(__name__)
self.health_data = DataStore('health_status.json')
def check_system_health(self) -> Dict:
"""Perform comprehensive system health check"""
health_status = {
'timestamp': datetime.now(timezone.utc).isoformat(),
'overall_status': 'healthy',
'components': {}
}
# Check disk space
disk_usage = self._check_disk_space()
health_status['components']['disk'] = disk_usage
# Check memory usage
memory_usage = self._check_memory_usage()
health_status['components']['memory'] = memory_usage
# Check log file size
log_status = self._check_log_files()
health_status['components']['logs'] = log_status
# Check configuration
config_status = self._check_configuration()
health_status['components']['configuration'] = config_status
# Determine overall status
component_statuses = [comp['status'] for comp in health_status['components'].values()]
if 'critical' in component_statuses:
health_status['overall_status'] = 'critical'
elif 'warning' in component_statuses:
health_status['overall_status'] = 'warning'
# Save health status
self.health_data.save(health_status)
return health_status
def _check_disk_space(self) -> Dict:
"""Check available disk space"""
try:
import shutil
total, used, free = shutil.disk_usage('.')
free_percent = (free / total) * 100
status = 'healthy'
if free_percent < 5:
status = 'critical'
elif free_percent < 10:
status = 'warning'
return {
'status': status,
'free_space_gb': free // (1024**3),
'free_percent': round(free_percent, 1),
'total_space_gb': total // (1024**3)
}
except Exception as e:
self.logger.error(f"Error checking disk space: {str(e)}")
return {'status': 'unknown', 'error': str(e)}
def _check_memory_usage(self) -> Dict:
"""Check memory usage"""
try:
import psutil
memory = psutil.virtual_memory()
used_percent = memory.percent
status = 'healthy'
if used_percent > 90:
status = 'critical'
elif used_percent > 80:
status = 'warning'
return {
'status': status,
'used_percent': round(used_percent, 1),
'available_gb': memory.available // (1024**3),
'total_gb': memory.total // (1024**3)
}
except ImportError:
# psutil not available, skip memory check
return {'status': 'unknown', 'error': 'psutil not available'}
except Exception as e:
self.logger.error(f"Error checking memory usage: {str(e)}")
return {'status': 'unknown', 'error': str(e)}
def _check_log_files(self) -> Dict:
"""Check log file sizes"""
try:
log_file = self.config['logging']['file']
max_size_mb = self.config['logging']['max_file_size_mb']
if not os.path.exists(log_file):
return {'status': 'healthy', 'file_size_mb': 0}
file_size = os.path.getsize(log_file)
file_size_mb = file_size / (1024 * 1024)
status = 'healthy'
if file_size_mb > max_size_mb * 0.9:
status = 'warning'
return {
'status': status,
'file_size_mb': round(file_size_mb, 1),
'max_size_mb': max_size_mb
}
except Exception as e:
self.logger.error(f"Error checking log files: {str(e)}")
return {'status': 'unknown', 'error': str(e)}
def _check_configuration(self) -> Dict:
"""Check configuration validity"""
try:
# Check for placeholder values
issues = []
# Check API keys
for chain, api_key in self.config['alchemy']['api_keys'].items():
if api_key.startswith('YOUR_'):
issues.append(f"Placeholder API key for {chain}")
# Check Telegram config
if self.config['telegram']['bot_token'].startswith('YOUR_'):
issues.append("Placeholder Telegram bot token")
if self.config['telegram']['chat_id'].startswith('YOUR_'):
issues.append("Placeholder Telegram chat ID")
# Check contract addresses
for chain, address in self.config['exchange_contracts'].items():
if address.startswith('0x123'):
issues.append(f"Placeholder contract address for {chain}")
status = 'critical' if issues else 'healthy'
return {
'status': status,
'issues': issues
}
except Exception as e:
self.logger.error(f"Error checking configuration: {str(e)}")
return {'status': 'unknown', 'error': str(e)}
def format_amount(amount: float, decimals: int = 18) -> str:
"""Format token amount for display"""
if amount == 0:
return "0"
# Convert from wei if needed
if amount > 1e15: # Likely in wei
amount = amount / (10 ** decimals)
if amount >= 1e6:
return f"{amount/1e6:.2f}M"
elif amount >= 1e3:
return f"{amount/1e3:.2f}K"
elif amount >= 1:
return f"{amount:.2f}"
else:
return f"{amount:.6f}"
def validate_address(address: str) -> bool:
"""Validate Ethereum address format"""
if not address.startswith('0x'):
return False
if len(address) != 42:
return False
try:
int(address[2:], 16)
return True
except ValueError:
return False
def get_explorer_url(chain: str, tx_hash: str = None, address: str = None) -> str:
"""Get block explorer URL for transaction or address"""
explorers = {
'ethereum': 'https://etherscan.io',
'arbitrum': 'https://arbiscan.io',
'base': 'https://basescan.org',
'sonic': 'https://sonicscan.org',
'blast': 'https://blastscan.io'
}
base_url = explorers.get(chain, 'https://etherscan.io')
if tx_hash:
return f"{base_url}/tx/{tx_hash}"
elif address:
return f"{base_url}/address/{address}"
else:
return base_url
def truncate_hash(hash_str: str, length: int = 10) -> str:
"""Truncate hash for display"""
if len(hash_str) <= length:
return hash_str
return f"{hash_str[:length]}...{hash_str[-6:]}"
class SystemMonitor:
"""Monitor system performance and resource usage"""
def __init__(self):
self.logger = logging.getLogger(__name__)
self.start_time = time.time()
def get_uptime(self) -> float:
"""Get system uptime in hours"""
return (time.time() - self.start_time) / 3600
def log_performance_metrics(self):
"""Log current performance metrics"""
try:
import psutil
# CPU usage
cpu_percent = psutil.cpu_percent(interval=1)
# Memory usage
memory = psutil.virtual_memory()
# Disk usage
disk = psutil.disk_usage('.')
self.logger.info(f"Performance metrics - CPU: {cpu_percent}%, "
f"Memory: {memory.percent}%, "
f"Disk: {(disk.used/disk.total)*100:.1f}%")
except ImportError:
self.logger.debug("psutil not available, skipping performance metrics")
except Exception as e:
self.logger.error(f"Error logging performance metrics: {str(e)}")