From d66cf270db0b7635b66b65b0fe5e2c1c1cf3b10a Mon Sep 17 00:00:00 2001
From: Killian <killian.trouve@metapolis.fr>
Date: Fri, 17 Nov 2023 16:26:12 +0100
Subject: [PATCH 1/3] Feat -> public : in public add a colors folder, where we
 store all colors for graphs

---
 app/public/colors/graphs_colors.json | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 app/public/colors/graphs_colors.json

diff --git a/app/public/colors/graphs_colors.json b/app/public/colors/graphs_colors.json
new file mode 100644
index 00000000..13ec5a56
--- /dev/null
+++ b/app/public/colors/graphs_colors.json
@@ -0,0 +1,17 @@
+{
+    "Global_budget": {
+        "A analyser": "#C08FFF",
+        "A approfondir": "#FFC736",
+        "Neutre": "#B4B4B4",
+        "Hors perimetre": "#00808D",
+        "Indefini": "#7CAAFF"
+    },
+    "Project_sheets": {
+        "Très favorable": "#36813B",
+        "Favorable": "#4AAF51",
+        "Favorable sous condition": "#4AAF51",
+        "Neutre": "#B4B4B4",
+        "Défavorable": "#E0761A",
+        "Indéfini": "#7CAAFF"
+    }
+}
-- 
GitLab


From ba26432ec2df876f8ae07fb5d7b09569c03c2888 Mon Sep 17 00:00:00 2001
From: Killian <killian.trouve@metapolis.fr>
Date: Fri, 17 Nov 2023 16:27:13 +0100
Subject: [PATCH 2/3] Fix -> schemas : in projects_schema remove a useless part
 'Config', and change a little bit the return's schema

---
 app/schemas/projects_schema.py | 66 ++++------------------------------
 1 file changed, 7 insertions(+), 59 deletions(-)

diff --git a/app/schemas/projects_schema.py b/app/schemas/projects_schema.py
index c7163908..b5a34e3e 100644
--- a/app/schemas/projects_schema.py
+++ b/app/schemas/projects_schema.py
@@ -55,66 +55,14 @@ class ProjectStatusOutput(BaseModel):
     year: int
 
 
+class CategoryInfos(BaseModel):
+    color: str | None
+    infos: list[SubItem]
+
+
 class ResultOutput(BaseModel):
-    Investissement: dict[str, list[SubItem]]
-    Fonctionnement: dict[str, list[SubItem]]
-
-    class Config:
-        arbitrary_types_allowed = True
-        json_schema_extra = {
-            "investissements": {
-                "A approfondir": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Neutre": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "A analyser": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Non traité": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Hors périmètre": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Indéfini": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-            },
-            "fonctionnement": {
-                "à approfondir": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Neutre": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "à analyser": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Non traité": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Hors périmètre": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-                "Indéfini": [{
-                    "name": "str",
-                    "value": "float",
-                }],
-            }
-        }
+    Investissement: dict[str, CategoryInfos]
+    Fonctionnement: dict[str, CategoryInfos]
 
 
 class Information(BaseModel):
-- 
GitLab


From 093900ac6f8b2af60c12e435155140b125075460 Mon Sep 17 00:00:00 2001
From: Killian <killian.trouve@metapolis.fr>
Date: Fri, 17 Nov 2023 16:28:38 +0100
Subject: [PATCH 3/3] Feat -> controllers && crud : in the functions that were
 returning datas for graphs, change the return's schema and add elements
 accordingly

---
 app/controllers/green_budget_controller.py | 12 ++++++++++--
 app/crud/green_budget_crud.py              | 13 +++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/app/controllers/green_budget_controller.py b/app/controllers/green_budget_controller.py
index cdcf2c1b..5f31a1a1 100644
--- a/app/controllers/green_budget_controller.py
+++ b/app/controllers/green_budget_controller.py
@@ -205,13 +205,21 @@ def get_green_budget_analyse(
     result = mongo['GB' + '.' + str(project.id)]
     aggregated_results = result.aggregate(pipeline)
 
+    with open('app/public/colors/graphs_colors.json') as file:
+        data = json.load(file)
     result_dict: dict = {}
+    color = None
     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(
+            if res['analyse'] in data['Global_budget']:
+                color = data['Global_budget'][res['analyse']]
+            else:
+                color = None
+            result_dict[res['name']][res['analyse']] = {
+                "color": color, "infos": []}
+        result_dict[res['name']][res['analyse']]['infos'].append(
             {"name": res['direction'], "value": str(res['total_budget'])})
     analysis_return = ResultOutput(**result_dict)
 
diff --git a/app/crud/green_budget_crud.py b/app/crud/green_budget_crud.py
index 521c8eab..ddf1c188 100644
--- a/app/crud/green_budget_crud.py
+++ b/app/crud/green_budget_crud.py
@@ -1,4 +1,5 @@
 import hashlib
+import json
 import pickle
 import random
 import string
@@ -770,12 +771,20 @@ def loop_in_aggregated_results(
 
     :return: a dictionary containing the results
     """
+    with open('app/public/colors/graphs_colors.json') as file:
+        data = json.load(file)
+    color = None
     for res in aggregated_results:
         if res['name'] not in result_dict:
             result_dict[res['name']] = {}
         if res['grade'] not in result_dict[res['name']]:
-            result_dict[res['name']][res['grade']] = []
-        result_dict[res['name']][res['grade']].append(
+            if res['grade'] in data['Project_sheets']:
+                color = data['Project_sheets'][res['grade']]
+            else:
+                color = None
+            result_dict[res['name']][res['grade']] = {
+                "color": color, "infos": []}
+        result_dict[res['name']][res['grade']]['infos'].append(
             {"name": res['direction'],
              "value": str(res['total_budget'])})
     return result_dict
-- 
GitLab