From eea435d6417752b7521ec5ce32f03d0225a944db Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Tue, 24 Feb 2026 17:08:21 +0000 Subject: [PATCH] get fast api app running locally --- backend/README.md | 2 +- backend/app/config.py | 11 ++++-- backend/app/plan/router.py | 69 +++++++++++++++++++++----------------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/backend/README.md b/backend/README.md index 005d6fc4..2ea6f153 100644 --- a/backend/README.md +++ b/backend/README.md @@ -172,7 +172,7 @@ For instance, if your server is running locally on port 8000, you can use curl to get a dummy token: ```commandline -curl http://localhost:8000/dummy-token +curl http://localhost:8000/local/dummy-token ``` You will receive a response containing the dummy JWT diff --git a/backend/app/config.py b/backend/app/config.py index feb312b4..d23dcd33 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -1,5 +1,6 @@ import os from functools import lru_cache +from pathlib import Path from pydantic_settings import BaseSettings, SettingsConfigDict from typing import Optional @@ -7,11 +8,17 @@ from typing import Optional def resolve_env_file() -> Optional[str]: env = os.getenv("ENVIRONMENT", "local") + backend_dir = Path(__file__).resolve().parents[1] + if env == "local": - return "backend/.env" + env_file = backend_dir / ".env" + print("USING ENV FILE:", env_file) + return str(env_file) if env == "test": - return "backend/.env.test" + env_file = backend_dir / ".env.test" + print("USING ENV FILE:", env_file) + return str(env_file) # prod = no env file return None diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index cdf2873d..f45daea3 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -38,6 +38,7 @@ router = APIRouter( ) settings = get_settings() +print("CONNECTION TO SQS IN REGION", settings.AWS_DEFAULT_REGION) sqs_client = boto3.client("sqs", settings.AWS_DEFAULT_REGION) @@ -55,46 +56,52 @@ async def trigger_categorisation( property_ids.sort() num_scenarios: int = get_scenarios_count_by_portfolio_id(payload.portfolio_id) - batch_size: int = math.ceil(1000 / num_scenarios) + batch_size: int = ( + math.ceil(1000 / num_scenarios) if num_scenarios > 1000 else num_scenarios + ) num_property_buckets: int = max(1, math.ceil(len(property_ids) / batch_size)) + print("num_scenarios", num_scenarios) + print("batch_size", batch_size) + print("num_property_buckets", num_property_buckets) + # Create task - task_id, _ = TasksInterface.create_task( - task_source="backend/plan/router.py:trigger_categorisation", - service="plan_engine", - inputs=payload.model_dump(), - task_only=True, - ) + # task_id, _ = TasksInterface.create_task( + # task_source="backend/plan/router.py:trigger_categorisation", + # service="plan_engine", + # inputs=payload.model_dump(), + # task_only=True, + # ) # Dispatch requests to lambdas - subtask_interface = SubTaskInterface() + # subtask_interface = SubTaskInterface() - for bucket_index in range(num_property_buckets): - bucket_property_ids: List[int] = [ - pid for pid in property_ids if pid % num_property_buckets == bucket_index - ] - bucket_request: CategorisationTriggerRequest = CategorisationTriggerRequest( - portfolio_id=payload.portfolio_id, - scenarios_to_consider=payload.scenarios_to_consider, - scenario_priority_order=payload.scenario_priority_order, - min_property_id=min(bucket_property_ids), - max_property_id=max(bucket_property_ids), - ) - # Create sub-task for each - subtask_id: UUID = subtask_interface.create_subtask( - task_id=task_id, inputs=bucket_request.model_dump() - ) + # for bucket_index in range(num_property_buckets): + # bucket_property_ids: List[int] = [ + # pid for pid in property_ids if pid % num_property_buckets == bucket_index + # ] + # bucket_request: CategorisationTriggerRequest = CategorisationTriggerRequest( + # portfolio_id=payload.portfolio_id, + # scenarios_to_consider=payload.scenarios_to_consider, + # scenario_priority_order=payload.scenario_priority_order, + # min_property_id=min(bucket_property_ids), + # max_property_id=max(bucket_property_ids), + # ) + # # Create sub-task for each + # subtask_id: UUID = subtask_interface.create_subtask( + # task_id=task_id, inputs=bucket_request.model_dump() + # ) - response = sqs_client.send_message( - QueueUrl="categorisation-queue-dev", - MessageBody=bucket_request.model_dump_json(), - ) + # response = sqs_client.send_message( + # QueueUrl="categorisation-queue-dev", + # MessageBody=bucket_request.model_dump_json(), + # ) - logger.info( - f"Chunk {bucket_index} sent to SQS. Property IDs {min(bucket_property_ids)}–{max(bucket_property_ids)}. Message ID: {response.get('MessageId')}" - ) + # logger.info( + # f"Chunk {bucket_index} sent to SQS. Property IDs {min(bucket_property_ids)}–{max(bucket_property_ids)}. Message ID: {response.get('MessageId')}" + # ) - await asyncio.sleep(0.05) # Small delay to avoid SQS throttling + # await asyncio.sleep(0.05) # Small delay to avoid SQS throttling return {"message": "Categorisation jobs distributed"}