Skip to content

Commit

Permalink
fix: sql raw query & task approve status
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Aug 8, 2024
1 parent cdbd392 commit 3404da7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
13 changes: 6 additions & 7 deletions src/backend/app/migrations/versions/2b92f8a9bbec_.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

# revision identifiers, used by Alembic.
revision: str = "2b92f8a9bbec"
down_revision: Union[str, None] = "d862bfa31c36"
down_revision: Union[str, None] = "5d38e368b3d2"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

Expand All @@ -46,11 +46,10 @@ def upgrade() -> None:

def downgrade() -> None:
op.execute("ALTER TYPE state RENAME TO state_old")

old_state_enum.create(op.get_bind(), checkfirst=False)
op.execute(
(
"ALTER TABLE task_events "
"ALTER COLUMN state TYPE state USING state::text::state"
)
)
op.execute("ALTER TABLE task_events ALTER COLUMN state TYPE text USING state::text")
# op.execute(
# "ALTER TABLE task_events ALTER COLUMN state TYPE state USING state::text::state"
# )
op.execute("DROP TYPE state_old")
43 changes: 43 additions & 0 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,49 @@ async def request_mapping(
return {"project_id": project_id, "task_id": task_id, "comment": comment}


async def update_task_state(
db: Database,
project_id: uuid.UUID,
task_id: uuid.UUID,
user_id: str,
comment: str,
initial_state: State,
final_state: State,
):
query = """
WITH last AS (
SELECT *
FROM task_events
WHERE project_id = :project_id AND task_id = :task_id
ORDER BY created_at DESC
LIMIT 1
),
locked AS (
SELECT *
FROM last
WHERE user_id = :user_id AND state = :initial_state
)
INSERT INTO task_events(event_id, project_id, task_id, user_id, state, comment, created_at)
SELECT gen_random_uuid(), project_id, task_id, user_id, :final_state, :comment, now()
FROM last
WHERE user_id = :user_id
RETURNING project_id, task_id, user_id, state;
"""

values = {
"project_id": str(project_id),
"task_id": str(task_id),
"user_id": str(user_id),
"comment": comment,
"initial_state": initial_state.name,
"final_state": final_state.name,
}

await db.fetch_one(query, values)

return {"project_id": project_id, "task_id": task_id, "comment": comment}


async def update_or_create_task_state(
db: Database,
project_id: uuid.UUID,
Expand Down
40 changes: 13 additions & 27 deletions src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,16 @@ async def get_task_stats(

if not records:
raise HTTPException(status_code=404, detail="User profile not found")

raw_sql = """
SELECT
(SELECT COUNT(*)
FROM tasks t
LEFT JOIN task_events te ON t.id = te.task_id
WHERE t.project_id IN (SELECT id FROM projects WHERE author_id = :user_id)
AND te.state = 'LOCKED_FOR_MAPPING') AS ongoing_tasks,
(SELECT COUNT(*)
FROM tasks t
LEFT JOIN task_events te ON t.id = te.task_id
WHERE t.project_id IN (SELECT id FROM projects WHERE author_id = :user_id)
AND te.state = 'REQUEST_FOR_MAPPING') AS request_logs,
(SELECT COUNT(*)
FROM tasks t
LEFT JOIN task_events te ON t.id = te.task_id
WHERE t.project_id IN (SELECT id FROM projects WHERE author_id = :user_id)
AND te.state = 'UNLOCKED_DONE') AS completed_tasks,
(SELECT COUNT(*)
FROM tasks t
LEFT JOIN task_events te ON t.id = te.task_id
WHERE t.project_id IN (SELECT id FROM projects WHERE author_id = :user_id)
AND te.state = 'UNFLYABLE_TASK') AS unflyable_tasks
"""
COUNT(CASE WHEN te.state = 'LOCKED_FOR_MAPPING' THEN 1 END) AS ongoing_tasks,
COUNT(CASE WHEN te.state = 'REQUEST_FOR_MAPPING' THEN 1 END) AS request_logs,
COUNT(CASE WHEN te.state = 'UNLOCKED_DONE' THEN 1 END) AS completed_tasks,
COUNT(CASE WHEN te.state = 'UNFLYABLE_TASK' THEN 1 END) AS unflyable_tasks
FROM tasks t
LEFT JOIN task_events te ON t.id = te.task_id
WHERE t.project_id IN (SELECT id FROM projects WHERE author_id = :user_id);
"""

try:
db_counts = await db.fetch_one(query=raw_sql, values={"user_id": user_id})
Expand Down Expand Up @@ -291,7 +277,7 @@ async def new_event(
html_content,
)

return await task_crud.update_or_create_task_state(
return await task_crud.update_task_state(
db,
project_id,
task_id,
Expand All @@ -301,7 +287,7 @@ async def new_event(
State.UNLOCKED_TO_MAP,
)
case EventType.FINISH:
return await task_crud.update_or_create_task_state(
return await task_crud.update_task_state(
db,
project_id,
task_id,
Expand All @@ -311,7 +297,7 @@ async def new_event(
State.UNLOCKED_TO_VALIDATE,
)
case EventType.VALIDATE:
return await task_crud.update_or_create_task_state(
return await task_crud.update_task_state(
db,
project_id,
task_id,
Expand All @@ -321,7 +307,7 @@ async def new_event(
State.LOCKED_FOR_VALIDATION,
)
case EventType.GOOD:
return await task_crud.update_or_create_task_state(
return await task_crud.update_task_state(
db,
project_id,
task_id,
Expand All @@ -332,7 +318,7 @@ async def new_event(
)

case EventType.BAD:
return await task_crud.update_or_create_task_state(
return await task_crud.update_task_state(
db,
project_id,
task_id,
Expand Down

0 comments on commit 3404da7

Please sign in to comment.