diff --git a/framework/kpi/time_kpi.py b/framework/kpi/time_kpi.py new file mode 100644 index 00000000..0c96aa66 --- /dev/null +++ b/framework/kpi/time_kpi.py @@ -0,0 +1,31 @@ +import time + +from framework.logging.automation_logger import get_logger + + +class TimeKPI: + """ + A class to represent a time-based KPI (Key Performance Indicator). + """ + + def __init__(self, start_time: float): + """Initialize the TimeKPI with a name and value. + + Args: + start_time (float): The time when the KPI started. + """ + self.start_time = start_time + + def log_elapsed_time(self, end_time: float, kpi_name: str) -> float: + """Calculate the elapsed time since the start time. + + Args: + end_time (float): The time when the KPI ended. + kpi_name (str): The time when the KPI ended. + + Returns: + float: The elapsed time. + """ + elapsed_time = time.time() - self.start_time + message = f"{kpi_name} elapsed time: {elapsed_time:.2f} seconds" + get_logger().log_kpi(message) diff --git a/framework/logging/automation_logger.py b/framework/logging/automation_logger.py index 15f727d6..d8e71d3d 100644 --- a/framework/logging/automation_logger.py +++ b/framework/logging/automation_logger.py @@ -97,6 +97,13 @@ class AutomationLogger(logging.getLoggerClass()): """ self._log(logging.INFO, message, None, stacklevel=2, extra={'source': 'SSH'}) + def log_kpi(self, message): + """This info-level log statement is used to log elapsed time for KPIs. + Args: + message: The message that will be logged. + """ + self._log(logging.INFO, message, None, stacklevel=2, extra={"source": "KPI"}) + def get_log_folder(self) -> str: """ Getter for log folder