From e9843299d390bcd45495bb3bd5a333e91e215be0 Mon Sep 17 00:00:00 2001
From: Waleed Akbar <wakbar@cttc.es>
Date: Fri, 2 Aug 2024 10:04:31 +0000
Subject: [PATCH] minor changes in code.

- refine Kpi_DB.py methods.
- improve description of messages.
- imporve the text description.
---
 src/kpi_manager/database/Kpi_DB.py                       | 4 +---
 src/kpi_value_api/tests/messages.py                      | 5 +++--
 src/kpi_value_writer/service/MetricWriterToPrometheus.py | 4 ++--
 3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/kpi_manager/database/Kpi_DB.py b/src/kpi_manager/database/Kpi_DB.py
index 530abe457..49ad9c9b5 100644
--- a/src/kpi_manager/database/Kpi_DB.py
+++ b/src/kpi_manager/database/Kpi_DB.py
@@ -70,8 +70,7 @@ class KpiDB:
             session.rollback()
             if "psycopg2.errors.UniqueViolation" in str(e):
                 LOGGER.error(f"Unique key voilation: {row.__class__.__name__} table. {str(e)}")
-                raise AlreadyExistsException(row.__class__.__name__, row,
-                                             extra_details=["Unique key voilation: {:}".format(e)] )
+                raise AlreadyExistsException(row.__class__.__name__, row, extra_details=["Unique key voilation: {:}".format(e)] )
             else:
                 LOGGER.error(f"Failed to insert new row into {row.__class__.__name__} table. {str(e)}")
                 raise OperationFailedException ("Deletion by column id", extra_details=["unable to delete row {:}".format(e)])
@@ -90,7 +89,6 @@ class KpiDB:
                 print("{:} ID not found, No matching row: {:}".format(model.__name__, id_to_search))
                 return None
         except Exception as e:
-            session.rollback()
             LOGGER.debug(f"Failed to retrieve {model.__name__} ID. {str(e)}")
             raise OperationFailedException ("search by column id", extra_details=["unable to search row {:}".format(e)])
         finally:
diff --git a/src/kpi_value_api/tests/messages.py b/src/kpi_value_api/tests/messages.py
index c2a1cbb0b..d8ad14bd4 100644
--- a/src/kpi_value_api/tests/messages.py
+++ b/src/kpi_value_api/tests/messages.py
@@ -18,8 +18,9 @@ from common.proto.kpi_value_api_pb2 import KpiValue, KpiValueList
 
 def create_kpi_value_list():
     _create_kpi_value_list = KpiValueList()
-    # To run this experiment sucessfully, already existing UUID in KPI DB in necessary.
-    # because the UUID is used to get the descriptor form KPI DB.
+    # To run this experiment sucessfully, add an existing UUID of a KPI Descriptor from the KPI DB.
+    # This UUID is used to get the descriptor form the KPI DB. If the Kpi ID does not exists, 
+    # some part of the code won't execute.
     EXISTING_KPI_IDs = ["725ce3ad-ac67-4373-bd35-8cd9d6a86e09",
                         str(uuid.uuid4()), 
                         str(uuid.uuid4())]
diff --git a/src/kpi_value_writer/service/MetricWriterToPrometheus.py b/src/kpi_value_writer/service/MetricWriterToPrometheus.py
index 81324b759..f1d079783 100644
--- a/src/kpi_value_writer/service/MetricWriterToPrometheus.py
+++ b/src/kpi_value_writer/service/MetricWriterToPrometheus.py
@@ -63,8 +63,8 @@ class MetricWriterToPrometheus:
     def create_and_expose_cooked_kpi(self, kpi_descriptor: KpiDescriptor, kpi_value: KpiValue):
         # merge both gRPC messages into single varible.
         cooked_kpi = self.merge_kpi_descriptor_and_kpi_value(kpi_descriptor, kpi_value)
-        tags_to_exclude = {'kpi_description', 'kpi_sample_type', 'kpi_value'} # extracted values will be used as metric tag
-        metric_tags = [tag for tag in cooked_kpi.keys() if tag not in tags_to_exclude]
+        tags_to_exclude = {'kpi_description', 'kpi_sample_type', 'kpi_value'}           
+        metric_tags = [tag for tag in cooked_kpi.keys() if tag not in tags_to_exclude]  # These values will be used as metric tags
         metric_name = cooked_kpi['kpi_sample_type']
         try:
             if metric_name not in PROM_METRICS:     # Only register the metric, when it doesn't exists
-- 
GitLab