Spaces:
Sleeping
Sleeping
File size: 9,959 Bytes
6e1537c |
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 |
"""
Background Agents for Data Collection and System Monitoring
دو agent برای جمعآوری داده و نظارت بر سیستم
"""
import asyncio
import logging
from datetime import datetime
from typing import Dict, Any
import httpx
logger = logging.getLogger(__name__)
class DataCollectionAgent:
"""Agent برای جمعآوری دورهای داده"""
def __init__(self):
self.is_running = False
self.stats = {
"total_collections": 0,
"successful_collections": 0,
"failed_collections": 0,
"last_collection": None
}
self.collected_data = {
"market": None,
"sentiment": None,
"trending": None,
"news": None
}
async def start(self):
"""شروع agent"""
if self.is_running:
logger.warning("Data collection agent is already running")
return
self.is_running = True
logger.info("🤖 Data Collection Agent started")
# شروع loop جمعآوری
asyncio.create_task(self._collection_loop())
async def stop(self):
"""توقف agent"""
self.is_running = False
logger.info("🛑 Data Collection Agent stopped")
async def _collection_loop(self):
"""حلقه اصلی جمعآوری داده"""
while self.is_running:
try:
await self.collect_all_data()
await asyncio.sleep(300) # هر 5 دقیقه
except Exception as e:
logger.error(f"Error in collection loop: {e}")
await asyncio.sleep(60)
async def collect_all_data(self):
"""جمعآوری تمام دادهها"""
self.stats["total_collections"] += 1
try:
async with httpx.AsyncClient(timeout=30.0) as client:
# جمعآوری موازی
tasks = [
self._collect_market_data(client),
self._collect_sentiment_data(client),
self._collect_trending_data(client),
self._collect_news_data(client)
]
results = await asyncio.gather(*tasks, return_exceptions=True)
# بررسی نتایج
success_count = sum(1 for r in results if not isinstance(r, Exception))
if success_count > 0:
self.stats["successful_collections"] += 1
else:
self.stats["failed_collections"] += 1
self.stats["last_collection"] = datetime.utcnow().isoformat() + "Z"
logger.info(f"✅ Data collection completed: {success_count}/4 successful")
except Exception as e:
self.stats["failed_collections"] += 1
logger.error(f"Data collection failed: {e}")
async def _collect_market_data(self, client: httpx.AsyncClient):
"""جمعآوری داده بازار"""
try:
response = await client.get("https://api.coingecko.com/api/v3/global")
if response.status_code == 200:
self.collected_data["market"] = response.json()
logger.debug("✅ Market data collected")
except Exception as e:
logger.debug(f"Failed to collect market data: {e}")
raise
async def _collect_sentiment_data(self, client: httpx.AsyncClient):
"""جمعآوری داده sentiment"""
try:
response = await client.get("https://api.alternative.me/fng/?limit=1")
if response.status_code == 200:
self.collected_data["sentiment"] = response.json()
logger.debug("✅ Sentiment data collected")
except Exception as e:
logger.debug(f"Failed to collect sentiment data: {e}")
raise
async def _collect_trending_data(self, client: httpx.AsyncClient):
"""جمعآوری داده trending"""
try:
response = await client.get("https://api.coingecko.com/api/v3/search/trending")
if response.status_code == 200:
self.collected_data["trending"] = response.json()
logger.debug("✅ Trending data collected")
except Exception as e:
logger.debug(f"Failed to collect trending data: {e}")
raise
async def _collect_news_data(self, client: httpx.AsyncClient):
"""جمعآوری اخبار"""
try:
response = await client.get(
"https://cryptopanic.com/api/v1/posts/",
params={"auth_token": "free", "public": "true", "kind": "news"}
)
if response.status_code == 200:
self.collected_data["news"] = response.json()
logger.debug("✅ News data collected")
except Exception as e:
logger.debug(f"Failed to collect news data: {e}")
raise
def get_stats(self) -> Dict[str, Any]:
"""دریافت آمار agent"""
return {
**self.stats,
"is_running": self.is_running,
"has_data": any(v is not None for v in self.collected_data.values())
}
def get_collected_data(self) -> Dict[str, Any]:
"""دریافت دادههای جمعآوری شده"""
return self.collected_data
class SystemMonitorAgent:
"""Agent برای نظارت بر وضعیت سیستم"""
def __init__(self):
self.is_running = False
self.stats = {
"total_checks": 0,
"system_healthy": True,
"last_check": None,
"alerts": []
}
self.system_metrics = {
"cpu_usage": 0,
"memory_usage": 0,
"active_connections": 0,
"requests_per_minute": 0,
"error_rate": 0
}
async def start(self):
"""شروع agent"""
if self.is_running:
logger.warning("System monitor agent is already running")
return
self.is_running = True
logger.info("🤖 System Monitor Agent started")
# شروع loop نظارت
asyncio.create_task(self._monitoring_loop())
async def stop(self):
"""توقف agent"""
self.is_running = False
logger.info("🛑 System Monitor Agent stopped")
async def _monitoring_loop(self):
"""حلقه اصلی نظارت"""
while self.is_running:
try:
await self.check_system_health()
await asyncio.sleep(60) # هر 1 دقیقه
except Exception as e:
logger.error(f"Error in monitoring loop: {e}")
await asyncio.sleep(30)
async def check_system_health(self):
"""بررسی سلامت سیستم"""
self.stats["total_checks"] += 1
try:
# شبیهسازی metrics (در production از psutil استفاده کنید)
import random
self.system_metrics = {
"cpu_usage": random.randint(20, 60),
"memory_usage": random.randint(40, 70),
"active_connections": random.randint(1, 10),
"requests_per_minute": random.randint(50, 150),
"error_rate": random.uniform(0, 5)
}
# بررسی آستانهها
alerts = []
if self.system_metrics["cpu_usage"] > 80:
alerts.append("High CPU usage detected")
if self.system_metrics["memory_usage"] > 85:
alerts.append("High memory usage detected")
if self.system_metrics["error_rate"] > 10:
alerts.append("High error rate detected")
self.stats["alerts"] = alerts
self.stats["system_healthy"] = len(alerts) == 0
self.stats["last_check"] = datetime.utcnow().isoformat() + "Z"
if alerts:
logger.warning(f"⚠️ System alerts: {', '.join(alerts)}")
else:
logger.debug("✅ System health check passed")
except Exception as e:
logger.error(f"System health check failed: {e}")
self.stats["system_healthy"] = False
def get_stats(self) -> Dict[str, Any]:
"""دریافت آمار agent"""
return {
**self.stats,
"is_running": self.is_running
}
def get_metrics(self) -> Dict[str, Any]:
"""دریافت metrics سیستم"""
return self.system_metrics
# Global instances
data_agent = DataCollectionAgent()
monitor_agent = SystemMonitorAgent()
async def start_agents():
"""شروع تمام agents"""
await data_agent.start()
await monitor_agent.start()
logger.info("✅ All background agents started")
async def stop_agents():
"""توقف تمام agents"""
await data_agent.stop()
await monitor_agent.stop()
logger.info("✅ All background agents stopped")
def get_agents_status() -> Dict[str, Any]:
"""دریافت وضعیت تمام agents"""
return {
"data_collection_agent": data_agent.get_stats(),
"system_monitor_agent": monitor_agent.get_stats(),
"timestamp": datetime.utcnow().isoformat() + "Z"
}
|