-
Notifications
You must be signed in to change notification settings - Fork 211
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
Pull all addons in one DB request #5289
Conversation
Drafting so that I can update the tests. |
@dhruvkb, how do we enable debug mode? I tried setting DJANGO_LOG_LEVEL to "DEBUG", but can't see the queries. |
@obulat can you try again with openverse/api/conf/settings/logging.py Line 24 in a048c41
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not able to see the DB queries, even after adding DJANGO_DATABASE_LOGGING=true
to the api/.env
file 🤔 is there a different way I should be doing that? It's under ov just logs web
, right?
|
||
@pytest.mark.django_db | ||
@mock.patch("api.models.audio.AudioAddOn.objects.get") | ||
def test_audio_waveform_sent_when_present(get_mock, audio_fixture): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be new tests for the modified functionality of get_db_results
with the include_addons
parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I'll add some tests for that asap.
@AetherUnbound can you try this and see if you can see the DB logs? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the DB queries in logs when these variables are set this way:
DJANGO_LOG_LEVEL=DEBUG
DJANGO_DATABASE_LOGGING=True
And indeed, there are no more N+1 queries when peaks are requested 😄 I support @AetherUnbound comment tho. It would be nice to have some tests on this functionality.
@AetherUnbound and @krysal, do you think the test cases introduced in 51e1c3d are sufficient to cover that the addons are fetched and that at most 1 extra query is introduced for peaks? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added tests fail on main 👍
@@ -147,6 +159,20 @@ def _validate_source(self, source): | |||
detail=f"Invalid source '{source}'. Valid sources are: {valid_string}.", | |||
) | |||
|
|||
def include_addons(self, serializer): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are addons described anywhere in the codebase docs? Do we only have waveforms as addons now, or is there something else, too?
I would like for this comment to include something like "addons, like waveforms for audio". A description of why something is an addon and not in the main model (if it doesn't exist anywhere now) , would be very useful. Not necessarily in this PR, though :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of the addon model is specific to the audio media type and that too only for the waveforms. The AudioAddOn
model does not have any docstrings, so the conversation around the PR that added this model WordPress/openverse-api#510 is our only reference.
The PR describes that the reason the waveforms are in an AudioAddOn
model is because the data in any columns that are not in the upstream DB would be gone after a data refresh.
Ahh, I was probably missing the |
Fixes
Fixes #5274
Description
This PR prevents audio list requests with
?peaks=true
from making N+1 queries by fetching allAudioAddon
models from the DB in a single call.Testing Instructions
With DEBUG logging enabled, make API calls to the
/v1/audio/
endpoint with?peaks=true
. You should see only 2 total calls to the DB, where the second call is for audio addons:Checklist
Update index.md
).main
) or a parent feature branch.ov just catalog/generate-docs
for catalogPRs) or the media properties generator (
ov just catalog/generate-docs media-props
for the catalog or
ov just api/generate-docs
for the API) where applicable.Developer Certificate of Origin
Developer Certificate of Origin