-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_monitor.py
More file actions
80 lines (66 loc) · 3.28 KB
/
cli_monitor.py
File metadata and controls
80 lines (66 loc) · 3.28 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
import asyncio
import websockets
import json
import datetime
import sys
# Windows console color support
import os
os.system('')
class Colors:
HEADER = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
async def monitor():
uri = "ws://localhost:8000/ws"
print(f"{Colors.HEADER}正在连接到系统监控终端 ({uri})...{Colors.ENDC}")
try:
async with websockets.connect(uri) as websocket:
print(f"{Colors.GREEN}系统已连接! 正在等待数据流...{Colors.ENDC}")
print("-" * 50)
while True:
try:
message = await websocket.recv()
data = json.loads(message)
if data.get("type") == "system_status":
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
# Process Signals
if "signals" in data and data["signals"]:
for signal in data["signals"]:
symbol = signal.get("symbol")
trend = signal.get("trend")
price = signal.get("price")
color = Colors.GREEN if trend == "bullish" else (Colors.FAIL if trend == "bearish" else Colors.WARNING)
print(f"[{timestamp}] {Colors.CYAN}[分析师]{Colors.ENDC} {symbol} 趋势: {color}{trend}{Colors.ENDC} 价格: {price}")
# Process Trades
if "trades" in data and data["trades"]:
for trade in data["trades"]:
action = trade.get("action")
symbol = trade.get("symbol")
price = trade.get("price")
pnl = trade.get("pnl")
pnl_str = f" PnL: {pnl}" if pnl is not None else ""
print(f"[{timestamp}] {Colors.HEADER}[交易员]{Colors.ENDC} 动作: {Colors.BOLD}{action}{Colors.ENDC} {symbol} @ {price}{pnl_str}")
# Optional: Print Agent Status changes if needed, but might be too noisy
# for agent in data.get("agents", []):
# if agent["status"] != "idle":
# print(f"[{timestamp}] [{agent['role']}] 状态: {agent['status']}")
except json.JSONDecodeError:
print(f"收到非JSON消息: {message}")
except Exception as e:
print(f"处理消息时出错: {e}")
except ConnectionRefusedError:
print(f"{Colors.FAIL}连接失败: 无法连接到后端服务器。请确保后端正在运行 (localhost:8000)。{Colors.ENDC}")
except Exception as e:
print(f"{Colors.FAIL}发生错误: {e}{Colors.ENDC}")
if __name__ == "__main__":
try:
asyncio.run(monitor())
except KeyboardInterrupt:
print(f"\n{Colors.WARNING}监控已停止。{Colors.ENDC}")
sys.exit(0)