Return active stream connections for a single user. Pass `user_id=None` to return all active connections across the system.
(user_id)
| 83 | return False |
| 84 | |
| 85 | def get_user_active_connections(user_id): |
| 86 | """Return active stream connections for a single user. |
| 87 | |
| 88 | Pass `user_id=None` to return all active connections across the system. |
| 89 | """ |
| 90 | redis_client = RedisClient.get_client() |
| 91 | connections = [] |
| 92 | |
| 93 | try: |
| 94 | # Grab live streams |
| 95 | for key in redis_client.scan_iter(match="live:channel:*:clients:*", count=1000): |
| 96 | parts = key.split(':') |
| 97 | if len(parts) >= 5: |
| 98 | channel_id = parts[2] |
| 99 | client_id = parts[4] |
| 100 | |
| 101 | client_user_id, connected_at = redis_client.hmget(key, 'user_id', 'connected_at') |
| 102 | |
| 103 | logger.debug(f"[stream limits] user_id = {user_id}") |
| 104 | logger.debug(f"[stream limits] channel_id = {channel_id}") |
| 105 | logger.debug(f"[stream limits] client_id = {client_id}") |
| 106 | |
| 107 | if user_id is None or (client_user_id and int(client_user_id) == user_id): |
| 108 | try: |
| 109 | logger.debug(f"[stream limits] Found LIVE connection for user {user_id} on channel {channel_id} with client ID {client_id}") |
| 110 | connected_at = float(connected_at) if connected_at else 0 |
| 111 | connections.append({ |
| 112 | 'media_id': channel_id, |
| 113 | 'client_id': client_id, |
| 114 | 'connected_at': connected_at, |
| 115 | 'type': 'live', |
| 116 | }) |
| 117 | except (ValueError, TypeError): |
| 118 | pass |
| 119 | |
| 120 | # Grab VOD |
| 121 | for key in redis_client.scan_iter(match="vod_persistent_connection:*", count=1000): |
| 122 | parts = key.split(':') |
| 123 | if len(parts) >= 2: |
| 124 | client_id = parts[1] |
| 125 | |
| 126 | client_user_id, connected_at, content_uuid = redis_client.hmget( |
| 127 | key, 'user_id', 'created_at', 'content_uuid' |
| 128 | ) |
| 129 | |
| 130 | logger.debug(f"[stream limits] user_id = {user_id}") |
| 131 | logger.debug(f"[stream limits] client_id = {client_id}") |
| 132 | |
| 133 | if user_id is None or (client_user_id and int(client_user_id) == user_id): |
| 134 | try: |
| 135 | logger.debug(f"[stream limits] Found VOD connection for user {user_id} on content {content_uuid} with client ID {client_id}") |
| 136 | connected_at = float(connected_at) if connected_at else 0 |
| 137 | connections.append({ |
| 138 | 'media_id': content_uuid or client_id, |
| 139 | 'client_id': client_id, |
| 140 | 'connected_at': connected_at, |
| 141 | 'type': 'vod', |
| 142 | }) |
no test coverage detected