-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #541 from djeck1432/add_leaderboard
Add leaderboard
- Loading branch information
Showing
4 changed files
with
81 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
This module handles leaderboard-related API endpoints. | ||
""" | ||
from fastapi import APIRouter | ||
from web_app.db.crud.leaderboard import LeaderboardDBConnector | ||
from web_app.api.serializers.leaderboard import UserLeaderboardItem, TokenPositionStatistic | ||
|
||
router = APIRouter() | ||
leaderboard_db_connector = LeaderboardDBConnector() | ||
|
||
@router.get( | ||
"/api/get-user-leaderboard", | ||
tags=["Leaderboard"], | ||
response_model=list[UserLeaderboardItem], | ||
summary="Get user leaderboard", | ||
response_description="Returns the top 10 users ordered by closed/opened positions.", | ||
) | ||
async def get_user_leaderboard() -> list[UserLeaderboardItem]: | ||
""" | ||
Get the top 10 users ordered by closed/opened positions. | ||
""" | ||
leaderboard_data = leaderboard_db_connector.get_top_users_by_positions() | ||
return leaderboard_data | ||
|
||
|
||
@router.get( | ||
"/api/get-position-tokens-statistic", | ||
tags=["Leaderboard"], | ||
response_model=list[TokenPositionStatistic], | ||
summary="Get statistics of positions by token", | ||
response_description="Returns statistics of opened/closed positions by token", | ||
) | ||
async def get_position_tokens_statistic() -> list[TokenPositionStatistic]: | ||
""" | ||
This endpoint retrieves statistics about positions grouped by token symbol. | ||
Returns counts of opened and closed positions for each token. | ||
""" | ||
return leaderboard_db_connector.get_position_token_statistics() |
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,46 +1,19 @@ | ||
""" | ||
This module defines the API endpoints and serializers for the leaderboard functionality. | ||
Serializers for leaderboard data. | ||
""" | ||
from fastapi import APIRouter, Depends | ||
from pydantic import BaseModel | ||
from typing import List | ||
from sqlalchemy.orm import Session | ||
from web_app.db.crud.leaderboard import LeaderboardCRUD | ||
from web_app.db.session import get_db | ||
|
||
router = APIRouter() | ||
from pydantic import BaseModel | ||
|
||
class UserLeaderboardItem(BaseModel): | ||
""" | ||
Args: | ||
db (Session): Database session dependency. | ||
Returns: | ||
UserLeaderboardResponse: Response containing the leaderboard data. | ||
Represents statistics for positions of a specific user. | ||
""" | ||
wallet_id: str | ||
positions_number: int | ||
|
||
class UserLeaderboardResponse(BaseModel): | ||
""" | ||
UserLeaderboardResponse is a model representing the response for a user leaderboard. | ||
Attributes: | ||
leaderboard (List[UserLeaderboardItem]): A list of user leaderboard items. | ||
""" | ||
leaderboard: List[UserLeaderboardItem] | ||
|
||
@router.get( | ||
"/api/get-user-leaderboard", | ||
tags=["Leaderboard"], | ||
response_model=UserLeaderboardResponse, | ||
summary="Get user leaderboard", | ||
response_description="Returns the top 10 users ordered by closed/opened positions.", | ||
) | ||
async def get_user_leaderboard(db: Session = Depends(get_db)) -> UserLeaderboardResponse: | ||
class TokenPositionStatistic(BaseModel): | ||
""" | ||
Get the top 10 users ordered by closed/opened positions. | ||
Represents statistics for positions of a specific token. | ||
""" | ||
leaderboard_crud = LeaderboardCRUD(db) | ||
leaderboard_data = leaderboard_crud.get_top_users_by_positions() | ||
return UserLeaderboardResponse(leaderboard=leaderboard_data) | ||
token_symbol: str | ||
total_positions: int |
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