Skip to content

Commit

Permalink
modify function to fit with the updated model
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewThien committed Dec 3, 2024
1 parent 4d5d61f commit 2df9265
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions app/workers/shared_code/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ScanReportConcept,
ScanReportField,
ScanReportValue,
ScanReportTable,
UploadStatus,
)
from shared_code.logger import logger
Expand Down Expand Up @@ -45,8 +46,8 @@ def create_or_update_job(
stage: JobStageType,
# The initial status. For a job that doesn't have any further updates, this is the final status.
status: StageStatusType,
scan_report_id: Optional[str] = None,
scan_report_table_id: Optional[str] = None,
scan_report: Optional[ScanReport] = None,
scan_report_table: Optional[ScanReportTable] = None,
details: Optional[str] = None,
) -> None:
"""
Expand All @@ -56,29 +57,27 @@ def create_or_update_job(
stage_entity = JobStage.objects.get(value=stage.name)
status_entity = StageStatus.objects.get(value=status.name)
# For a job related to SR
if scan_report_id:
if scan_report:
# Check if the record already exists
if not Job.objects.filter(
scan_report_id=scan_report_id, stage=stage_entity
).exists():
if not Job.objects.filter(scan_report=scan_report, stage=stage_entity).exists():
# If not, create a new record
Job.objects.create(
scan_report_id=scan_report_id,
scan_report=scan_report,
stage=stage_entity,
status=status_entity,
details=details,
)
else:
# If yes, update it, in case re-running the job
update_job(stage, status, scan_report_id=scan_report_id, details=details)
update_job(stage, status, scan_report=scan_report, details=details)
# For a job related to SR table
if scan_report_table_id:
if scan_report_table:
# Check if the record already exists
if not Job.objects.filter(
scan_report_table_id=scan_report_table_id, stage=stage_entity
scan_report_table=scan_report_table, stage=stage_entity
).exists():
Job.objects.create(
scan_report_table_id=scan_report_table_id,
scan_report_table=scan_report_table,
stage=stage_entity,
status=status_entity,
details=details,
Expand All @@ -87,16 +86,16 @@ def create_or_update_job(
update_job(
stage,
status,
scan_report_table_id=scan_report_table_id,
scan_report_table=scan_report_table,
details=details,
)


def update_job(
stage: JobStageType,
status: StageStatusType,
scan_report_id: Optional[str] = None,
scan_report_table_id: Optional[str] = None,
scan_report: Optional[ScanReport] = None,
scan_report_table: Optional[ScanReportTable] = None,
details: Optional[str] = None,
) -> None:
"""
Expand All @@ -112,13 +111,13 @@ def update_job(
job_stage_entity = JobStage.objects.get(value=stage.name)
stage_status_entity = StageStatus.objects.get(value=status.name)
# Get the job enity based on the passed id and stage
if scan_report_id:
if scan_report:
scan_report_job = Job.objects.get(
scan_report_id=scan_report_id, stage=job_stage_entity
scan_report=scan_report, stage=job_stage_entity
)
if scan_report_table_id:
if scan_report_table:
scan_report_job = Job.objects.get(
scan_report_table_id=scan_report_table_id, stage=job_stage_entity
scan_report_table=scan_report_table, stage=job_stage_entity
)
# Update status and details
scan_report_job.status = stage_status_entity
Expand Down

0 comments on commit 2df9265

Please sign in to comment.