File size: 5,284 Bytes
6578cff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
WebSocket Test Client β€” Monitor what the hub is broadcasting
No external dependencies beyond websockets (already installed)

Usage:
    python3 test_websocket.py [--subscribe|--publish] [--space ASSET_NAME]
"""

import asyncio
import json
import sys
import websockets
from datetime import datetime

async def test_subscribe(hub_url: str):
    """Listen to what the hub is broadcasting."""
    print(f"[*] Connecting to hub subscriber at {hub_url}/ws/subscribe")
    
    try:
        async with websockets.connect(f"{hub_url}/ws/subscribe") as ws:
            print(f"[βœ“] Connected! Listening for metrics updates...\n")
            
            count = 0
            while True:
                try:
                    msg = await asyncio.wait_for(ws.recv(), timeout=10.0)
                    count += 1
                    data = json.loads(msg)
                    
                    ts = datetime.now().strftime("%H:%M:%S")
                    print(f"[{ts}] Message #{count}:")
                    print(f"  {json.dumps(data, indent=2)}\n")
                    
                except asyncio.TimeoutError:
                    print("[!] No messages received for 10 seconds...")
                    print("    β†’ Asset spaces may not be connected yet")
                    sys.exit(1)
                    
    except Exception as e:
        print(f"[βœ—] Connection failed: {e}")
        print(f"    Make sure hub is running and accessible at {hub_url}")
        sys.exit(1)


async def test_publish(hub_url: str, space_name: str):
    """Send a test metric to the hub."""
    print(f"[*] Connecting to hub publisher for space: {space_name}")
    
    test_message = {
        "training": {
            "training_steps": 9999,
            "actor_loss": 0.123,
            "critic_loss": 0.456,
            "avn_loss": 0.789,
            "avn_accuracy": 0.95,
        },
        "voting": {
            "dominant_signal": "BUY",
            "buy_count": 42,
            "sell_count": 18,
        }
    }
    
    try:
        uri = f"{hub_url}/ws/publish/{space_name}"
        print(f"[*] Connecting to {uri}")
        
        async with websockets.connect(uri) as ws:
            print(f"[βœ“] Connected! Sending test message...")
            await ws.send(json.dumps(test_message))
            print(f"[βœ“] Sent:\n{json.dumps(test_message, indent=2)}")
            
            # Keep connection open for 5 seconds
            print(f"[*] Keeping connection open for 5 seconds...")
            await asyncio.sleep(5)
            print(f"[βœ“] Done!")
            
    except Exception as e:
        print(f"[βœ—] Error: {e}")
        sys.exit(1)


async def main():
    # Default hub URL (adjust if needed)
    hub_url = "ws://127.0.0.1:7860"
    
    if len(sys.argv) > 1:
        if "--subscribe" in sys.argv:
            print("=" * 60)
            print("QUASAR Hub WebSocket Monitor (Subscribe Mode)")
            print("=" * 60)
            await test_subscribe(hub_url)
            
        elif "--publish" in sys.argv:
            space_name = "TEST_ASSET"
            if "--space" in sys.argv:
                idx = sys.argv.index("--space")
                if idx + 1 < len(sys.argv):
                    space_name = sys.argv[idx + 1]
            
            print("=" * 60)
            print(f"QUASAR Hub WebSocket Test (Publish Mode)")
            print("=" * 60)
            await test_publish(hub_url, space_name)
        else:
            print_usage()
    else:
        print_usage()


def print_usage():
    print("""
╔════════════════════════════════════════════════════════════════╗
β•‘            QUASAR WebSocket Test Tool v1.0                     β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

USAGE:
    # Monitor what hub is broadcasting (metrics from all spaces)
    python3 test_websocket.py --subscribe
    
    # Send a test metric to hub (publish as a space)
    python3 test_websocket.py --publish --space TEST_ASSET
    
    # Send test metrics with a different space name
    python3 test_websocket.py --publish --space V100_1h

EXAMPLE WORKFLOW:

  Terminal 1 (Monitor hub):
    $ python3 test_websocket.py --subscribe
    [βœ“] Connected! Listening for metrics updates...

  Terminal 2 (Send test data):
    $ python3 test_websocket.py --publish --space V100_1h
    [βœ“] Connected! Sending test message...
    [βœ“] Sent:
    {
      "training": {...},
      "voting": {...}
    }

  Terminal 1 (should see the message):
    [12:34:56] Message #1:
    {
      "space_name": "V100_1h",
      "training": {...},
      "voting": {...}
    }

TROUBLESHOOTING:

  "Connection refused" β†’ Hub not running on port 7860
    $ curl http://127.0.0.1:7860/api/health
    
  No messages on subscribe β†’ Asset spaces not connected
    Check if asset spaces are running and sending data
    
  "Module not found: websockets" β†’ Install it
    $ pip install websockets
""")


if __name__ == "__main__":
    asyncio.run(main())