From 67b0309bf37b398bfd53a49f08731bcdadda87ff Mon Sep 17 00:00:00 2001 From: Tri Thien Nguyen <119530400+AndrewThien@users.noreply.github.com> Date: Thu, 28 Nov 2024 13:25:54 +0000 Subject: [PATCH] Add data models for ScanReport Job overview (#916) Co-authored-by: Andy Rae --- app/api/config/settings.py | 1 + app/shared/shared/jobs/__init__.py | 0 app/shared/shared/jobs/apps.py | 6 + .../shared/jobs/migrations/0001_initial.py | 158 ++++++++++++++++++ app/shared/shared/jobs/migrations/__init__.py | 0 app/shared/shared/jobs/models.py | 32 ++++ 6 files changed, 197 insertions(+) create mode 100644 app/shared/shared/jobs/__init__.py create mode 100644 app/shared/shared/jobs/apps.py create mode 100644 app/shared/shared/jobs/migrations/0001_initial.py create mode 100644 app/shared/shared/jobs/migrations/__init__.py create mode 100644 app/shared/shared/jobs/models.py diff --git a/app/api/config/settings.py b/app/api/config/settings.py index 6048a087f..03d09fa11 100644 --- a/app/api/config/settings.py +++ b/app/api/config/settings.py @@ -71,6 +71,7 @@ "revproxy", "shared", "shared.files", + "shared.jobs", ] MIDDLEWARE = [ diff --git a/app/shared/shared/jobs/__init__.py b/app/shared/shared/jobs/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/shared/shared/jobs/apps.py b/app/shared/shared/jobs/apps.py new file mode 100644 index 000000000..105d7e551 --- /dev/null +++ b/app/shared/shared/jobs/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class JobsConfig(AppConfig): + name = "shared.jobs" + label = "jobs" diff --git a/app/shared/shared/jobs/migrations/0001_initial.py b/app/shared/shared/jobs/migrations/0001_initial.py new file mode 100644 index 000000000..be6dd8e9c --- /dev/null +++ b/app/shared/shared/jobs/migrations/0001_initial.py @@ -0,0 +1,158 @@ +# Generated by Django 4.2.15 on 2024-11-28 09:27 + +from django.db import migrations, models +import django.db.models.deletion +from django.conf import settings + + +def seeding_objects(apps, schema_editor): + JobStage = apps.get_model("jobs", "JobStage") + StageStatus = apps.get_model("jobs", "StageStatus") + + job_stages = [ + {"id": 1, "value": "UPLOAD_SCAN_REPORT", "display_name": "Upload Scan Report"}, + { + "id": 2, + "value": "BUILD_CONCEPTS_FROM_DICT", + "display_name": "Build concepts from OMOP Data dictionary", + }, + { + "id": 3, + "value": "REUSE_CONCEPTS", + "display_name": "Reuse concepts from other scan reports", + }, + { + "id": 4, + "value": "GENERATE_RULES", + "display_name": "Auto generate mapping rules from available concepts", + }, + { + "id": 5, + "value": "DOWNLOAD_RULES", + "display_name": "Generate and download mapping rules JSON", + }, + ] + + for stage in job_stages: + JobStage.objects.create( + id=stage["id"], + value=stage["value"], + display_name=stage["display_name"], + ) + + stage_statuses = [ + { + "id": 1, + "value": "IN_PROGRESS", + "display_name": "Job in Progress", + }, + { + "id": 2, + "value": "COMPLETE", + "display_name": "Job Complete", + }, + {"id": 3, "value": "FAILED", "display_name": "Job Failed"}, + ] + + for status in stage_statuses: + StageStatus.objects.create( + id=status["id"], + value=status["value"], + display_name=status["display_name"], + ) + + +def unseeding_objects(apps, schema_editor): + JobStage = apps.get_model("jobs", "JobStage") + StageStatus = apps.get_model("jobs", "StageStatus") + # Remove all objects + JobStage.objects.all().delete() + StageStatus.objects.all().delete() + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("mapping", "0005_auto_20241015_0900"), + ] + + operations = [ + migrations.CreateModel( + name="JobStage", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("value", models.CharField(max_length=64)), + ("display_name", models.CharField(max_length=64)), + ], + ), + migrations.CreateModel( + name="StageStatus", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("value", models.CharField(max_length=64)), + ("display_name", models.CharField(max_length=64)), + ], + ), + migrations.RunPython(seeding_objects, unseeding_objects), + migrations.CreateModel( + name="Job", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("scan_report_id", models.IntegerField(null=True)), + ("scan_report_table_id", models.IntegerField(null=True)), + ( + "stage", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="job_stage", + to="jobs.jobstage", + ), + ), + ( + "status", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="stage_status", + to="jobs.stagestatus", + ), + ), + ("details", models.CharField(blank=True, max_length=256, null=True)), + ("created_at", models.DateTimeField(auto_now_add=True)), + ("updated_at", models.DateTimeField(auto_now=True)), + ], + options={ + "abstract": False, + }, + ), + ] diff --git a/app/shared/shared/jobs/migrations/__init__.py b/app/shared/shared/jobs/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/shared/shared/jobs/models.py b/app/shared/shared/jobs/models.py new file mode 100644 index 000000000..45349e270 --- /dev/null +++ b/app/shared/shared/jobs/models.py @@ -0,0 +1,32 @@ +from django.db import models +from shared.mapping.models import BaseModel + + +class JobStage(models.Model): + value = models.CharField(max_length=64) + display_name = models.CharField(max_length=64) + + +class StageStatus(models.Model): + value = models.CharField(max_length=64) + display_name = models.CharField(max_length=64) + + +class Job(BaseModel): + scan_report_id = models.IntegerField(null=True) + scan_report_table_id = models.IntegerField(null=True) + stage = models.ForeignKey( + "JobStage", + null=True, + blank=True, + on_delete=models.DO_NOTHING, + related_name="job_stage", + ) + status = models.ForeignKey( + "StageStatus", + null=True, + blank=True, + on_delete=models.DO_NOTHING, + related_name="stage_status", + ) + details = models.CharField(max_length=256, null=True, blank=True)