-
Notifications
You must be signed in to change notification settings - Fork 11
Lous list open seats async #990
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
Merged
Changes from 33 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
ee9f971
section enrollment model
brandonistfan 95460b9
basic async updating enrollment
brandonistfan f8459bf
enrollment func and filtering w/o reloading
brandonistfan 3a64eb6
refactoring
brandonistfan 6a5db3b
refactoring
brandonistfan a9bac3c
lint fixes
brandonistfan ace06b5
no testing
brandonistfan 4dc9be7
adding test cases!
brandonistfan 50c91e2
trigger ci
brandonistfan 605da6d
Merge branch 'dev' into lous-list-open-seats-async
brandonistfan c8a211f
lint fixes
brandonistfan b219e2f
adding the refresh enrollment func
brandonistfan ba8cc92
linter issues
brandonistfan 05217a2
lint fix
brandonistfan eefbe68
reducing update check count
brandonistfan 07bae05
enrollment progress bars
Jay-Lalwani 0eb7973
design cleanup
Jay-Lalwani d7da76c
top align progress bars
Jay-Lalwani d83a048
show first 5 sections; click section to see sis course
Jay-Lalwani 2577a8b
added padding to top
Jay-Lalwani 1d54d07
python command for enrollment
brandonistfan 91da4e6
first pylint fix
brandonistfan 75ce37e
switching to already defined setup function for testing
brandonistfan efd7560
course enrollment model
brandonistfan d8b3041
two hour updating limit
brandonistfan e9ea738
fixing merge conflicts
brandonistfan 84963d2
minor enrollment update fix
brandonistfan 2c75acc
adding open sections back to searchbar
brandonistfan 2969cf7
lint fixes
brandonistfan 4cb8468
Merge branch 'dev' into lous-list-open-seats-async
brandonistfan 1aa2274
defaulting open sections val to false
brandonistfan dbe1a8d
context
brandonistfan 3e74714
condensing migrations
brandonistfan f051ef3
Removed None from section_times and section_nums
ajnye a8c9a1e
quick fix to fetch data
Jay-Lalwani 934b9bb
merge dev
Jay-Lalwani 83129a4
Merge branch 'dev' into lous-list-open-seats-async
brandonistfan 511bacb
comments
brandonistfan c9ff2b4
Removed unnecessary comment space
ajnye a78913d
Merge branch 'dev' into lous-list-open-seats-async
ajnye 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| Django~=4.2.8 | ||
| asgiref~=3.6.0 | ||
| backoff~=2.2.1 | ||
| black~=24.1.1 | ||
| coverage~=7.3.3 | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """ | ||
| Module for fetching and updating section enrollment data asynchronously. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import time | ||
| import requests | ||
| from asgiref.sync import sync_to_async | ||
| from django.utils import timezone | ||
| from django.http import HttpResponseNotFound | ||
| from tcf_website.models import Section, SectionEnrollment, Semester, Course | ||
| from tcf_website.utils.enrollment import build_sis_api_url, format_enrollment_update_message | ||
|
|
||
| TIMEOUT = 10 | ||
| MAX_WORKERS = 5 | ||
|
|
||
|
|
||
| def fetch_section_data(section): | ||
| """Fetch enrollment data for a given section from the UVA SIS API.""" | ||
| url = build_sis_api_url(section) | ||
|
|
||
| try: | ||
| response = requests.get(url, timeout=TIMEOUT) | ||
| response.raise_for_status() | ||
| data = response.json() | ||
|
|
||
| if data and "classes" in data and data["classes"]: | ||
| class_data = data["classes"][0] | ||
| return { | ||
| "enrollment_taken": class_data.get("enrollment_total", 0), | ||
| "enrollment_limit": class_data.get("class_capacity", 0), | ||
| "waitlist_taken": class_data.get("wait_tot", 0), | ||
| "waitlist_limit": class_data.get("wait_cap", 0), | ||
| } | ||
| except requests.exceptions.RequestException as e: | ||
| print(f"Network error while fetching section {section.sis_section_number}: {e}") | ||
| except ValueError as e: | ||
| print(f"JSON decoding error for section {section.sis_section_number}: {e}") | ||
|
|
||
| return {} | ||
|
|
||
|
|
||
| async def update_enrollment_data(course_id): | ||
| """Asynchronous function to update enrollment data.""" | ||
| start_time = time.monotonic() | ||
|
|
||
| course_exists = await sync_to_async(Course.objects.filter(id=course_id).exists)() | ||
| if not course_exists: | ||
| return HttpResponseNotFound("Course not found.") | ||
|
|
||
| course = await sync_to_async(Course.objects.get)(id=course_id) | ||
| latest_semester = await sync_to_async(lambda: Semester.objects.order_by("-year").first())() | ||
|
|
||
| sections_queryset = Section.objects.filter(course=course, semester=latest_semester) | ||
| sections = await sync_to_async(list)(sections_queryset) | ||
|
|
||
| if not sections: | ||
| print(f"No sections found for course {course.code()} in semester {latest_semester}.") | ||
| return | ||
|
|
||
| print(f"Starting async enrollment update for {len(sections)} sections...") | ||
|
|
||
| changed_sections = 0 | ||
|
|
||
| async def process_section(section): | ||
| """Fetch and update enrollment data asynchronously.""" | ||
| nonlocal changed_sections | ||
| loop = asyncio.get_running_loop() | ||
| data = await loop.run_in_executor(None, fetch_section_data, section) | ||
|
|
||
| if data: | ||
| was_changed = await sync_to_async(update_section_enrollment)(section, data) | ||
| if was_changed: | ||
| changed_sections += 1 | ||
| print(f"Updated enrollment for section {section.sis_section_number}") | ||
|
|
||
| await asyncio.gather(*(process_section(section) for section in sections)) | ||
|
|
||
| elapsed_time = time.monotonic() - start_time | ||
| print( | ||
| f"Enrollment update completed at {timezone.now()} " | ||
| f"(Total time: {elapsed_time:.2f} seconds, " | ||
| f"{changed_sections} sections changed)" | ||
| ) | ||
|
|
||
|
|
||
| def update_section_enrollment(section, data): | ||
| """Update SectionEnrollment only if the data has changed.""" | ||
| section_enrollment, _ = SectionEnrollment.objects.get_or_create(section=section) | ||
|
|
||
| has_changes = any( | ||
| [ | ||
| section_enrollment.enrollment_taken != data.get("enrollment_taken", 0), | ||
| section_enrollment.enrollment_limit != data.get("enrollment_limit", 0), | ||
| section_enrollment.waitlist_taken != data.get("waitlist_taken", 0), | ||
| section_enrollment.waitlist_limit != data.get("waitlist_limit", 0), | ||
| ] | ||
| ) | ||
|
|
||
| if has_changes: | ||
| section_enrollment.enrollment_taken = data.get("enrollment_taken", 0) | ||
| section_enrollment.enrollment_limit = data.get("enrollment_limit", 0) | ||
| section_enrollment.waitlist_taken = data.get("waitlist_taken", 0) | ||
| section_enrollment.waitlist_limit = data.get("waitlist_limit", 0) | ||
| section_enrollment.save() | ||
| print(format_enrollment_update_message(section, section_enrollment)) | ||
| else: | ||
| print(f"No changes in enrollment data for section {section.sis_section_number}") | ||
|
|
||
| return has_changes | ||
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
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.