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

optimize events creation and improve indexes #2431

Closed
wants to merge 6 commits into from
Closed
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
6 changes: 3 additions & 3 deletions care/facility/api/viewsets/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@extend_schema(tags=("event_types",))
@method_decorator(cache_page(86400))
@action(detail=True, methods=["GET"])
def descendants(self, request, pk=None):

Check failure on line 34 in care/facility/api/viewsets/events.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ARG002)

care/facility/api/viewsets/events.py:34:27: ARG002 Unused method argument: `request`
event_type: EventType = get_object_or_404(self.queryset, pk=pk)
queryset = self.get_queryset().filter(pk__in=event_type.get_descendants())
serializer = self.get_serializer(queryset, many=True)
Expand All @@ -40,7 +40,7 @@
@extend_schema(tags=("event_types",))
@method_decorator(cache_page(86400))
@action(detail=False, methods=["GET"])
def roots(self, request):

Check failure on line 43 in care/facility/api/viewsets/events.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ARG002)

care/facility/api/viewsets/events.py:43:21: ARG002 Unused method argument: `request`
queryset = self.get_queryset().filter(parent__isnull=True)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
Expand All @@ -56,11 +56,11 @@

class Meta:
model = PatientConsultationEvent
fields = [
"event_type",
"caused_by",
"is_latest",
]

Check failure on line 63 in care/facility/api/viewsets/events.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF012)

care/facility/api/viewsets/events.py:59:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`


class PatientConsultationEventViewSet(ReadOnlyModelViewSet):
Expand All @@ -71,14 +71,14 @@
permission_classes = (IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = PatientConsultationEventFilterSet
# lookup_field = "external_id"

Check failure on line 74 in care/facility/api/viewsets/events.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ERA001)

care/facility/api/viewsets/events.py:74:5: ERA001 Found commented-out code
# lookup_url_kwarg = "external_id"

Check failure on line 75 in care/facility/api/viewsets/events.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (ERA001)

care/facility/api/viewsets/events.py:75:5: ERA001 Found commented-out code

def get_consultation_obj(self):
return get_object_or_404(
get_consultation_queryset(self.request.user).filter(
external_id=self.kwargs["consultation_external_id"]
)
get_consultation_queryset(self.request.user)
.filter(external_id=self.kwargs["consultation_external_id"])
.only("id")
)

def get_queryset(self):
Expand Down
22 changes: 14 additions & 8 deletions care/facility/events/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
fields: set[str] = (
get_changed_fields(old_instance, object_instance)
if old_instance
else {field.name for field in object_instance._meta.fields}

Check failure on line 28 in care/facility/events/handler.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (SLF001)

care/facility/events/handler.py:28:39: SLF001 Private member accessed: `_meta`
)

fields_to_store = fields_to_store & fields if fields_to_store else fields

batch = []
groups_to_mark_stale: list[int] = []
groups = EventType.objects.filter(
model=object_instance.__class__.__name__, fields__len__gt=0, is_active=True
).values_list("id", "fields")
Expand All @@ -44,14 +45,7 @@
if all(not v for v in value.values()):
continue

PatientConsultationEvent.objects.select_for_update().filter(
consultation_id=consultation_id,
event_type=group_id,
is_latest=True,
object_model=object_instance.__class__.__name__,
object_id=object_instance.id,
taken_at__lt=taken_at,
).update(is_latest=False)
groups_to_mark_stale.append(group_id)

Check warning on line 48 in care/facility/events/handler.py

View check run for this annotation

Codecov / codecov/patch

care/facility/events/handler.py#L48

Added line #L48 was not covered by tests
batch.append(
PatientConsultationEvent(
consultation_id=consultation_id,
Expand All @@ -71,6 +65,18 @@
)
)

old_events_filter = {
"consultation_id": consultation_id,
"event_type__in": groups_to_mark_stale,
"is_latest": True,
"object_model": object_instance.__class__.__name__,
"taken_at__lt": taken_at,
}
if change_type == ChangeType.UPDATED:
old_events_filter["object_id"] = object_instance.id
PatientConsultationEvent.objects.select_for_update().filter(
**old_events_filter
).update(is_latest=False)
PatientConsultationEvent.objects.bulk_create(batch)
return len(batch)

Expand All @@ -91,10 +97,10 @@
taken_at = created_date

with transaction.atomic():
if isinstance(objects, (QuerySet, list, tuple)):

Check failure on line 100 in care/facility/events/handler.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (UP038)

care/facility/events/handler.py:100:12: UP038 Use `X | Y` in `isinstance` call instead of `(X, Y)`
if old is not None:
raise ValueError(
"diff is not available when objects is a list or queryset"

Check failure on line 103 in care/facility/events/handler.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM101)

care/facility/events/handler.py:103:21: EM101 Exception must not use a string literal, assign to variable first
)
for obj in objects:
create_consultation_event_entry(
Expand Down
12 changes: 12 additions & 0 deletions care/facility/migrations/0465_merge_20240923_1043.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 5.1.1 on 2024-09-23 05:13

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("facility", "0464_alter_facilitycapacity_room_type_and_more"),
("facility", "0464_rename_spo2_dailyround_archived_spo2"),
]

Check failure on line 10 in care/facility/migrations/0465_merge_20240923_1043.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF012)

care/facility/migrations/0465_merge_20240923_1043.py:7:20: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`

operations = []

Check failure on line 12 in care/facility/migrations/0465_merge_20240923_1043.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF012)

care/facility/migrations/0465_merge_20240923_1043.py:12:18: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 5.1.1 on 2024-09-23 05:13

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0465_merge_20240923_1043"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RemoveIndex(
model_name="patientconsultationevent",
name="facility_pa_consult_7b22fe_idx",
),
migrations.AlterField(
model_name="patientconsultationevent",
name="is_latest",
field=models.BooleanField(db_index=True, default=True),
),
migrations.AddIndex(
model_name="patientconsultationevent",
index=models.Index(
condition=models.Q(("is_latest", True)),
fields=[
"consultation_id",
"is_latest",
"event_type_id",
"object_model",
"taken_at",
],
name="consultation_events_latest_idx",
),
),
]
23 changes: 14 additions & 9 deletions care/facility/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PatientConsultationEvent(models.Model):
)
object_id = models.IntegerField(null=False, blank=False)
event_type = models.ForeignKey(EventType, null=False, on_delete=models.PROTECT)
is_latest = models.BooleanField(default=True)
is_latest = models.BooleanField(default=True, db_index=True)
meta = models.JSONField(default=dict, encoder=CustomJSONEncoder)
value = models.JSONField(default=dict, encoder=CustomJSONEncoder)
change_type = models.CharField(
Expand All @@ -73,11 +73,16 @@ def __str__(self) -> str:

class Meta:
ordering = ["-created_date"]
indexes = [models.Index(fields=["consultation", "is_latest"])]
# constraints = [
# models.UniqueConstraint(
# fields=["consultation", "event_type", "is_latest"],
# condition=models.Q(is_latest=True),
# name="unique_consultation_event_type_is_latest",
# )
# ]
indexes = [
models.Index(
fields=[
"consultation_id",
"is_latest",
"event_type_id",
"object_model",
"taken_at",
],
condition=models.Q(is_latest=True),
name="consultation_events_latest_idx",
),
]
Loading