Skip to content

Commit

Permalink
Remove default ordering on XForm, Attachment model (#2645)
Browse files Browse the repository at this point in the history
* remove default ordering on XForm

ordering creates a performance bottleneck and should be avoided if unnecessary

* order queryset in XFormViewSet

* restore original file

* refactor code

* update max_runs flaky test

* update max_runs flaky test

* remove default ordering on Attachment model
  • Loading branch information
kelvin-muchiri authored Jul 16, 2024
1 parent 1d3c637 commit b130a3d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3766,7 +3766,7 @@ def setUp(self):
self.logger = logging.getLogger("console_logger")

# pylint: disable=invalid-name,too-many-locals
@flaky(max_runs=10)
@flaky(max_runs=15)
def test_data_retrieve_instance_osm_format(self):
"""Test /data endpoint OSM format."""
filenames = [
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/api/viewsets/attachment_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def list(self, request, *args, **kwargs):

# pylint: disable=attribute-defined-outside-init
self.object_list = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(self.object_list)
page = self.paginate_queryset(self.object_list.order_by("pk"))
if page is not None:
serializer = self.get_serializer(page, many=True)

Expand Down
18 changes: 11 additions & 7 deletions onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def retrieve(self, request, *args, **kwargs):
# pylint: disable=attribute-defined-outside-init
self.object_list = self._get_public_forms_queryset()

page = self.paginate_queryset(self.object_list)
page = self.paginate_queryset(self.object_list.order_by("pk"))
if page is not None:
serializer = self.get_serializer(page, many=True)
else:
Expand Down Expand Up @@ -725,9 +725,11 @@ def data_import(self, request, *args, **kwargs):

return Response(
data=resp,
status=status.HTTP_200_OK
if resp.get("error") is None
else status.HTTP_400_BAD_REQUEST,
status=(
status.HTTP_200_OK
if resp.get("error") is None
else status.HTTP_400_BAD_REQUEST
),
)

@action(methods=["POST", "GET"], detail=True)
Expand Down Expand Up @@ -795,9 +797,11 @@ def csv_import(self, request, *args, **kwargs):

return Response(
data=resp,
status=status.HTTP_200_OK
if resp.get("error") is None
else status.HTTP_400_BAD_REQUEST,
status=(
status.HTTP_200_OK
if resp.get("error") is None
else status.HTTP_400_BAD_REQUEST
),
)

def partial_update(self, request, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.13 on 2024-07-16 10:46

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('logger', '0020_rename_logger_inst_deleted_at_da31a3_idx_logger_inst_deleted_da31a3_idx_and_more'),
]

operations = [
migrations.AlterModelOptions(
name='attachment',
options={},
),
migrations.AlterModelOptions(
name='xform',
options={'permissions': (('view_xform_all', 'Can view all associated data'), ('view_xform_data', 'Can view submitted data'), ('report_xform', 'Can make submissions to the form'), ('move_xform', 'Can move form between projects'), ('transfer_xform', 'Can transfer form ownership.'), ('can_export_xform_data', 'Can export form data'), ('delete_submission', 'Can delete submissions from form')), 'verbose_name': 'XForm', 'verbose_name_plural': 'XForms'},
),
]
1 change: 0 additions & 1 deletion onadata/apps/logger/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class Attachment(models.Model):

class Meta:
app_label = "logger"
ordering = ("pk",)

def save(self, *args, **kwargs):
if self.media_file and self.mimetype == "":
Expand Down
7 changes: 3 additions & 4 deletions onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,9 @@ def get_mongo_field_names_dict(self):
"""
names = {}
for elem in self.get_survey_elements():
names[
_encode_for_mongo(str(elem.get_abbreviated_xpath()))
] = elem.get_abbreviated_xpath()
names[_encode_for_mongo(str(elem.get_abbreviated_xpath()))] = (
elem.get_abbreviated_xpath()
)
return names

survey_elements = property(get_survey_elements)
Expand Down Expand Up @@ -903,7 +903,6 @@ class Meta:
)
verbose_name = gettext_lazy("XForm")
verbose_name_plural = gettext_lazy("XForms")
ordering = ("pk",)
permissions = (
("view_xform_all", _("Can view all associated data")),
("view_xform_data", _("Can view submitted data")),
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/logger/tests/test_briefcase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _download_submissions(self):
mocker.head(requests_mock.ANY, content=submission_list)
self.briefcase_client.download_instances(self.xform.id_string)

@flaky(max_runs=11)
@flaky(max_runs=15)
def test_download_xform_xml(self):
"""
Download xform via briefcase api
Expand Down

0 comments on commit b130a3d

Please sign in to comment.