Skip to content

Commit

Permalink
Add registered user count & total pp earned APIs for new FE homepage (#6
Browse files Browse the repository at this point in the history
)

* Add registered user count & total pp earned APIs

* remove unused code

* consistent style
  • Loading branch information
cmyui authored Aug 11, 2024
1 parent 7567ff1 commit b73bce8
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/api/public/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from fastapi import APIRouter

from . import authentication
from . import overall_stats
from . import users
from . import user_stats

public_router = APIRouter()

public_router.include_router(authentication.router)
public_router.include_router(overall_stats.router)
public_router.include_router(users.router)
public_router.include_router(user_stats.router)
24 changes: 24 additions & 0 deletions app/api/public/overall_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from fastapi import APIRouter, Response
from fastapi.responses import JSONResponse
from app.usecases import user_stats, users


router = APIRouter(tags=["(Public) Overall Stats API"])


@router.get("/public/api/v1/overall-stats/total-registered-users")
async def fetch_total_registered_user_count() -> Response:
response = await users.fetch_total_registered_user_count()
return JSONResponse(
content=response,
status_code=200,
)


@router.get("/public/api/v1/overall-stats/global-all-time-pp-earned")
async def get_global_all_time_pp_earned() -> Response:
response = await user_stats.fetch_global_all_time_pp_earned()
return JSONResponse(
content=response,
status_code=200,
)
1 change: 1 addition & 0 deletions app/models/user_stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pydantic import BaseModel


class UserStats(BaseModel):
ranked_score: int
total_score: int
Expand Down
17 changes: 17 additions & 0 deletions app/repositories/user_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,20 @@ async def fetch_one_by_user_id_and_akatsuki_mode(
d_count=user_stats["d_count"],
max_combo=user_stats["max_combo"],
)


async def fetch_global_all_time_pp_earned() -> int:
# NOTE: this query is not representative of the actual
# "total pp earned over all time" because it only regards
# the pp earned based on their all-time best scores, rather
# than the sum of all pp earned from all scores over time.
# It would be more difficult to calculat the latter, but
# it would be more accurate.
query = """
SELECT SUM(pp) as total_pp
FROM user_stats
"""
val = await app.state.database.fetch_val(query)
if val is None:
return 0
return val
11 changes: 11 additions & 0 deletions app/repositories/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,14 @@ async def update_email_address(user_id: int, new_email_address: str) -> None:
}

await app.state.database.execute(query, params)


async def fetch_total_registered_user_count() -> int:
query = """\
SELECT COUNT(*)
FROM users
"""
val = await app.state.database.fetch_val(query)
if val is None:
return 0
return val
4 changes: 4 additions & 0 deletions app/usecases/user_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ async def fetch_one_by_user_id_and_akatsuki_mode(
d_count=stats.d_count,
max_combo=stats.max_combo,
)


async def fetch_global_all_time_pp_earned() -> int:
return await user_stats.fetch_global_all_time_pp_earned()
4 changes: 4 additions & 0 deletions app/usecases/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,7 @@ async def update_email_address(

await users.update_email_address(user_id, new_email_address)
return None


async def fetch_total_registered_user_count() -> int:
return await users.fetch_total_registered_user_count()

0 comments on commit b73bce8

Please sign in to comment.