Merge "Added Time KPI metrics"

This commit is contained in:
Zuul 2025-05-12 13:36:33 +00:00 committed by Gerrit Code Review
commit 2709f067b5
2 changed files with 38 additions and 0 deletions

31
framework/kpi/time_kpi.py Normal file
View File

@ -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)

View File

@ -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