-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an endpoint to view basic stats (#865)
- Loading branch information
1 parent
2162c4f
commit 456de60
Showing
14 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import json | ||
from typing import Callable, Awaitable | ||
from dataclasses import asdict | ||
|
||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
|
||
from src.usage_stats import UsageStats | ||
|
||
|
||
async def stats_endpoint( | ||
get_usage_info: Callable[[], Awaitable[UsageStats]], request: Request | ||
) -> Response: | ||
return Response(json.dumps(asdict(await get_usage_info()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
from typing import List, Any | ||
from typing import List, Any, Callable, Awaitable | ||
|
||
from starlette.routing import WebSocketRoute | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
from starlette.routing import WebSocketRoute, Route | ||
|
||
from src.api.wsmanager import WebsocketManager | ||
from src.ws.starlette_ws_client import StarletteWebsocketClient | ||
|
||
|
||
def routes(ws: WebsocketManager) -> List[Any]: | ||
def routes( | ||
stats_endpoint: Callable[[Request], Awaitable[Response]], ws: WebsocketManager | ||
) -> List[Any]: | ||
return [ | ||
Route('/stats', stats_endpoint), | ||
WebSocketRoute( | ||
'/{room_id}', | ||
lambda websocket: ws.connection_handler( | ||
StarletteWebsocketClient(websocket) | ||
), | ||
) | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from src.rate_limit.rate_limit import RateLimiter | ||
from src.room_store.room_store import RoomStore | ||
|
||
|
||
@dataclass | ||
class UsageStats: | ||
seconds_since_last_activity: Optional[int] | ||
num_connections: int | ||
|
||
|
||
async def get_usage_stats( | ||
room_store: RoomStore, rate_limiter: RateLimiter | ||
) -> UsageStats: | ||
return UsageStats( | ||
await room_store.seconds_since_last_activity(), | ||
await rate_limiter.get_total_num_connections(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters