Skip to content

Commit

Permalink
fix: enable tasks/activity endpoint until merged with dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Oct 29, 2024
1 parent a123d10 commit e7130b1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 62 deletions.
82 changes: 44 additions & 38 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,48 @@
#
"""Logic for FMTM tasks."""

from datetime import datetime, timedelta

# FIXME the endpoint that calls this isn't used?
# async def get_project_task_activity(
# db: Connection,
# project_id: int,
# days: int,
# ) -> task_schemas.TaskEventCount:
# """Get number of tasks mapped and validated for project.

# Args:
# project_id (int): The ID of the project.
# days (int): The number of days to consider for the
# task activity (default: 10).
# db (Connection): The database connection.

# Returns:
# list[task_schemas.TaskEventCount]: A list of task event counts.
# """
# end_date = datetime.now() - timedelta(days=days)

# sql = """
# SELECT
# to_char(created_at::date, 'dd/mm/yyyy') as date,
# COUNT(*) FILTER (WHERE state = 'UNLOCKED_DONE') AS validated,
# COUNT(*) FILTER (WHERE state = 'UNLOCKED_TO_VALIDATE') AS mapped
# FROM
# task_events
# WHERE
# project_id = %(project_id)s
# AND created_at >= %(end_date)s
# GROUP BY
# created_at::date
# ORDER BY
# created_at::date;
# """

# async with db.cursor(row_factory=class_row(task_schemas.TaskEventCount)) as cur:
# await cur.execute(sql, {"project_id": project_id, "end_date": end_date})
# return await cur.fetchall()
from psycopg import Connection
from psycopg.rows import class_row

from app.tasks import task_schemas


async def get_project_task_activity(
db: Connection,
project_id: int,
days: int,
) -> task_schemas.TaskEventCount:
"""Get number of tasks mapped and validated for project.
Args:
project_id (int): The ID of the project.
days (int): The number of days to consider for the
task activity (default: 10).
db (Connection): The database connection.
Returns:
list[task_schemas.TaskEventCount]: A list of task event counts.
"""
end_date = datetime.now() - timedelta(days=days)

sql = """
SELECT
to_char(created_at::date, 'dd/mm/yyyy') as date,
COUNT(*) FILTER (WHERE state = 'UNLOCKED_DONE') AS validated,
COUNT(*) FILTER (WHERE state = 'UNLOCKED_TO_VALIDATE') AS mapped
FROM
task_events
WHERE
project_id = %(project_id)s
AND created_at >= %(end_date)s
GROUP BY
created_at::date
ORDER BY
created_at::date;
"""

async with db.cursor(row_factory=class_row(task_schemas.TaskEventCount)) as cur:
await cur.execute(sql, {"project_id": project_id, "end_date": end_date})
return await cur.fetchall()
46 changes: 22 additions & 24 deletions src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
from app.db.database import db_conn
from app.db.enums import HTTPStatus
from app.db.models import DbTask, DbTaskEvent
from app.tasks import task_schemas
from app.tasks.task_deps import get_task
from app.tasks import task_crud, task_schemas

router = APIRouter(
prefix="/tasks",
Expand Down Expand Up @@ -85,35 +84,34 @@ async def add_new_task_event(
return await DbTaskEvent.create(db, new_event)


# FIXME this endpoint isn't used?
# @router.get("/activity/", response_model=list[task_schemas.TaskEventCount])
# async def task_activity(
# project_id: int,
# db: Annotated[Connection, Depends(db_conn)],
# project_user: Annotated[ProjectUserDict, Depends(mapper)],
# days: int = 10,
# ):
# """Get the number of mapped or validated tasks on each day.

# Return format:
# [
# {
# date: DD/MM/YYYY,
# validated: int,
# mapped: int,
# }
# ]
# """
# return await task_crud.get_project_task_activity(db, project_id, days)
@router.get("/activity/", response_model=list[task_schemas.TaskEventCount])
async def task_activity(
project_id: int,
db: Annotated[Connection, Depends(db_conn)],
project_user: Annotated[ProjectUserDict, Depends(mapper)],
days: int = 10,
):
"""Get the number of mapped or validated tasks on each day.
Return format:
[
{
date: DD/MM/YYYY,
validated: int,
mapped: int,
}
]
"""
return await task_crud.get_project_task_activity(db, project_id, days)


@router.get("/{task_id}/history/", response_model=list[task_schemas.TaskEventOut])
async def get_task_event_history(
task_id: int,
db: Annotated[Connection, Depends(db_conn)],
db_task: Annotated[DbTask, Depends(get_task)],
project_user: Annotated[ProjectUserDict, Depends(mapper)],
days: int = 10,
comments: bool = False,
):
"""Get the detailed history for a task."""
return await DbTaskEvent.all(db, task_id=db_task.id, days=days, comments=comments)
return await DbTaskEvent.all(db, task_id=task_id, days=days, comments=comments)

0 comments on commit e7130b1

Please sign in to comment.