Skip to content
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
104 changes: 71 additions & 33 deletions tmh_registry/registry/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,24 @@ def discharge(self, request, pk=None):

return Response(data)

@action(
detail=True,
serializer_class=FollowUpReadSerializer,
queryset=FollowUp.objects.none(),
url_path="follow-ups",
)
def follow_ups(self, request, pk=None):
try:
episode = Episode.objects.get(pk=pk)
except ObjectDoesNotExist:
raise NotFound(f"Episode {pk=} not found.")

follow_ups = FollowUp.objects.filter(episode_id=episode.id)

serializer = FollowUpReadSerializer(follow_ups, many=True)

return Response(serializer.data)

@swagger_auto_schema(
method="get",
responses={
Expand All @@ -317,7 +335,7 @@ def discharge(self, request, pk=None):
)
@action(detail=False, methods=["get"])
def stats(self, request):
period = request.query_params.get("period")
# period = request.query_params.get("period")
group_by = request.query_params.get("group_by")

response: dict[str, Any] = {}
Expand All @@ -335,21 +353,31 @@ def stats(self, request):
# All hospital statistics
response["global"] = {
"total_episodes": all_episodes.count(),
"past_year_episodes": all_episodes.filter(surgery_date__gte=past_year).count(),
"past_month_episodes": all_episodes.filter(surgery_date__gte=past_month).count(),
"past_week_episodes": all_episodes.filter(surgery_date__gte=past_week).count(),
"past_year_episodes": all_episodes.filter(
surgery_date__gte=past_year
).count(),
"past_month_episodes": all_episodes.filter(
surgery_date__gte=past_month
).count(),
"past_week_episodes": all_episodes.filter(
surgery_date__gte=past_week
).count(),
"last_episode_date": all_episodes.aggregate(
last=Max("surgery_date")
)["last"],
"patients_without_episode": Patient.objects.filter(
hospital_mappings__isnull=False
).annotate(
)
.annotate(
has_episode=Exists(
Episode.objects.filter(
patient_hospital_mapping__patient=OuterRef("pk")
)
)
).filter(has_episode=False).distinct().count(),
)
.filter(has_episode=False)
.distinct()
.count(),
}

# Hospital specific statistics
Expand All @@ -365,35 +393,45 @@ def stats(self, request):
)

# Patients without episode
patients_without_episode = Patient.objects.filter(
hospital_mappings__hospital=hospital
).annotate(
has_episode=Exists(
Episode.objects.filter(
patient_hospital_mapping__patient=OuterRef("pk"),
patient_hospital_mapping__hospital=hospital,
patients_without_episode = (
Patient.objects.filter(
hospital_mappings__hospital=hospital
)
.annotate(
has_episode=Exists(
Episode.objects.filter(
patient_hospital_mapping__patient=OuterRef(
"pk"
),
patient_hospital_mapping__hospital=hospital,
)
)
)
).filter(has_episode=False).distinct().count()

hospital_rows.append({
"hospital_id": hospital.id,
"hospital_name": hospital.name,
"total_episodes": hospital_episodes.count(),
"past_year_episodes": hospital_episodes.filter(
surgery_date__gte=past_year
).count(),
"past_month_episodes": hospital_episodes.filter(
surgery_date__gte=past_month
).count(),
"past_week_episodes": hospital_episodes.filter(
surgery_date__gte=past_week
).count(),
"last_episode_date": hospital_episodes.aggregate(
last=Max("surgery_date")
)["last"],
"patients_without_episode": patients_without_episode,
})
.filter(has_episode=False)
.distinct()
.count()
)

hospital_rows.append(
{
"hospital_id": hospital.id,
"hospital_name": hospital.name,
"total_episodes": hospital_episodes.count(),
"past_year_episodes": hospital_episodes.filter(
surgery_date__gte=past_year
).count(),
"past_month_episodes": hospital_episodes.filter(
surgery_date__gte=past_month
).count(),
"past_week_episodes": hospital_episodes.filter(
surgery_date__gte=past_week
).count(),
"last_episode_date": hospital_episodes.aggregate(
last=Max("surgery_date")
)["last"],
"patients_without_episode": patients_without_episode,
}
)

response["by_hospital"] = hospital_rows

Expand Down
5 changes: 1 addition & 4 deletions tmh_registry/users/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def get_medical_personnel(self, user):
except MedicalPersonnel.DoesNotExist:
return None

return {
"level": mp.level,
"level_display": mp.get_level_display()
}
return {"level": mp.level, "level_display": mp.get_level_display()}


class MedicalPersonnelSerializer(ModelSerializer):
Expand Down