-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add user quiz statistics endpoint with per-question breakdown #173
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
Open
Batirro
wants to merge
7
commits into
dev
Choose a base branch
from
feat/163-feature-quiz-stats
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c9cea0
feat: add user quiz statistics endpoint with per-question breakdown
Batirro b83ed0d
refactor: optimize quiz stats queries and expand include parsing test
Batirro 2a4be5b
refactor: extract shared include query parser for stats and metadata
Batirro 3292c00
refactor: Remove unused Answer import
Batirro a2f37eb
refactor: centralize include param parsing and reuse in metadata/stats
Batirro 0bf900e
refactor: move param parser from views to utility
Batirro 64e3124
Merge branch 'dev' into feat/163-feature-quiz-stats
Batirro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,88 @@ | ||
| from django.db.models import Count, Max, Q | ||
|
|
||
| from quizzes.models import AnswerRecord, QuizSession | ||
|
|
||
|
|
||
| def get_quiz_stats(quiz, user, *, include_per_question: bool = False) -> dict: | ||
| """ | ||
| Compute aggregated quiz statistics for a specific user. | ||
|
|
||
| Aggregates answer data across all sessions (active + archived) for the given quiz/user pair. | ||
| Study time is taken from the active session only. | ||
|
|
||
| Args: | ||
| quiz: Quiz instance to compute stats for. | ||
| user: User instance to compute stats for. | ||
| include_per_question: If True, includes per-question breakdown in the result. | ||
|
|
||
| Returns: | ||
| A dict with aggregated stats ready to be passed to QuizStatsSerializer. | ||
| """ | ||
| sessions = QuizSession.objects.filter(quiz=quiz, user=user) | ||
|
|
||
| # Aggregate basic session stats in a single query to avoid extra round-trips. | ||
| session_aggregates = sessions.aggregate( | ||
| sessions_count=Count("id"), | ||
| last_activity_at=Max("updated_at"), | ||
| ) | ||
| sessions_count = session_aggregates["sessions_count"] or 0 | ||
| last_activity_at = session_aggregates["last_activity_at"] | ||
|
|
||
| # Aggregate answer data directly from AnswerRecord to avoid cross-join row duplication. | ||
| answer_aggregates = AnswerRecord.objects.filter(session__in=sessions).aggregate( | ||
| total_answers=Count("id"), | ||
| correct_answers=Count("id", filter=Q(was_correct=True)), | ||
| ) | ||
|
|
||
| total_answers = answer_aggregates["total_answers"] or 0 | ||
| correct_answers = answer_aggregates["correct_answers"] or 0 | ||
| wrong_answers = total_answers - correct_answers | ||
|
|
||
| accuracy = round(correct_answers / total_answers * 100, 2) if total_answers > 0 else 0.0 | ||
|
|
||
| active_study_time = sessions.filter(is_active=True).values_list("study_time", flat=True).first() | ||
| study_time_seconds = int(active_study_time.total_seconds()) if active_study_time else 0 | ||
|
|
||
| result = { | ||
| "quiz_id": quiz.id, | ||
| "total_answers": total_answers, | ||
| "correct_answers": correct_answers, | ||
| "wrong_answers": wrong_answers, | ||
| "accuracy": accuracy, | ||
| "study_time_seconds": study_time_seconds, | ||
| "sessions_count": sessions_count, | ||
| "last_activity_at": last_activity_at, | ||
| } | ||
|
|
||
| if include_per_question: | ||
| result["per_question"] = _get_per_question_stats(sessions) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def _get_per_question_stats(sessions) -> list[dict]: | ||
| """ | ||
| Compute per-question statistics for the given sessions queryset. | ||
|
|
||
| Returns a list of dicts with question_id, attempts, correct_attempts, and last_answered_at. | ||
| """ | ||
| per_question = ( | ||
| AnswerRecord.objects.filter(session__in=sessions) | ||
| .values("question_id") | ||
| .annotate( | ||
| attempts=Count("id"), | ||
| correct_attempts=Count("id", filter=Q(was_correct=True)), | ||
| last_answered_at=Max("answered_at"), | ||
| ) | ||
| .order_by("question_id") | ||
| ) | ||
|
|
||
| return [ | ||
| { | ||
| "question_id": row["question_id"], | ||
| "attempts": row["attempts"], | ||
| "correct_attempts": row["correct_attempts"], | ||
| "last_answered_at": row["last_answered_at"], | ||
| } | ||
| for row in per_question | ||
| ] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
potrzebujemy rozdzielić i zwracać i ogólne i per user, spójrz tutaj co powinno być zwracane
Solvro/pm-testownik#28 (comment)