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

Feat: add counts for total tasks and ongoing tasks in project retrieval #216

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
37 changes: 29 additions & 8 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,38 @@ async def all(
limit: int = 100,
):
"""
Get all projects. Optionally filter by the project creator (user).
Get all projects, count total tasks and task states (ongoing, completed, etc.).
Optionally filter by the project creator (user).
"""
async with db.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
SELECT
id, slug, name, description, per_task_instructions,
ST_AsGeoJSON(outline)::jsonb AS outline,
requires_approval_from_manager_for_locking
FROM projects
WHERE (author_id = COALESCE(%(user_id)s, author_id))
ORDER BY created_at DESC
p.id, p.slug, p.name, p.description, p.per_task_instructions,
ST_AsGeoJSON(p.outline)::jsonb AS outline,
p.requires_approval_from_manager_for_locking,

-- Count total tasks for each project
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

FROM projects p
LEFT JOIN tasks t ON t.project_id = p.id
LEFT JOIN (
-- Get the latest event per task
SELECT DISTINCT ON (te.task_id)
te.task_id,
te.state,
te.created_at
FROM task_events te
ORDER BY te.task_id, te.created_at DESC
) AS te ON te.task_id = t.id

WHERE (p.author_id = COALESCE(%(user_id)s, p.author_id))
GROUP BY p.id
ORDER BY p.created_at DESC
OFFSET %(skip)s
LIMIT %(limit)s
""",
Expand Down Expand Up @@ -417,9 +437,10 @@ class ProjectOut(BaseModel):
outline: Optional[Polygon | Feature | FeatureCollection]
no_fly_zones: Optional[Polygon | Feature | FeatureCollection | MultiPolygon] = None
requires_approval_from_manager_for_locking: bool
task_count: int = 0
total_task_count: int = 0
tasks: Optional[list[TaskOut]] = []
image_url: Optional[str] = None
ongoing_task_count: Optional[int] = 0

@model_validator(mode="after")
def set_image_url(cls, values):
Expand Down