Skip to content

Commit

Permalink
Merge pull request #84 from hotosm/task_events
Browse files Browse the repository at this point in the history
Update task events endpoint to return task states
  • Loading branch information
nrjadkry authored Jul 22, 2024
2 parents e6dd4ab + 1034e94 commit c0e1090
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProjectIn(BaseModel):
dem_url: Optional[str] = None
gsd_cm_px: float = None
is_terrain_follow: bool = False
outline_no_fly_zones: Union[FeatureCollection, Feature, Polygon]
outline_no_fly_zones: Optional[Union[FeatureCollection, Feature, Polygon]] = None
outline_geojson: Union[FeatureCollection, Feature, Polygon]
output_orthophoto_url: Optional[str] = None
output_pointcloud_url: Optional[str] = None
Expand Down
46 changes: 43 additions & 3 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,56 @@
from app.models.enums import State


async def get_all_tasks(db: Database, project_id: uuid.UUID):
query = """
SELECT id FROM tasks WHERE project_id = :project_id
"""
values = {"project_id": str(project_id)}

data = await db.fetch_all(query, values)

# Extracting the list of IDs from the data
task_ids = [task["id"] for task in data]

return task_ids


async def all_tasks_states(db: Database, project_id: uuid.UUID):
query = """
SELECT DISTINCT ON (task_id) project_id, task_id, state
FROM task_events
WHERE project_id=:project_id
WHERE project_id = :project_id
ORDER BY task_id, created_at DESC
"""
"""

r = await db.fetch_all(query, {"project_id": str(project_id)})

return [dict(r) for r in r]
# Extract task_ids and corresponding states from the query result
existing_tasks = [dict(r) for r in r]

# Get all task_ids from the tasks table
task_ids = await get_all_tasks(db, project_id)

# Create a set of existing task_ids for quick lookup
existing_task_ids = {task["task_id"] for task in existing_tasks}

# task ids that are not in task_events table
remaining_task_ids = [x for x in task_ids if x not in existing_task_ids]

# Add missing tasks with state as "UNLOCKED_FOR_MAPPING"
remaining_tasks = [
{
"project_id": str(project_id),
"task_id": task_id,
"state": State.UNLOCKED_TO_MAP.name,
}
for task_id in remaining_task_ids
]

# Combine both existing tasks and remaining tasks
combined_tasks = existing_tasks + remaining_tasks

return combined_tasks


async def request_mapping(
Expand Down

0 comments on commit c0e1090

Please sign in to comment.