get fast api app running locally

This commit is contained in:
Daniel Roth 2026-02-24 17:08:21 +00:00
parent 5c9a8b55f2
commit eea435d641
3 changed files with 48 additions and 34 deletions

View file

@ -172,7 +172,7 @@ For instance, if your server is running locally on port 8000, you can use curl
to get a dummy token: to get a dummy token:
```commandline ```commandline
curl http://localhost:8000/dummy-token curl http://localhost:8000/local/dummy-token
``` ```
You will receive a response containing the dummy JWT You will receive a response containing the dummy JWT

View file

@ -1,5 +1,6 @@
import os import os
from functools import lru_cache from functools import lru_cache
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import Optional from typing import Optional
@ -7,11 +8,17 @@ from typing import Optional
def resolve_env_file() -> Optional[str]: def resolve_env_file() -> Optional[str]:
env = os.getenv("ENVIRONMENT", "local") env = os.getenv("ENVIRONMENT", "local")
backend_dir = Path(__file__).resolve().parents[1]
if env == "local": if env == "local":
return "backend/.env" env_file = backend_dir / ".env"
print("USING ENV FILE:", env_file)
return str(env_file)
if env == "test": 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 # prod = no env file
return None return None

View file

@ -38,6 +38,7 @@ router = APIRouter(
) )
settings = get_settings() settings = get_settings()
print("CONNECTION TO SQS IN REGION", settings.AWS_DEFAULT_REGION)
sqs_client = boto3.client("sqs", settings.AWS_DEFAULT_REGION) sqs_client = boto3.client("sqs", settings.AWS_DEFAULT_REGION)
@ -55,46 +56,52 @@ async def trigger_categorisation(
property_ids.sort() property_ids.sort()
num_scenarios: int = get_scenarios_count_by_portfolio_id(payload.portfolio_id) 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)) 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 # Create task
task_id, _ = TasksInterface.create_task( # task_id, _ = TasksInterface.create_task(
task_source="backend/plan/router.py:trigger_categorisation", # task_source="backend/plan/router.py:trigger_categorisation",
service="plan_engine", # service="plan_engine",
inputs=payload.model_dump(), # inputs=payload.model_dump(),
task_only=True, # task_only=True,
) # )
# Dispatch requests to lambdas # Dispatch requests to lambdas
subtask_interface = SubTaskInterface() # subtask_interface = SubTaskInterface()
for bucket_index in range(num_property_buckets): # for bucket_index in range(num_property_buckets):
bucket_property_ids: List[int] = [ # bucket_property_ids: List[int] = [
pid for pid in property_ids if pid % num_property_buckets == bucket_index # pid for pid in property_ids if pid % num_property_buckets == bucket_index
] # ]
bucket_request: CategorisationTriggerRequest = CategorisationTriggerRequest( # bucket_request: CategorisationTriggerRequest = CategorisationTriggerRequest(
portfolio_id=payload.portfolio_id, # portfolio_id=payload.portfolio_id,
scenarios_to_consider=payload.scenarios_to_consider, # scenarios_to_consider=payload.scenarios_to_consider,
scenario_priority_order=payload.scenario_priority_order, # scenario_priority_order=payload.scenario_priority_order,
min_property_id=min(bucket_property_ids), # min_property_id=min(bucket_property_ids),
max_property_id=max(bucket_property_ids), # max_property_id=max(bucket_property_ids),
) # )
# Create sub-task for each # # Create sub-task for each
subtask_id: UUID = subtask_interface.create_subtask( # subtask_id: UUID = subtask_interface.create_subtask(
task_id=task_id, inputs=bucket_request.model_dump() # task_id=task_id, inputs=bucket_request.model_dump()
) # )
response = sqs_client.send_message( # response = sqs_client.send_message(
QueueUrl="categorisation-queue-dev", # QueueUrl="categorisation-queue-dev",
MessageBody=bucket_request.model_dump_json(), # MessageBody=bucket_request.model_dump_json(),
) # )
logger.info( # logger.info(
f"Chunk {bucket_index} sent to SQS. Property IDs {min(bucket_property_ids)}{max(bucket_property_ids)}. Message ID: {response.get('MessageId')}" # 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"} return {"message": "Categorisation jobs distributed"}