diff --git a/jtl-wafi-agent.py b/jtl-wafi-agent.py index d57a668..5618fc8 100644 --- a/jtl-wafi-agent.py +++ b/jtl-wafi-agent.py @@ -4043,6 +4043,9 @@ class JTLWAFiAgent: elif event_type == 'command.whois': await self._handle_whois_command(event_data) + elif event_type == 'command.reset_stats': + await self._handle_reset_stats_command(event_data) + elif event_type == 'log.subscribe': shop = event_data.get('shop') if shop: @@ -4589,6 +4592,41 @@ class JTLWAFiAgent: 'error': str(e) }) + async def _handle_reset_stats_command(self, data: Dict[str, Any]): + """Setzt alle In-Memory Caches und Statistiken zurück.""" + global _ip_info_cache, _whois_cache, _shop_stats_trackers + + command_id = data.get('command_id', 'unknown') + + try: + # IP-Info Cache leeren + _ip_info_cache.clear() + logger.info("IP-Info Cache geleert") + + # WHOIS Cache leeren + _whois_cache.clear() + logger.info("WHOIS Cache geleert") + + # LiveStats Tracker für alle Shops leeren + _shop_stats_trackers.clear() + logger.info("LiveStats Tracker geleert") + + await self._send_event('command.result', { + 'command_id': command_id, + 'status': 'success', + 'message': f'Agent {self.hostname}: Alle Caches zurückgesetzt' + }) + + logger.info("Reset Stats: Alle In-Memory Daten zurückgesetzt") + + except Exception as e: + logger.error(f"Reset Stats Fehler: {e}") + await self._send_event('command.result', { + 'command_id': command_id, + 'status': 'error', + 'message': str(e) + }) + async def _periodic_tasks(self): """Führt periodische Tasks aus.""" while self.running: diff --git a/jtl-wafi-dashboard.py b/jtl-wafi-dashboard.py index a55bb20..6278764 100644 --- a/jtl-wafi-dashboard.py +++ b/jtl-wafi-dashboard.py @@ -1277,6 +1277,72 @@ async def get_shop_history_api(domain: str, request: Request): return {"domain": domain, **data} +@app.post("/api/reset-stats") +async def reset_stats(request: Request): + """Setzt alle In-Memory Statistiken zurück (Dashboard + alle Agents).""" + user = await get_current_user(request) + if not user: + raise HTTPException(401) + + # 1. Reset-Command an alle verbundenen Agents senden + agent_count = 0 + command_id = f"reset_{int(time.time())}" + for agent_id in list(manager.agent_connections.keys()): + await manager.send_to_agent(agent_id, { + 'type': 'command.reset_stats', + 'data': {'command_id': command_id} + }) + agent_count += 1 + + # 2. Dashboard Shops zurücksetzen (Statistiken, History) + for shop in store.shops.values(): + shop.log_entries = 0 + shop.bot_bans = 0 + shop.country_bans = 0 + shop.active_bot_bans = 0 + shop.active_country_bans = 0 + shop.banned_bots = [] + shop.banned_countries = [] + shop.req_per_min = 0.0 + shop.unique_ips = 0 + shop.unique_bots = 0 + shop.unique_countries = 0 + shop.top_bots = {} + shop.top_ips = [] + shop.top_countries = {} + shop.top_requests = {} + shop.human_requests = 0 + shop.bot_requests = 0 + shop.human_rpm = 0.0 + shop.bot_rpm = 0.0 + shop.history.clear() + shop.bot_history.clear() + shop.country_history.clear() + + # 3. Agents zurücksetzen (nicht löschen, nur Stats) + for agent in store.agents.values(): + agent.load_1m = 0.0 + agent.load_5m = 0.0 + agent.memory_percent = 0.0 + + # 4. Broadcast an alle Browser um UI zu aktualisieren + await manager.broadcast_to_browsers({ + 'type': 'refresh', + 'data': { + 'agents': store.get_all_agents(), + 'shops': store.get_all_shops(), + 'stats': { + 'agents_online': len([a for a in store.agents.values() if a.status == 'online']), + 'shops_active': len([s for s in store.shops.values() if s.status == 'active']), + 'total_bans': 0, + 'total_rpm': 0 + } + } + }) + + return {"success": True, "message": f"Dashboard + {agent_count} Agent(s) zurückgesetzt"} + + @app.post("/api/update-dashboard") async def update_dashboard(request: Request): """Dashboard selbst updaten.""" @@ -1629,7 +1695,7 @@ def get_dashboard_html() -> str: