feat: Add management command for updating discussion user stats for all users in a course (#29973)

Adds a management command to trigger an update to generate user stats for all users in a course.
This commit is contained in:
Hamza Khchine
2022-04-13 14:09:50 +00:00
committed by GitHub
parent 60918fb160
commit ea8f667bee
2 changed files with 53 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
"""
Management command to update user stats for all users in a course.
"""
import logging
from django.core.management.base import BaseCommand
from opaque_keys.edx.keys import CourseKey
import openedx.core.djangoapps.django_comment_common.comment_client.course as cc
log = logging.getLogger(__name__)
class Command(BaseCommand):
"""
Invoke with:
python manage.py lms update_user_discussion_stats <course_id>
"""
help = 'Update the user stats for all users for a particular course.'
def add_arguments(self, parser):
parser.add_argument('course_id', help="ID of the Course to update user stats for")
def handle(self, *args, **options):
course_id = options['course_id']
course_key = CourseKey.from_string(course_id)
data = cc.update_course_users_stats(course_key)
log.info(f"Updated user stats for {data['user_count']} users in {course_key}")

View File

@@ -87,3 +87,26 @@ def get_course_user_stats(course_key: CourseKey, params: Optional[Dict] = None)
"function:get_course_user_stats",
],
)
@function_trace("update_course_users_stats")
def update_course_users_stats(course_key: CourseKey) -> Dict:
"""
Update the user stats for all users for a particular course.
Args:
course_key (str|CourseKey): course key for which stats are needed.
Returns:
dict: data returned by API. Contains count of users updated.
"""
url = f"{settings.PREFIX}/users/{course_key}/update_stats"
return perform_request(
'post',
url,
metric_action='user.update_course_stats',
metric_tags=[
f"course_key:{course_key}",
"function:update_course_users_stats",
],
)