Skip to content

Commit

Permalink
feat: update the status of each projects on projects list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Oct 8, 2024
1 parent 1f7bb9e commit 83ddfe3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ async def all(
COUNT(t.id) AS total_task_count,
-- Count based on the latest state of tasks
COUNT(CASE WHEN te.state = 'LOCKED_FOR_MAPPING' THEN 1 END) AS ongoing_task_count
COUNT(CASE WHEN te.state = 'LOCKED_FOR_MAPPING' THEN 1 END) AS ongoing_task_count,
-- Count based on the latest state of tasks
COUNT(CASE WHEN te.state = 'IMAGE_PROCESSED' THEN 1 END) AS completed_task_count
FROM projects p
LEFT JOIN tasks t ON t.project_id = p.id
Expand Down Expand Up @@ -492,6 +495,8 @@ class ProjectOut(BaseModel):
tasks: Optional[list[TaskOut]] = []
image_url: Optional[str] = None
ongoing_task_count: Optional[int] = 0
completed_task_count: Optional[int] = 0
status: Optional[str] = "not-started"

@model_validator(mode="after")
def set_image_url(cls, values):
Expand All @@ -503,6 +508,21 @@ def set_image_url(cls, values):
values.image_url = get_presigned_url(settings.S3_BUCKET_NAME, image_dir, 5)
return values

@model_validator(mode="after")
def calculate_status(cls, values):
"""Set the project status based on task counts."""
ongoing_task_count = values.ongoing_task_count
completed_task_count = values.completed_task_count

if ongoing_task_count == 0:
values.status = "not-started"
elif ongoing_task_count > 0 and ongoing_task_count != completed_task_count:
values.status = "ongoing"
elif ongoing_task_count == completed_task_count:
values.status = "completed"

return values


class PresignedUrlRequest(BaseModel):
project_id: uuid.UUID
Expand Down

0 comments on commit 83ddfe3

Please sign in to comment.