Skip to content

Commit

Permalink
feat: updated downlaod assets url
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Oct 4, 2024
1 parent 3e64732 commit 5dffbed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
9 changes: 8 additions & 1 deletion src/backend/app/projects/project_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def process_drone_images(project_id: uuid.UUID, task_id: uuid.UUID):
)


async def get_project_info_from_s3(project_id: uuid.UUID, task_id: uuid.UUID):
def get_project_info_from_s3(project_id: uuid.UUID, task_id: uuid.UUID):
"""
Helper function to get the number of images and the URL to download the assets.
"""
Expand Down Expand Up @@ -219,6 +219,13 @@ async def get_project_info_from_s3(project_id: uuid.UUID, task_id: uuid.UUID):
log.error(f"An error occurred while accessing assets file: {e}")
raise HTTPException(status_code=500, detail=str(e))

# return {
# "project_id":str(project_id),
# "task_id":str(task_id),
# "image_count":image_count,
# "assets_url":presigned_url,
# }

return project_schemas.AssetsInfo(
project_id=str(project_id),
task_id=str(task_id),
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,4 @@ async def get_assets_info(
"""
Endpoint to get the number of images and the URL to download the assets for a given project and task.
"""
return await project_logic.get_project_info_from_s3(project.id, task_id)
return project_logic.get_project_info_from_s3(project.id, task_id)
45 changes: 37 additions & 8 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid
from typing import Annotated, Optional, List
from datetime import datetime, date
from app.projects import project_logic
import geojson
from loguru import logger as log
from pydantic import BaseModel, computed_field, Field, model_validator
Expand All @@ -26,6 +27,13 @@
from app.s3 import get_image_dir_url


class AssetsInfo(BaseModel):
project_id: str
task_id: str
image_count: int
assets_url: Optional[str]


def validate_geojson(
value: FeatureCollection | Feature | Polygon,
) -> geojson.FeatureCollection:
Expand Down Expand Up @@ -121,16 +129,41 @@ def validate_to_json(cls, value):
return value


class AssetsInfoData(BaseModel):
project_id: int


class TaskOut(BaseModel):
"""Base project model."""

id: uuid.UUID
project_id: uuid.UUID
project_task_index: int
outline: Optional[Polygon | Feature | FeatureCollection]
outline: Optional[Polygon | Feature | FeatureCollection] = None
state: Optional[str] = None
user_id: Optional[str] = None
task_area: Optional[float] = None
name: Optional[str] = None
image_count: Optional[int] = None
assets_url: Optional[str] = None

@model_validator(mode="after")
def set_assets_url(cls, values):
"""Set image_url and image count before rendering the model."""
task_id = values.id
project_id = values.project_id

if task_id and project_id:
data = project_logic.get_project_info_from_s3(project_id, task_id)
if data:
return values.copy(
update={
"assets_url": data.assets_url,
"image_count": data.image_count,
}
)

return values


class DbProject(BaseModel):
Expand Down Expand Up @@ -227,6 +260,7 @@ async def one(db: Connection, project_id: uuid.UUID):
SELECT
t.id,
t.project_task_index,
t.project_id,
ST_AsGeoJSON(t.outline)::jsonb -> 'coordinates' AS coordinates,
ST_AsGeoJSON(t.outline)::jsonb -> 'type' AS type,
ST_XMin(ST_Envelope(t.outline)) AS xmin,
Expand All @@ -253,6 +287,7 @@ async def one(db: Connection, project_id: uuid.UUID):
user_id,
name,
task_area,
project_id,
jsonb_build_object(
'type', 'Feature',
'geometry', jsonb_build_object(
Expand All @@ -272,6 +307,7 @@ async def one(db: Connection, project_id: uuid.UUID):
)

task_records = await cur.fetchall()
print(task_records)
project_record.tasks = task_records if task_records is not None else []
project_record.task_count = len(task_records)
return project_record
Expand Down Expand Up @@ -457,10 +493,3 @@ class PresignedUrlRequest(BaseModel):
task_id: uuid.UUID
image_name: List[str]
expiry: int # Expiry time in hours


class AssetsInfo(BaseModel):
project_id: str
task_id: str
image_count: int
assets_url: Optional[str]
4 changes: 0 additions & 4 deletions src/backend/app/tasks/task_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,12 @@ async def all(db: Connection, project_id: uuid.UUID):
existing_tasks = await cur.fetchall()
# Get all task_ids from the tasks table
task_ids = await Task.get_all_tasks(db, project_id)
print("task ids = ", task_ids)

# Create a set of existing task_ids for quick lookup
existing_task_ids = {task.task_id for task in existing_tasks}
print("existing task ids = ", existing_task_ids)

# 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]
print("remaining task ids = ", remaining_task_ids)

# Add missing tasks with state as "UNLOCKED_FOR_MAPPING"
remaining_tasks = [
Expand All @@ -140,7 +137,6 @@ async def all(db: Connection, project_id: uuid.UUID):
}
for task_id in remaining_task_ids
]
print("remaining tasks = ", remaining_tasks)
# Combine both existing tasks and remaining tasks
combined_tasks = existing_tasks + remaining_tasks
return combined_tasks
Expand Down

0 comments on commit 5dffbed

Please sign in to comment.