forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 6
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
update: add leaderboard apis #452
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
68 changes: 68 additions & 0 deletions
68
lms/djangoapps/badges/management/commands/update_leaderboard.py
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,68 @@ | ||
import logging | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import Sum, Case, When, Value, IntegerField, Count | ||
from lms.djangoapps.badges.utils import calculate_score | ||
from lms.djangoapps.badges.models import BadgeAssertion, LeaderboardConfiguration, LeaderboardEntry | ||
|
||
logger = logging.getLogger(__name__) # pylint: disable=invalid-name | ||
User = get_user_model() | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Command to populate or update leaderboard entries | ||
Example: | ||
./manage.py lms update_leaderboard | ||
""" | ||
help = 'Populate or update leaderboard entries' | ||
|
||
def get_leaderboard_data(self): | ||
""" | ||
Get leaderboard data from BadgeAssertion model. | ||
|
||
Returns: | ||
QuerySet: A queryset containing aggregated leaderboard data. | ||
""" | ||
leaderboard_data = ( | ||
BadgeAssertion.objects | ||
.values('user__id', 'badge_class__issuing_component') | ||
.annotate( | ||
is_course_badge=Case( | ||
When(badge_class__issuing_component='', then=Value(1)), | ||
default=Value(0), | ||
output_field=IntegerField() | ||
), | ||
is_event_badge=Case( | ||
When(badge_class__issuing_component='openedx__course', then=Value(1)), | ||
default=Value(0), | ||
output_field=IntegerField() | ||
) | ||
).values('user__id') | ||
.annotate(badge_count=Count('id'), course_badge_count=Sum('is_course_badge'), event_badge_count=Sum('is_event_badge')) | ||
) | ||
|
||
return leaderboard_data | ||
|
||
def populate_or_update_leaderboard_entries(self): | ||
""" | ||
Populate or create leaderboard entries based on BadgeAssertion data. | ||
""" | ||
leaderboard_data = self.get_leaderboard_data() | ||
course_badge_score, event_badge_score = LeaderboardConfiguration.get_current_or_default_values() | ||
|
||
for entry in leaderboard_data: | ||
user_id = entry['user__id'] | ||
score = calculate_score(course_badge_score, event_badge_score, entry['course_badge_count'], entry['event_badge_count']) | ||
|
||
LeaderboardEntry.objects.update_or_create( | ||
user_id=user_id, | ||
badge_count=entry['badge_count'], | ||
course_badge_count=entry['course_badge_count'], | ||
event_badge_count=entry['event_badge_count'], | ||
score=score, | ||
) | ||
|
||
def handle(self, *args, **options): | ||
self.populate_or_update_leaderboard_entries() | ||
logger.info('Successfully updated leaderboard entries') |
38 changes: 38 additions & 0 deletions
38
lms/djangoapps/badges/migrations/0005_leaderboardconfiguration_leaderboardentry.py
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 @@ | ||
# Generated by Django 3.2.21 on 2023-11-20 12:45 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('badges', '0004_badgeclass_badgr_server_slug'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='LeaderboardEntry', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('badge_count', models.IntegerField(default=0)), | ||
('event_badge_count', models.IntegerField(default=0)), | ||
('course_badge_count', models.IntegerField(default=0)), | ||
('score', models.IntegerField(default=0)), | ||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='LeaderboardConfiguration', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('change_date', models.DateTimeField(auto_now_add=True, verbose_name='Change date')), | ||
('enabled', models.BooleanField(default=False, verbose_name='Enabled')), | ||
('course_badge_score', models.IntegerField(default=50, help_text='Set the score for a course-completion badge')), | ||
('event_badge_score', models.IntegerField(default=50, help_text='Set the score for the event badge i.e program badge')), | ||
('changed_by', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL, verbose_name='Changed by')), | ||
], | ||
), | ||
] |
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,28 @@ | ||
""" | ||
Defines asynchronous celery task for updateing leaderboard entries | ||
""" | ||
import logging | ||
|
||
from django.db.models import F | ||
from celery import shared_task | ||
from celery_utils.logged_task import LoggedTask | ||
from edx_django_utils.monitoring import set_code_owner_attribute | ||
from lms.djangoapps.badges.models import LeaderboardEntry | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task(base=LoggedTask) | ||
@set_code_owner_attribute | ||
def update_leaderboard_enties(course_badge_score, event_badge_score): | ||
""" | ||
Bulk Update scores for all entries in the LeaderboardEntry | ||
""" | ||
leaderboard_entries = LeaderboardEntry.objects.all() | ||
leaderboard_entries.update( | ||
score=F('course_badge_count') * course_badge_score + F('event_badge_count') * event_badge_score | ||
) | ||
log.info( | ||
f"Updated {leaderboard_entries.count()} enties in the LeaderboardEntry table" | ||
) |
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
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.
I don't think we should update leaderboard entries when configuration is changed. It can be very time consuming task and should be done via management command as you have created one below.