Closed gannonprudhomme closed 3 years ago
Updated this with information on how to do it - let me know if you have any questions!
If you think it's too much work for one issue, we can always split it up into a backend + frontend task (in case you want to do one side but not the other). Just looking at the text makes it seem like it's a lot of work, but it should actually be a lot more straightforward than it looks.
We should add some an indicator somewhere on the site (maybe on the navbar?) that says when the course information was last updated.
An easy way of doing this would probably be to just have a table with a single row / column that says when
scrape_courses
was last ran. The route would then just retrieve this value and the frontend would display it accordinglyAdd the model to the backend
So I'm not really sure how we want to handle this, but my thought process would be that the easiest way to do it would be to have a model that just stores a term and date, and it's ID / primary key is the term. So it'll basically just show when the data was last updated for each term.
To do this, you'll need to:
1) Create the model
autoscheduler/scraper/models/
since this is related to scraping.LastUpdated
or something like that.term
and adate
, where theterm
is a string that is also it's ID / primary key.python manage.py makemigrations
to create a migration file.autoscheduler/scraper/migrations/
3) Runpython manage.py migrate
to apply the migration to your databaseSave/update these models in
scrape_courses
Now that the model is created, you'll need to actually save them whenever
scrape_courses
is ran.autoscheduler/scraper/management/commands/scrape_courses.py
LastUpdated
model(s) that have the term as the respective term and the current datetime as the date model.term
, or multiple terms interms
, so make sure you handle both situationsscrape_courses -t 202031
, but can also scrape a group of terms, such asscrape_courses -y 2020
to scrape 2020,scrape_courses -r
to do the most recent semester, or even justscrape_courses
without args, which scrapes all terms possible.LastUpdated.objects.delete()
thenLastUpdated.objects.bulk_create(objects)
. Sounds counter-intuitive, but it's what we're doing elsewhere and it seems to work for us (Django'sbulk_update
is pretty slow from what we've seen)save_models
Add a route that returns the respective model on the backend
We'll probably want to set this on a per-term basis? Or maybe a per-semester basis (since all locations are scraped each semester)
You should create a route like
api/get_last_updated
which takes a term query parameter. A potential query would look something likeapi/get_lasted_updated?term=202031
1) Create the API view in
views.py
scraper
route, you'll do this inautoscheduler/scraper/views.py
. Note thatscraper
,scheduler
, anduser_sessions
all have aviews.py
fileRetrieveLastUpdated
view at the bottom of the file. It will take be aget
request, so as such you can base this off of theRetrieveCourseView
at the top of the file.term
2) Add the URL tourls.py
scraper
route, you'll do this inautoscheduler/scraper/urls.py
urlpatterns
list that'll look something likepath('get_last_updated
, RetrieveLastUpdated.as_view())`api/
isn't in here - that's b/c all of the routes inscraper
automatically have theapi/
appended to it. You can see this if you go toautoscheduler/autoscheduler/urls.py
.Add the component on the frontend + make it fetch the data
You'll then want to add a component on the frontend (and make it display in the navbar) that fetches the last updated data and displays it
We'll also probably want to have some mechanism for updating this, since in peak times we hope to scrape the data every 15 minutes. Maybe we can try to update this value every minute? Whatever you think is best really
Add frontend tests
You'll need to create some basic tests on the frontend. Since these are UI tests, you'll want to put them in
autoscheduler/frontend/src/tests/ui/LastUpdated.test.tsx
or something like that. When you get around to this message in Discord and we'll find a good example test.Add backend tests
The tests should be relatively straightforward. There are a lot of examples in
autoscheduler/scraper/tests/api_tests.py
(which is also in the folder where you'll be making these tests)api_tests.py
is already huge, make a separate test file called likeapi_last_updated_tests.py
or something like that.pytest
in theautoscheduler/
folder (where thepytest.ini
is located)