File size: 13,246 Bytes
5cd2b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""
Centralized WebSocket Service Manager

This module provides a unified interface for managing WebSocket connections
and broadcasting real-time data from various services.
"""

import asyncio
import json
from datetime import datetime
from typing import Dict, List, Set, Any, Optional, Callable
from fastapi import WebSocket, WebSocketDisconnect
from enum import Enum
import logging

logger = logging.getLogger(__name__)


class ServiceType(str, Enum):
    """Available service types for WebSocket subscriptions"""
    # Data Collection Services
    MARKET_DATA = "market_data"
    EXPLORERS = "explorers"
    NEWS = "news"
    SENTIMENT = "sentiment"
    WHALE_TRACKING = "whale_tracking"
    RPC_NODES = "rpc_nodes"
    ONCHAIN = "onchain"

    # Monitoring Services
    HEALTH_CHECKER = "health_checker"
    POOL_MANAGER = "pool_manager"
    SCHEDULER = "scheduler"

    # Integration Services
    HUGGINGFACE = "huggingface"
    PERSISTENCE = "persistence"

    # System Services
    SYSTEM = "system"
    ALL = "all"


class WebSocketConnection:
    """Represents a single WebSocket connection with subscription management"""

    def __init__(self, websocket: WebSocket, client_id: str):
        self.websocket = websocket
        self.client_id = client_id
        self.subscriptions: Set[ServiceType] = set()
        self.connected_at = datetime.utcnow()
        self.last_activity = datetime.utcnow()
        self.metadata: Dict[str, Any] = {}

    async def send_message(self, message: Dict[str, Any]) -> bool:
        """
        Send a message to the client

        Returns:
            bool: True if successful, False if failed
        """
        try:
            await self.websocket.send_json(message)
            self.last_activity = datetime.utcnow()
            return True
        except Exception as e:
            logger.error(f"Error sending message to client {self.client_id}: {e}")
            return False

    def subscribe(self, service: ServiceType):
        """Subscribe to a service"""
        self.subscriptions.add(service)
        logger.info(f"Client {self.client_id} subscribed to {service.value}")

    def unsubscribe(self, service: ServiceType):
        """Unsubscribe from a service"""
        self.subscriptions.discard(service)
        logger.info(f"Client {self.client_id} unsubscribed from {service.value}")

    def is_subscribed(self, service: ServiceType) -> bool:
        """Check if subscribed to a service or 'all'"""
        return service in self.subscriptions or ServiceType.ALL in self.subscriptions


class WebSocketServiceManager:
    """
    Centralized manager for all WebSocket connections and service broadcasts
    """

    def __init__(self):
        self.connections: Dict[str, WebSocketConnection] = {}
        self.service_handlers: Dict[ServiceType, List[Callable]] = {}
        self._lock = asyncio.Lock()
        self._client_counter = 0

    def generate_client_id(self) -> str:
        """Generate a unique client ID"""
        self._client_counter += 1
        return f"client_{self._client_counter}_{int(datetime.utcnow().timestamp())}"

    async def connect(self, websocket: WebSocket) -> WebSocketConnection:
        """
        Accept a new WebSocket connection

        Args:
            websocket: The FastAPI WebSocket instance

        Returns:
            WebSocketConnection: The connection object
        """
        await websocket.accept()
        client_id = self.generate_client_id()

        async with self._lock:
            connection = WebSocketConnection(websocket, client_id)
            self.connections[client_id] = connection

        logger.info(f"New WebSocket connection: {client_id}")

        # Send connection established message
        await connection.send_message({
            "type": "connection_established",
            "client_id": client_id,
            "timestamp": datetime.utcnow().isoformat(),
            "available_services": [s.value for s in ServiceType]
        })

        return connection

    async def disconnect(self, client_id: str):
        """
        Disconnect a client

        Args:
            client_id: The client ID to disconnect
        """
        async with self._lock:
            if client_id in self.connections:
                connection = self.connections[client_id]
                try:
                    await connection.websocket.close()
                except:
                    pass
                del self.connections[client_id]
                logger.info(f"Client disconnected: {client_id}")

    async def broadcast(
        self,
        service: ServiceType,
        message_type: str,
        data: Any,
        filter_func: Optional[Callable[[WebSocketConnection], bool]] = None
    ):
        """
        Broadcast a message to all subscribed clients

        Args:
            service: The service sending the message
            message_type: Type of message
            data: Message payload
            filter_func: Optional function to filter which clients receive the message
        """
        message = {
            "service": service.value,
            "type": message_type,
            "data": data,
            "timestamp": datetime.utcnow().isoformat()
        }

        disconnected_clients = []

        async with self._lock:
            for client_id, connection in self.connections.items():
                # Check subscription and optional filter
                if connection.is_subscribed(service):
                    if filter_func is None or filter_func(connection):
                        success = await connection.send_message(message)
                        if not success:
                            disconnected_clients.append(client_id)

        # Clean up disconnected clients
        for client_id in disconnected_clients:
            await self.disconnect(client_id)

    async def send_to_client(
        self,
        client_id: str,
        service: ServiceType,
        message_type: str,
        data: Any
    ) -> bool:
        """
        Send a message to a specific client

        Args:
            client_id: Target client ID
            service: Service sending the message
            message_type: Type of message
            data: Message payload

        Returns:
            bool: True if successful
        """
        async with self._lock:
            if client_id in self.connections:
                connection = self.connections[client_id]
                message = {
                    "service": service.value,
                    "type": message_type,
                    "data": data,
                    "timestamp": datetime.utcnow().isoformat()
                }
                return await connection.send_message(message)
        return False

    async def handle_client_message(
        self,
        connection: WebSocketConnection,
        message: Dict[str, Any]
    ):
        """
        Handle incoming messages from clients

        Expected message format:
        {
            "action": "subscribe" | "unsubscribe" | "get_status" | "ping",
            "service": "service_name" (for subscribe/unsubscribe),
            "data": {} (optional additional data)
        }
        """
        action = message.get("action")

        if action == "subscribe":
            service_name = message.get("service")
            if service_name:
                try:
                    service = ServiceType(service_name)
                    connection.subscribe(service)
                    await connection.send_message({
                        "service": "system",
                        "type": "subscription_confirmed",
                        "data": {
                            "service": service_name,
                            "subscriptions": [s.value for s in connection.subscriptions]
                        },
                        "timestamp": datetime.utcnow().isoformat()
                    })
                except ValueError:
                    await connection.send_message({
                        "service": "system",
                        "type": "error",
                        "data": {
                            "message": f"Invalid service: {service_name}",
                            "available_services": [s.value for s in ServiceType]
                        },
                        "timestamp": datetime.utcnow().isoformat()
                    })

        elif action == "unsubscribe":
            service_name = message.get("service")
            if service_name:
                try:
                    service = ServiceType(service_name)
                    connection.unsubscribe(service)
                    await connection.send_message({
                        "service": "system",
                        "type": "unsubscription_confirmed",
                        "data": {
                            "service": service_name,
                            "subscriptions": [s.value for s in connection.subscriptions]
                        },
                        "timestamp": datetime.utcnow().isoformat()
                    })
                except ValueError:
                    await connection.send_message({
                        "service": "system",
                        "type": "error",
                        "data": {"message": f"Invalid service: {service_name}"},
                        "timestamp": datetime.utcnow().isoformat()
                    })

        elif action == "get_status":
            await connection.send_message({
                "service": "system",
                "type": "status",
                "data": {
                    "client_id": connection.client_id,
                    "connected_at": connection.connected_at.isoformat(),
                    "last_activity": connection.last_activity.isoformat(),
                    "subscriptions": [s.value for s in connection.subscriptions],
                    "total_clients": len(self.connections)
                },
                "timestamp": datetime.utcnow().isoformat()
            })

        elif action == "ping":
            await connection.send_message({
                "service": "system",
                "type": "pong",
                "data": message.get("data", {}),
                "timestamp": datetime.utcnow().isoformat()
            })

        else:
            await connection.send_message({
                "service": "system",
                "type": "error",
                "data": {
                    "message": f"Unknown action: {action}",
                    "supported_actions": ["subscribe", "unsubscribe", "get_status", "ping"]
                },
                "timestamp": datetime.utcnow().isoformat()
            })

    async def start_service_stream(
        self,
        service: ServiceType,
        data_generator: Callable,
        interval: float = 1.0
    ):
        """
        Start a continuous data stream for a service

        Args:
            service: The service type
            data_generator: Async function that generates data
            interval: Update interval in seconds
        """
        logger.info(f"Starting stream for service: {service.value}")

        while True:
            try:
                # Check if anyone is subscribed
                has_subscribers = False
                async with self._lock:
                    for connection in self.connections.values():
                        if connection.is_subscribed(service):
                            has_subscribers = True
                            break

                # Only fetch data if there are subscribers
                if has_subscribers:
                    data = await data_generator()
                    if data:
                        await self.broadcast(
                            service=service,
                            message_type="update",
                            data=data
                        )

                await asyncio.sleep(interval)

            except asyncio.CancelledError:
                logger.info(f"Stream cancelled for service: {service.value}")
                break
            except Exception as e:
                logger.error(f"Error in service stream {service.value}: {e}")
                await asyncio.sleep(interval)

    def get_stats(self) -> Dict[str, Any]:
        """Get manager statistics"""
        subscription_counts = {}
        for service in ServiceType:
            subscription_counts[service.value] = sum(
                1 for conn in self.connections.values()
                if conn.is_subscribed(service)
            )

        return {
            "total_connections": len(self.connections),
            "clients": [
                {
                    "client_id": conn.client_id,
                    "connected_at": conn.connected_at.isoformat(),
                    "last_activity": conn.last_activity.isoformat(),
                    "subscriptions": [s.value for s in conn.subscriptions]
                }
                for conn in self.connections.values()
            ],
            "subscription_counts": subscription_counts
        }


# Global instance
ws_manager = WebSocketServiceManager()