diff --git a/app/controllers/green_budget_controller.py b/app/controllers/green_budget_controller.py index dd352ec0f584b606d2567cfe9c276250d9231665..110e6c22a83e3ecdb96f2681be11caceb01aba64 100644 --- a/app/controllers/green_budget_controller.py +++ b/app/controllers/green_budget_controller.py @@ -1,4 +1,5 @@ from datetime import date, datetime, timezone +from typing import Any from fastapi import Depends, Request, UploadFile from motor.core import AgnosticClient @@ -8,7 +9,7 @@ from app.crud.projects_crud import (get_project_by_organisation_id_and_year, insert_a_project, update_file_object) from app.crud.user_crud import get_user_by_email from app.Engines.GreenBudgetEngine import GreenBudget -from app.schemas.projects_schema import ImportFileOutput +from app.schemas.projects_schema import ImportFileOutput, ResultOutput from . import get_client, get_db, router @@ -60,3 +61,54 @@ async def upload_green_budget( await mongo_db[collection_name].insert_many(json_data) return ImportFileOutput(**dico) + + +@router.get("/green_budget/analyse", response_model=ResultOutput) +async def get_green_budget_analyse( + req: Request, + year: int, + db: Session = Depends(get_db), + client: AgnosticClient = Depends(get_client) +) -> dict[Any, dict[Any, Any]]: + user_email = req.state.user_email + user = get_user_by_email(db, user_email) + id_organisation = user.organisation_id + project = get_project_by_organisation_id_and_year(db, year, + id_organisation) + mongo = client.OKO + + pipeline = [ + { + '$group': { + '_id': { + 'name': '$Section', + 'direction': '$CDR instr prog', + 'analyse': '$analyse' + }, + 'total_value': { + '$sum': '$mtbudgprec_v' + } + } + }, { + '$project': { + '_id': 0, + 'name': '$_id.name', + 'analyse': '$_id.analyse', + 'direction': '$_id.direction', + 'total_budget': '$total_value' + } + } + ] + result = mongo[str(project.id)] + aggregated_results = result.aggregate(pipeline) + + result_dict = {} + async for res in aggregated_results: + if res['name'] not in result_dict: + result_dict[res['name']] = {} + if res['analyse'] not in result_dict[res['name']]: + result_dict[res['name']][res['analyse']] = [] + result_dict[res['name']][res['analyse']].append( + {"name": res['direction'], "value": str(res['total_budget'])}) + + return result_dict diff --git a/app/routers/router.py b/app/routers/router.py index fcb1699af865a6a96e00b26872e27558ce42ce09..d56f36a061d0831e5f52879b038111181bdcfae3 100644 --- a/app/routers/router.py +++ b/app/routers/router.py @@ -5,7 +5,8 @@ from fastapi.security import OAuth2PasswordBearer from app.controllers.auth_controller import auth_login_controller, auth_refresh from app.controllers.auth_recovery_controller import ( change_password_controller, send_code_controller, validate_code_controller) -from app.controllers.green_budget_controller import upload_green_budget +from app.controllers.green_budget_controller import (get_green_budget_analyse, + upload_green_budget) from app.controllers.modules_controller import get_modules_infos from app.controllers.plans_controller import (create_plan, delete_plan, get_plans, update_plans) @@ -105,6 +106,10 @@ router.post( "/green_budget", tags=["GreenBudget"], dependencies=[Depends(security)])(upload_green_budget) +router.get( + "/green_budget/analyse", + tags=["GreenBudget"], + dependencies=[Depends(security)])(get_green_budget_analyse) router.get( "/projects", diff --git a/app/schemas/projects_schema.py b/app/schemas/projects_schema.py index f948bcfc80efd22601953f796ddfc4e0e65512ca..b164f78f6727bdc132ec9545410b66cf4ceb8474 100644 --- a/app/schemas/projects_schema.py +++ b/app/schemas/projects_schema.py @@ -13,3 +13,9 @@ class ProjectOutput(BaseModel): project_id: UUID import_date: str year: int + + +class ResultOutput(BaseModel): + # investissements: dict[str, list[dict[str, str | float]]] + # fonctionnement: dict[str, list[dict[str, str | float]]] + pass