forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #455 from edly-io/alisalman/sdaia-leaderboard
feat: leaderboard apis and design
- Loading branch information
Showing
15 changed files
with
527 additions
and
6 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
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.