rcpch / rcpch-audit-engine

Epilepsy12 Audit Platform
https://e12.rcpch.ac.uk/
GNU Affero General Public License v3.0
5 stars 5 forks source link

s/seed end date generation broken #927

Closed dc2007git closed 3 months ago

dc2007git commented 3 months ago

NOTE BEFORE READING - FIXED IN #928

Attempted to boot up a fresh e12 instance in docker with s/up, and once that had worked, I went to run s/seed, but ran into the following error stack:

 Seeding 200 fictional cases... 
Creating 25 Cases in ADDENBROOKE'S HOSPITAL
Creating 25 Cases in AIREDALE GENERAL HOSPITAL
Creating 25 Cases in ALDER HEY CHILDREN'S HOSPITAL
Creating 25 Cases in BRONGLAIS GENERAL HOSPITAL
Creating 25 Cases in CHELSEA & WESTMINSTER HOSPITAL
Creating 25 Cases in CHEPSTOW COMMUNITY HOSPITAL
Creating 25 Cases in KING'S COLLEGE HOSPITAL (DENMARK HILL)
Creating 25 Cases in YSBYTY YSTRAD FAWR
docker compose exec django python manage.py seed --mode=seed_registrations $*
register cases in audit and complete all fields with random answers...
 Registering fictional cases in Epilepsy12... 
 Completing all the Epilepsy12 fields for the fictional cases... 
Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/epilepsy12/management/commands/seed.py", line 84, in handle
    run_registrations(cohort=cohort, full_year=completed_full_year)
  File "/app/epilepsy12/management/commands/seed.py", line 176, in run_registrations
    complete_registrations(verbose=verbose, cohort=cohort, full_year=full_year)
  File "/app/epilepsy12/management/commands/seed.py", line 232, in complete_registrations
    create_epilepsy12_record(registration_instance=registration, verbose=verbose)
  File "/app/epilepsy12/management/commands/create_e12_records.py", line 155, in create_epilepsy12_record
    assessment = create_assessment(
                 ^^^^^^^^^^^^^^^^^^
  File "/app/epilepsy12/management/commands/create_e12_records.py", line 477, in create_assessment
    assessment.childrens_epilepsy_surgical_service_referral_date = random_date(
                                                                   ^^^^^^^^^^^^
  File "/app/epilepsy12/general_functions/random_generator.py", line 14, in random_date
    raise Exception(
Exception: Cannot generate a random date if end date supplied is before start date.

The most relevant error seems to be related to seed.py, where a random date between a cohort's start and end date is generated for the first paediatric assessment date for a simulated case (line 223 in seed.py).

Importantly, while the above stack suggests that create_assessment() assessment.childrens_epilepsy_surgical_service_referral_date is the affected function and variable, after attempting s/seed a couple more times, I found that create_management() can be affected, in the case of antiepilepsy_medicine_startdate, and really any other 'create' can be affected.

This is the first time I've encountered an error like this, previously seeding was successful without any errors. Does this happen to anyone else?

dc2007git commented 3 months ago

For additional context/ logging of problem-solving:

After trying s/seed again, this time create_multiaxial_diagnosis() was the function inside of create_epilepsy12_record() that failed. The specific function call that failed is:

Syndrome.objects.create( syndrome_diagnosis_date=random_date( start=registration_instance.first_paediatric_assessment_date, end=date.today(), ), syndrome=syndrome_entity, multiaxial_diagnosis=multiaxial_diagnosis, )

Where the start date passed in was 2024-09-12, and the end date is today, ie 2024-04-30

Obviously this is in agreement with the error, where the end date (30-04-2024) is before the start date (12-09-2024). But why is this error just now appearing? Any ideas @mbarton @eatyourpeas ?