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

events module: serialize only specific fields of related objects #2324

Merged
merged 5 commits into from
Aug 8, 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
1 change: 1 addition & 0 deletions care/facility/api/serializers/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def create(self, validated_data):
daily_round_obj,
daily_round_obj.created_by_id,
daily_round_obj.created_date,
taken_at=daily_round_obj.taken_at,
)
return daily_round_obj

Expand Down
4 changes: 2 additions & 2 deletions care/facility/api/serializers/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def update(self, instance, validated_data):
consultation,
self.context["request"].user.id,
consultation.modified_date,
old_instance,
old=old_instance,
)

if "assigned_to" in validated_data:
Expand Down Expand Up @@ -819,7 +819,7 @@ def update(self, instance: PatientConsultation, validated_data):
instance,
self.context["request"].user.id,
instance.modified_date,
old_instance,
old=old_instance,
)

return instance
Expand Down
35 changes: 34 additions & 1 deletion care/facility/api/viewsets/consultation_diagnosis.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from copy import copy

from django.shortcuts import get_object_or_404
from django_filters import rest_framework as filters
from dry_rest_permissions.generics import DRYPermissions
from rest_framework import mixins
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from care.facility.api.serializers.consultation_diagnosis import (
ConsultationDiagnosisSerializer,
)
from care.facility.events.handler import create_consultation_events
from care.facility.models import (
ConditionVerificationStatus,
ConsultationDiagnosis,
Expand Down Expand Up @@ -52,4 +56,33 @@ def get_queryset(self):

def perform_create(self, serializer):
consultation = self.get_consultation_obj()
serializer.save(consultation=consultation, created_by=self.request.user)
diagnosis = serializer.save(
consultation=consultation, created_by=self.request.user
)
create_consultation_events(
consultation.id,
diagnosis,
caused_by=self.request.user.id,
created_date=diagnosis.created_date,
)

def perform_update(self, serializer):
return serializer.save()

def update(self, request, *args, **kwargs):
partial = kwargs.pop("partial", False)
instance = self.get_object()
old_instance = copy(instance)
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
instance = self.perform_update(serializer)

create_consultation_events(
instance.consultation_id,
instance,
caused_by=self.request.user.id,
created_date=instance.created_date,
old=old_instance,
)

return Response(serializer.data)
15 changes: 12 additions & 3 deletions care/facility/api/viewsets/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ def roots(self, request):


class PatientConsultationEventFilterSet(filters.FilterSet):
ordering = filters.OrderingFilter(
fields=(
"created_date",
"taken_at",
)
)

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


class PatientConsultationEventViewSet(ReadOnlyModelViewSet):
Expand Down
56 changes: 24 additions & 32 deletions care/facility/events/handler.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,56 @@
from contextlib import suppress
from datetime import datetime

from django.core.exceptions import FieldDoesNotExist
from django.db import transaction
from django.db.models import Field, Model
from django.db.models import Model
from django.db.models.query import QuerySet
from django.utils.timezone import now

from care.facility.models.events import ChangeType, EventType, PatientConsultationEvent
from care.utils.event_utils import get_changed_fields, serialize_field


def transform(
object_instance: Model,
old_instance: Model,
fields_to_store: set[str] | None = None,
) -> dict[str, any]:
fields: set[Field] = set()
if old_instance:
changed_fields = get_changed_fields(old_instance, object_instance)
fields = {
field
for field in object_instance._meta.fields
if field.name in changed_fields
}
else:
fields = set(object_instance._meta.fields)

if fields_to_store:
fields = {field for field in fields if field.name in fields_to_store}

return {field.name: serialize_field(object_instance, field) for field in fields}


def create_consultation_event_entry(
consultation_id: int,
object_instance: Model,
caused_by: int,
created_date: datetime,
taken_at: datetime,
old_instance: Model | None = None,
fields_to_store: set[str] | None = None,
):
change_type = ChangeType.UPDATED if old_instance else ChangeType.CREATED

data = transform(object_instance, old_instance, fields_to_store)
fields_to_store = fields_to_store or set(data.keys())
fields: set[str] = (
get_changed_fields(old_instance, object_instance)
if old_instance
else {field.name for field in object_instance._meta.fields}
)

fields_to_store = fields_to_store & fields if fields_to_store else fields

batch = []
groups = EventType.objects.filter(
model=object_instance.__class__.__name__, fields__len__gt=0, is_active=True
).values_list("id", "fields")
for group_id, group_fields in groups:
if set(group_fields) & fields_to_store:
if fields_to_store & {field.split("__", 1)[0] for field in group_fields}:
value = {}
for field in group_fields:
try:
value[field] = data[field]
except KeyError:
value[field] = getattr(object_instance, field, None)
# if all values in the group are Falsy, skip creating the event for this group
with suppress(FieldDoesNotExist):
value[field] = serialize_field(object_instance, field)

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

View check run for this annotation

Codecov / codecov/patch

care/facility/events/handler.py#L42

Added line #L42 was not covered by tests

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,
created_date__lt=created_date,
taken_at__lt=taken_at,
).update(is_latest=False)
batch.append(
PatientConsultationEvent(
Expand All @@ -74,6 +59,7 @@
event_type_id=group_id,
is_latest=True,
created_date=created_date,
taken_at=taken_at,
object_model=object_instance.__class__.__name__,
object_id=object_instance.id,
value=value,
Expand All @@ -94,12 +80,16 @@
objects: list | QuerySet | Model,
caused_by: int,
created_date: datetime = None,
taken_at: datetime = None,
old: Model | None = None,
fields_to_store: list[str] | set[str] | None = None,
):
if created_date is None:
created_date = now()

if taken_at is None:
taken_at = created_date

with transaction.atomic():
if isinstance(objects, (QuerySet, list, tuple)):
if old is not None:
Expand All @@ -112,6 +102,7 @@
obj,
caused_by,
created_date,
taken_at,
fields_to_store=set(fields_to_store) if fields_to_store else None,
)
else:
Expand All @@ -120,6 +111,7 @@
objects,
caused_by,
created_date,
taken_at,
old,
fields_to_store=set(fields_to_store) if fields_to_store else None,
)
15 changes: 8 additions & 7 deletions care/facility/management/commands/load_event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ class Command(BaseCommand):
"name": "INVESTIGATION",
"fields": ("investigation",),
},
# disabling until we have a better way to serialize user objects
# {
# "name": "TREATING_PHYSICIAN",
# "fields": ("treating_physician",),
# },
{
"name": "TREATING_PHYSICIAN",
"fields": (
"treating_physician__username",
"treating_physician__full_name",
),
},
),
},
{
Expand Down Expand Up @@ -225,7 +227,7 @@ class Command(BaseCommand):
{
"name": "DIAGNOSIS",
"model": "ConsultationDiagnosis",
"fields": ("diagnosis", "verification_status", "is_principal"),
"fields": ("diagnosis__label", "verification_status", "is_principal"),
},
{
"name": "SYMPTOMS",
Expand All @@ -246,7 +248,6 @@ class Command(BaseCommand):
"VENTILATOR_MODES",
"SYMPTOMS",
"ROUND_SYMPTOMS",
"TREATING_PHYSICIAN",
)

def create_objects(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.10 on 2024-07-02 10:44

from django.db import migrations, models


def backfill_taken_at(apps, schema_editor):
PatientConsultationEvent = apps.get_model("facility", "PatientConsultationEvent")
PatientConsultationEvent.objects.filter(taken_at__isnull=True).update(
taken_at=models.F("created_date")
)


class Migration(migrations.Migration):
dependencies = [
("facility", "0446_alter_notification_event"),
]

operations = [
migrations.AddField(
model_name="patientconsultationevent",
name="taken_at",
field=models.DateTimeField(db_index=True, null=True),
),
migrations.RunPython(backfill_taken_at, reverse_code=migrations.RunPython.noop),
migrations.AlterField(
model_name="patientconsultationevent",
name="taken_at",
field=models.DateTimeField(db_index=True),
),
]
1 change: 1 addition & 0 deletions care/facility/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PatientConsultationEvent(models.Model):
)
caused_by = models.ForeignKey(User, on_delete=models.PROTECT)
created_date = models.DateTimeField(db_index=True)
taken_at = models.DateTimeField(db_index=True)
object_model = models.CharField(
max_length=50, db_index=True, null=False, blank=False
)
Expand Down
4 changes: 4 additions & 0 deletions care/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@

CSV_MAKE_PRETTY = {"user_type": (lambda x: User.REVERSE_TYPE_MAP[x])}

@property
def full_name(self):
return self.get_full_name()

Check warning on line 316 in care/users/models.py

View check run for this annotation

Codecov / codecov/patch

care/users/models.py#L316

Added line #L316 was not covered by tests

@staticmethod
def has_read_permission(request):
return True
Expand Down
24 changes: 18 additions & 6 deletions care/utils/event_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from json import JSONEncoder
from logging import getLogger

from django.core.serializers import serialize
from django.core.exceptions import FieldDoesNotExist
from django.db.models import Field, Model
from multiselectfield.db.fields import MSFList, MultiSelectField

Expand All @@ -27,11 +27,23 @@
return changed_fields


def serialize_field(object: Model, field: Field):
value = getattr(object, field.name)
if isinstance(value, Model):
# serialize the fields of the related model
return serialize("python", [value])[0]["fields"]
def serialize_field(object: Model, field_name: str):
if "__" in field_name:
field_name, sub_field = field_name.split("__", 1)
related_object = getattr(object, field_name)
return serialize_field(related_object, sub_field)

Check warning on line 34 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L32-L34

Added lines #L32 - L34 were not covered by tests

field = None
try:
field = object._meta.get_field(field_name)
except FieldDoesNotExist as e:
try:

Check warning on line 40 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L36-L40

Added lines #L36 - L40 were not covered by tests
# try to get property field
return getattr(object, field_name)
except AttributeError:
raise e

Check warning on line 44 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L42-L44

Added lines #L42 - L44 were not covered by tests

value = getattr(object, field.name, None)

Check warning on line 46 in care/utils/event_utils.py

View check run for this annotation

Codecov / codecov/patch

care/utils/event_utils.py#L46

Added line #L46 was not covered by tests
if issubclass(field.__class__, Field) and field.choices:
# serialize choice fields with display value
return getattr(object, f"get_{field.name}_display", lambda: value)()
Expand Down
1 change: 1 addition & 0 deletions scripts/celery_beat-ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ done

python manage.py migrate --noinput
python manage.py load_redis_index
python manage.py load_event_types

touch /tmp/healthy

Expand Down
1 change: 1 addition & 0 deletions scripts/celery_beat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ done

python manage.py migrate --noinput
python manage.py load_redis_index
python manage.py load_event_types

touch /tmp/healthy

Expand Down