forked from openedx/openedx-platform
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: course optimizer extended report #426
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
Merged
djoseph-apphelix
merged 9 commits into
release-ulmo
from
nsprenkle/course-optimizer-extended-report
Sep 9, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a38dab
feat: add Course Optimizer extended-report proxy views
nsprenkle d9065c7
chore(devstack): point Course Optimizer proxy at local xpert-ai-workf…
nsprenkle 613111b
feat: add enable_course_optimizer_extended_report waffle flag
nsprenkle e474580
refactor: move timeout to settings
nsprenkle c5a1407
fix: return 404 for invalid/nonexistent course in analysis report status
nsprenkle 5bb8cc9
fix: gate course analysis report endpoints behind waffle flag
nsprenkle 787f2ba
refactor: update waffle flag name
nsprenkle dd20076
refactor: move Course Optimizer extended-analysis views to v1 API
nsprenkle 4d4241a
fix: guard against malformed JSON from xpert-ai-workflows and allow d…
nsprenkle 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 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
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
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
159 changes: 159 additions & 0 deletions
159
cms/djangoapps/contentstore/rest_api/v1/views/course_optimizer.py
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,159 @@ | ||
| """API Views for the Course Optimizer extended-analysis report.""" | ||
|
|
||
| import os | ||
|
|
||
| import edx_api_doc_tools as apidocs | ||
| import requests | ||
| from django.conf import settings | ||
| from opaque_keys.edx.keys import CourseKey | ||
| from rest_framework import status | ||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
|
|
||
| from cms.djangoapps.contentstore.tasks import create_export_tarball | ||
| from cms.djangoapps.contentstore.toggles import enable_course_optimizer_extended_checks | ||
| from common.djangoapps.student.auth import has_course_author_access | ||
| from common.djangoapps.util.json_request import JsonResponse | ||
| from openedx.core.lib.api.view_utils import ( | ||
| DeveloperErrorViewMixin, | ||
| verify_course_exists, | ||
| view_auth_classes, | ||
| ) | ||
| from xmodule.modulestore.django import modulestore | ||
|
|
||
|
|
||
| @view_auth_classes(is_authenticated=True) | ||
| class CourseAnalysisReportView(DeveloperErrorViewMixin, APIView): | ||
| """ | ||
| View for kicking off a Course Optimizer extended-analysis run. | ||
| """ | ||
|
|
||
| @apidocs.schema( | ||
| parameters=[ | ||
| apidocs.string_parameter("course_id", apidocs.ParameterLocation.PATH, description="Course ID"), | ||
| ], | ||
| responses={ | ||
| 202: "Analysis run queued.", | ||
| 401: "The requester is not authenticated.", | ||
| 403: "The requester cannot access the specified course.", | ||
| 404: "The requested course does not exist.", | ||
| 502: "The Course Optimizer extended-report backend is unreachable.", | ||
| }, | ||
| ) | ||
| @verify_course_exists() | ||
| def post(self, request: Request, course_id: str): | ||
| """ | ||
| Generate a fresh export of the course and hand it to the Course | ||
| Optimizer extended-report backend (xpert-ai-workflows) to start a | ||
| new analysis run. Studio generates the export server-side -- the | ||
| browser never uploads anything or talks to that backend directly. | ||
|
|
||
| **Example Request** | ||
|
|
||
| POST /api/contentstore/v1/course_optimizer/analysis/{course_id} | ||
|
|
||
| **Response Values** | ||
| ```json | ||
| { | ||
| "run_id": <string> | ||
| } | ||
| ``` | ||
| """ | ||
| course_key = CourseKey.from_string(course_id) | ||
| if not has_course_author_access(request.user, course_key): | ||
| self.permission_denied(request) | ||
|
|
||
| if not enable_course_optimizer_extended_checks(course_key): | ||
| return JsonResponse( | ||
| {"error": "Course optimizer extended checks are not enabled."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| course_block = modulestore().get_course(course_key) | ||
| tarball = create_export_tarball(course_block, course_key, {}) | ||
| try: | ||
|
nsprenkle marked this conversation as resolved.
|
||
| tarball.seek(0) | ||
| try: | ||
| response = requests.post( | ||
| f'{settings.COURSE_ANALYSIS_WORKFLOW_URL}/courses/{course_id}/runs', | ||
| files={'file': (os.path.basename(tarball.name), tarball, 'application/gzip')}, | ||
| headers={'X-Api-Key': settings.COURSE_ANALYSIS_WORKFLOW_API_KEY}, | ||
| timeout=settings.COURSE_ANALYSIS_WORKFLOW_REQUEST_TIMEOUT_SECONDS, | ||
| ) | ||
| except requests.RequestException: | ||
| return Response(status=status.HTTP_502_BAD_GATEWAY) | ||
| finally: | ||
| tarball.close() | ||
|
|
||
| try: | ||
| response_data = response.json() | ||
| except ValueError: | ||
| return Response(status=status.HTTP_502_BAD_GATEWAY) | ||
|
|
||
| return Response(response_data, status=response.status_code) | ||
|
|
||
|
|
||
| @view_auth_classes() | ||
| class CourseAnalysisReportStatusView(DeveloperErrorViewMixin, APIView): | ||
| """ | ||
| View proxying a course's Course Optimizer extended-report status. | ||
|
|
||
| Studio calls the Course Optimizer extended-report backend | ||
| (xpert-ai-workflows) server-side and returns its response as-is; the | ||
| browser never calls that backend directly. | ||
| """ | ||
|
|
||
| @apidocs.schema( | ||
| parameters=[ | ||
| apidocs.string_parameter("course_id", apidocs.ParameterLocation.PATH, description="Course ID"), | ||
| ], | ||
| responses={ | ||
| 200: "OK", | ||
| 401: "The requester is not authenticated.", | ||
| 403: "The requester cannot access the specified course.", | ||
| 404: "The course has no analysis runs yet.", | ||
| 502: "The Course Optimizer extended-report backend is unreachable.", | ||
| }, | ||
| ) | ||
| @verify_course_exists() | ||
| def get(self, request: Request, course_id: str): | ||
| """ | ||
| Proxy the status of a course's most recent Course Optimizer | ||
| extended-analysis run. | ||
|
|
||
| **Example Request** | ||
|
|
||
| GET /api/contentstore/v1/course_optimizer/analysis/{course_id}/status | ||
|
|
||
| **Response Values** | ||
|
|
||
| The xpert-ai-workflows run-status response, passed through | ||
| unchanged: `{run_id, status, report, error}`. A 404 means the | ||
| course has no analysis runs yet. | ||
| """ | ||
| course_key = CourseKey.from_string(course_id) | ||
| if not has_course_author_access(request.user, course_key): | ||
| self.permission_denied(request) | ||
|
|
||
| if not enable_course_optimizer_extended_checks(course_key): | ||
| return JsonResponse( | ||
| {"error": "Course optimizer extended checks are not enabled."}, | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
|
||
| try: | ||
| response = requests.get( | ||
| f'{settings.COURSE_ANALYSIS_WORKFLOW_URL}/courses/{course_id}/runs/latest', | ||
| headers={'X-Api-Key': settings.COURSE_ANALYSIS_WORKFLOW_API_KEY}, | ||
| timeout=settings.COURSE_ANALYSIS_WORKFLOW_REQUEST_TIMEOUT_SECONDS, | ||
| ) | ||
| except requests.RequestException: | ||
| return Response(status=status.HTTP_502_BAD_GATEWAY) | ||
|
|
||
| try: | ||
| response_data = response.json() | ||
| except ValueError: | ||
| return Response(status=status.HTTP_502_BAD_GATEWAY) | ||
|
|
||
| return Response(response_data, status=response.status_code) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.