Skip to content

Commit

Permalink
feat: updated the task as completed after process from s3
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Oct 8, 2024
1 parent 3ba1c73 commit befca95
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/backend/app/projects/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import tempfile
import shutil
from pathlib import Path
from app.tasks import task_logic
from app.models.enums import State
from app.utils import timestamp
from pyodm import Node
from app.s3 import get_file_from_bucket, list_objects_from_bucket, add_file_to_bucket
from loguru import logger as log
from concurrent.futures import ThreadPoolExecutor
from psycopg import Connection


class DroneImageProcessor:
Expand All @@ -14,6 +18,8 @@ def __init__(
node_odm_url: str,
project_id: uuid.UUID,
task_id: uuid.UUID,
user_id: str,
db: Connection,
):
"""
Initializes the connection to the ODM node.
Expand All @@ -22,6 +28,8 @@ def __init__(
self.node = Node.from_url(node_odm_url)
self.project_id = project_id
self.task_id = task_id
self.user_id = user_id
self.db = db

def options_list_to_dict(self, options=[]):
"""
Expand Down Expand Up @@ -147,6 +155,17 @@ def process_images_from_s3(self, bucket_name, name=None, options=[]):
# Upload the results into s3
s3_path = f"projects/{self.project_id}/{self.task_id}/assets.zip"
add_file_to_bucket(bucket_name, path_to_download, s3_path)
# now update the task as completed in Db.
task_logic.update_task_state(
self.db,
self.project_id,
self.task_id,
self.user_id,
"Task completed.",
State.LOCKED_FOR_MAPPING,
State.UNLOCKED_DONE,
timestamp(),
)
return task

finally:
Expand Down
8 changes: 6 additions & 2 deletions src/backend/app/projects/project_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ async def preview_split_by_square(boundary: str, meters: int):
)


def process_drone_images(project_id: uuid.UUID, task_id: uuid.UUID):
def process_drone_images(
project_id: uuid.UUID, task_id: uuid.UUID, user_id: str, db: Connection
):
# Initialize the processor
processor = DroneImageProcessor(settings.NODE_ODM_URL, project_id, task_id)
processor = DroneImageProcessor(
settings.NODE_ODM_URL, project_id, task_id, user_id, db
)

# Define processing options
options = [
Expand Down
6 changes: 5 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,12 @@ async def process_imagery(
],
user_data: Annotated[AuthUser, Depends(login_required)],
background_tasks: BackgroundTasks,
db: Annotated[Connection, Depends(database.get_db)],
):
background_tasks.add_task(project_logic.process_drone_images, project.id, task_id)
user_id = user_data.id
background_tasks.add_task(
project_logic.process_drone_images, project.id, task_id, user_id, db
)
return {"message": "Processing started"}


Expand Down

0 comments on commit befca95

Please sign in to comment.