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

Reduce transaction scope #367

Open
wants to merge 3 commits into
base: v1.33
Choose a base branch
from
Open
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
60 changes: 19 additions & 41 deletions rbhl/management/commands/generate_clinic_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
Generates the clinic activity facts
"""
import datetime
from collections import defaultdict
from django.core.management.base import BaseCommand
from django.db import transaction
from opal.models import Episode
from rbhl.models import ClinicLog, Diagnosis, Fact, Referral
from rbhl.models import ClinicLog, Fact
import logging
logger = logging.getLogger('commands')


class Command(BaseCommand):
def get_five_year_episodes(cls):
def get_five_year_episodes(self):
today = datetime.date.today()
if today.month > 9:
five_year_range = (
Expand All @@ -32,8 +31,6 @@ def get_five_year_episodes(cls):
cliniclog__clinic_date__lt=five_year_range[1]
).distinct().prefetch_related(
"cliniclog_set",
"referral_set",
"diagnosis_set"
)

def handle(self, *args, **kwargs):
Expand All @@ -44,24 +41,27 @@ def handle(self, *args, **kwargs):
raise

@transaction.atomic
def save_facts(self, mean_clinic_patients_per_year, mean_known_diagnosis):
Fact.objects.filter(
label__in=[
Fact.MEAN_CLINIC_PATIENTS_PER_YEAR,
Fact.FIVE_YEAR_MEAN_KNOWN_DIAGNOSIS
]
).delete()
Fact.objects.create(
label=Fact.MEAN_CLINIC_PATIENTS_PER_YEAR,
value_int=mean_clinic_patients_per_year
)
Fact.objects.create(
label=Fact.FIVE_YEAR_MEAN_KNOWN_DIAGNOSIS,
value_int=mean_known_diagnosis
)

def process(self, *args, **options):
five_year_episodes = self.get_five_year_episodes()
episode_count = five_year_episodes.count()

referral_mean = round(episode_count/5)
Fact.objects.create(
label=Fact.MEAN_CLINIC_PATIENTS_PER_YEAR,
value_int=referral_mean
)
episode_ids = set(five_year_episodes.values_list('id', flat=True))
diagnosis = Diagnosis.objects.filter(
episode_id__in=episode_ids
).exclude(
date=None
).order_by("date")
episode_id_to_diagnosis = defaultdict(list)
for i in diagnosis:
episode_id_to_diagnosis[i.episode_id].append(i)
diagnosed_count = 0

# Diagnosis % is calclated by the
Expand All @@ -73,26 +73,4 @@ def process(self, *args, **options):
diagnosed_count += 1

mean_diagnosis_percent = round(diagnosed_count/episode_count * 100)
Fact.objects.filter(label=Fact.FIVE_YEAR_MEAN_KNOWN_DIAGNOSIS).delete()
Fact.objects.create(
label=Fact.FIVE_YEAR_MEAN_KNOWN_DIAGNOSIS,
value_int=mean_diagnosis_percent
)

# Diagnosis time is calculated the time from our first diagnosis
# after the referral date
total_days = 0
count_with_diagnosis = 0
for episode in five_year_episodes:
referral = Referral.get_recent_referral_for_episode(episode)
if referral and referral.date_of_referral:
diagnoses = episode.diagnosis_set.all()
diagnoses = [i for i in diagnoses if i.date]
diagnosis_after_referral = [
i for i in diagnoses if i.date > referral.date_of_referral
]
if diagnosis_after_referral:
diagnosis = diagnosis_after_referral[0]
days_to_diagnosis = diagnosis.date - referral.date_of_referral
total_days += days_to_diagnosis.days
count_with_diagnosis += 1
self.save_facts(referral_mean, mean_diagnosis_percent)