From 8fef830740bd547dc57b6a87f7d48d02bc39b7f1 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Mon, 1 Apr 2013 14:02:54 -0400 Subject: [PATCH] optimize db round trips, by getting the course descriptor outside of the user loop --- .../management/commands/ungenerated_certs.py | 5 ++++- lms/djangoapps/certificates/queue.py | 9 ++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lms/djangoapps/certificates/management/commands/ungenerated_certs.py b/lms/djangoapps/certificates/management/commands/ungenerated_certs.py index 071b2c261b..ab1459766a 100644 --- a/lms/djangoapps/certificates/management/commands/ungenerated_certs.py +++ b/lms/djangoapps/certificates/management/commands/ungenerated_certs.py @@ -73,6 +73,9 @@ class Command(BaseCommand): ended_courses.append(course_id) for course_id in ended_courses: + # prefetch all chapters/sequentials by saying depth=2 + course = modulestore().get_instance(course_id, CourseDescriptor.id_to_location(course_id), depth=2) + print "Fetching enrolled students for {0}".format(course_id) enrolled_students = User.objects.filter( courseenrollment__course_id=course_id).prefetch_related( @@ -99,6 +102,6 @@ class Command(BaseCommand): student, course_id)['status'] in valid_statuses: if not options['noop']: # Add the certificate request to the queue - ret = xq.add_cert(student, course_id) + ret = xq.add_cert(student, course_id, course=course) if ret == 'generating': print '{0} - {1}'.format(student, ret) diff --git a/lms/djangoapps/certificates/queue.py b/lms/djangoapps/certificates/queue.py index a7eb4e3c81..b4632ce9ab 100644 --- a/lms/djangoapps/certificates/queue.py +++ b/lms/djangoapps/certificates/queue.py @@ -115,7 +115,7 @@ class XQueueCertInterface(object): raise NotImplementedError - def add_cert(self, student, course_id): + def add_cert(self, student, course_id, course=None): """ Arguments: @@ -151,9 +151,12 @@ class XQueueCertInterface(object): if cert_status in VALID_STATUSES: # grade the student - course = courses.get_course_by_id(course_id) - profile = UserProfile.objects.get(user=student) + # re-use the course passed in optionally so we don't have to re-fetch everything + # for every student + if course is None: + course = courses.get_course_by_id(course_id) + profile = UserProfile.objects.get(user=student) cert, created = GeneratedCertificate.objects.get_or_create( user=student, course_id=course_id)