Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics endpoints for schoolsyst itself #24

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
13 changes: 13 additions & 0 deletions schoolsyst_api/global_stats/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pydantic import conint
from schoolsyst_api.models import BaseModel


class HomeworkCompletedStats(BaseModel):
"""
Global statistics for completed homework accross all users of schoolsyst
"""

week: conint(ge=0)
month: conint(ge=0)
year: conint(ge=0)
all_time: conint(ge=0)
53 changes: 53 additions & 0 deletions schoolsyst_api/global_stats/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from datetime import date

from arango.database import StandardDatabase
from fastapi import Depends
from fastapi_utils.inferring_router import InferringRouter
from pydantic import PositiveInt
from schoolsyst_api import database
from schoolsyst_api.global_stats.models import HomeworkCompletedStats
from schoolsyst_api.homework.models import Homework

router = InferringRouter()


@router.get("/$/registered_users", summary="Get the number of registered users")
def get_global_stats_registered_users(
db: StandardDatabase = Depends(database.get),
) -> PositiveInt:
return db.collection("users").all().count()


@router.get("/$/confirmed_users", summary="Get the number of confirmed users")
def get_global_stats_confirmed_users(
db: StandardDatabase = Depends(database.get),
) -> PositiveInt:
"""
The number of registered users which have confirmed their email address.
"""
return db.collection("users").find({"email_is_confirmed": True}).count()


@router.get("/$/homework_completed")
def get_global_stats_homework_completed(
db: StandardDatabase = Depends(database.get),
) -> HomeworkCompletedStats:
all_time = 0
year = 0
week = 0
month = 0

for doc in db.collection("homework").all():
homework = Homework(**doc)
all_time += 1
if homework.updated_at.isocalendar()[1] == date.today().isocalendar()[1]:
week += 1
month += 1
year += 1
elif homework.updated_at.month == date.today().month:
month += 1
year += 1
elif homework.updated_at.year == date.today().year:
year += 1

return HomeworkCompletedStats(all_time=all_time, year=year, week=week, month=month)
4 changes: 4 additions & 0 deletions schoolsyst_api/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

import schoolsyst_api.global_stats.routes
import schoolsyst_api.homework.routes
import schoolsyst_api.schedule.routes
import schoolsyst_api.settings.routes
Expand Down Expand Up @@ -33,6 +34,9 @@
api.include_router(schoolsyst_api.homework.routes.router, tags=["Homework"])
api.include_router(schoolsyst_api.settings.routes.router, tags=["Settings"])
api.include_router(schoolsyst_api.schedule.routes.router, tags=["Schedule"])
api.include_router(
schoolsyst_api.global_stats.routes.router, tags=["schoolsyst Statistics"]
)
# Modify the OpenAPI spec
edit_openapi_spec(api)

Expand Down