Skip to content
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

fix: optimize n+1 query when creating upload #377

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions upload/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def get_url(self, obj: ReportSession):
commit = obj.report.commit
return f"{settings.CODECOV_DASHBOARD_URL}/{repository.author.service}/{repository.author.username}/{repository.name}/commit/{commit.commitid}"

def _create_existing_flags_map(self, repoid: int) -> dict:
existing_flags = RepositoryFlag.objects.filter(repository_id=repoid).all()
existing_flags_map = {}
for flag_obj in existing_flags:
existing_flags_map[flag_obj.flag_name] = flag_obj
return existing_flags_map

def create(self, validated_data):
flag_names = (
validated_data.pop("flags") if "flags" in validated_data.keys() else []
Expand All @@ -67,17 +74,17 @@ def create(self, validated_data):
)
upload = ReportSession.objects.create(**validated_data)
flags = []

if upload:
repoid = upload.report.commit.repository.repoid
existing_flags_map = self._create_existing_flags_map(repoid)
for individual_flag in flag_names:
existing_flag = RepositoryFlag.objects.filter(
repository_id=repoid, flag_name=individual_flag
).first()
if not existing_flag:
existing_flag = RepositoryFlag.objects.create(
flag_obj = existing_flags_map.get(individual_flag, None)
if flag_obj is None:
flag_obj = RepositoryFlag.objects.create(
repository_id=repoid, flag_name=individual_flag
)
flags.append(existing_flag)
flags.append(flag_obj)
upload.flags.set(flags)
return upload

Expand Down
19 changes: 19 additions & 0 deletions upload/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ def test_upload_serializer_null_build_url_empty_flags(transactional_db, mocker):
assert serializer.is_valid()


def test__create_existing_flags_map(transactional_db, mocker):
mocker.patch(
"services.archive.StorageService.create_presigned_put",
return_value="presigned put",
)
upload = get_fake_upload_with_flags()
serializer = UploadSerializer(instance=upload)
flags_map = serializer._create_existing_flags_map(
upload.report.commit.repository.repoid
)
upload_flags = upload.flags.all()
flag1 = list(filter(lambda flag: flag.flag_name == "flag1", upload_flags))[0]
flag2 = list(filter(lambda flag: flag.flag_name == "flag2", upload_flags))[0]
assert flags_map == {
"flag1": flag1,
"flag2": flag2,
}


def test_commit_serializer_contains_expected_fields(transactional_db, mocker):
commit = CommitFactory.create()
serializer = CommitSerializer(commit)
Expand Down
Loading