From a46c0eed39447030f04c57fffa23d20605e7e870 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 3 Sep 2024 17:48:24 +0100 Subject: [PATCH] Setting up energy assessments extraction process --- backend/app/config.py | 3 ++ backend/app/energy_assessments/router.py | 65 +++++++++++++++++++++++ backend/app/energy_assessments/schemas.py | 7 +++ 3 files changed, 75 insertions(+) create mode 100644 backend/app/energy_assessments/router.py create mode 100644 backend/app/energy_assessments/schemas.py diff --git a/backend/app/config.py b/backend/app/config.py index b5ea72fe..9aaa0a52 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -33,6 +33,9 @@ class Settings(BaseSettings): HEATING_KWH_PREDICTIONS_BUCKET: str HOTWATER_KWH_PREDICTIONS_BUCKET: str + # Other S3 buckts + ENERGY_ASSESSMENTS_BUCKET: str + class Config: env_file = "backend/.env" diff --git a/backend/app/energy_assessments/router.py b/backend/app/energy_assessments/router.py new file mode 100644 index 00000000..8a52bdfb --- /dev/null +++ b/backend/app/energy_assessments/router.py @@ -0,0 +1,65 @@ +from fastapi import APIRouter, Depends +from starlette.responses import Response + +from backend.app.db.connection import db_engine + +from backend.app.config import get_settings +from backend.app.dependencies import validate_token +from backend.app.energy_assessments.schemas import EnergyAssessmentUploadPayload + +from sqlalchemy.exc import IntegrityError, OperationalError +from sqlalchemy.orm import sessionmaker + +from utils.logger import setup_logger + +logger = setup_logger() + +router = APIRouter( + prefix="/energy-assessments", + tags=["energy-assessments"], + dependencies=[Depends(validate_token)], + responses={404: {"description": "Not found"}} +) + + +@router.post("/upload") +async def upload(body: EnergyAssessmentUploadPayload): + """ + Given a location in S3, this service will retrieve the data in s3 and perform the following: + 1) Extract the data and store it to the data + 2) Extract the links to other artefacts collected during the energy assessment, such as EPRs, floor plans and + condition reports + + This will allow us to do the following: + 1) Present the findings of the energy assessment to the client + 2) Allow the end use to download the artefacts collected during the energy assessment + + Eventually, we will this service to collect the key documents from the service where they're uploaded + (e.g. Onedrive) and store them to S3, but for the moment, this is sufficient + """ + + logger.info("Connecting to db") + session = sessionmaker(bind=db_engine)() + + try: + logger.info("Uploading energy assessment data") + except IntegrityError: + logger.error("Database integrity error occurred", exc_info=True) + session.rollback() + return Response(status_code=500, content="Database integrity error.") + except OperationalError: + logger.error("Database operational error occurred", exc_info=True) + session.rollback() + return Response(status_code=500, content="Database operational error.") + except ValueError: + logger.error("Value error - possibly due to malformed data", exc_info=True) + session.rollback() + return Response(status_code=400, content="Bad request: malformed data.") + except Exception as e: # General exception handling + logger.error(f"An error occurred: {e}") + session.rollback() + return Response(status_code=500, content="An unexpected error occurred.") + finally: + session.close() + + return Response(status_code=200) diff --git a/backend/app/energy_assessments/schemas.py b/backend/app/energy_assessments/schemas.py new file mode 100644 index 00000000..83a9a44e --- /dev/null +++ b/backend/app/energy_assessments/schemas.py @@ -0,0 +1,7 @@ +from pydantic import BaseModel + + +class EnergyAssessmentUploadPayload(BaseModel): + portfolio_id: int + # This is the s3 location, where the informaton collected during the energy assessment is stored + s3_filepath: str