2019-02-19 13:09:32 -05:00
|
|
|
#!/usr/bin/python3
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
|
2023-05-04 17:21:54 -03:00
|
|
|
"""
|
|
|
|
This module provides functions to track and report key performance indicators (KPIs) for a program.
|
|
|
|
"""
|
2019-02-19 13:09:32 -05:00
|
|
|
|
|
|
|
import time
|
|
|
|
from utils.install_log import LOG
|
|
|
|
|
|
|
|
STAGES = []
|
|
|
|
METRICS = {}
|
2023-05-04 17:21:54 -03:00
|
|
|
START = 0
|
2019-02-19 13:09:32 -05:00
|
|
|
|
2023-06-06 12:22:06 -03:00
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
def init_kpi_metrics():
|
2023-05-04 17:21:54 -03:00
|
|
|
"""
|
|
|
|
Initializes the global variable START with the current time to start tracking the
|
|
|
|
duration of a program.
|
|
|
|
"""
|
|
|
|
|
2023-06-06 12:22:06 -03:00
|
|
|
global START # pylint: disable=global-statement
|
2023-05-04 17:21:54 -03:00
|
|
|
START = time.time()
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
|
|
|
|
def get_formated_time(sec):
|
2023-05-04 17:21:54 -03:00
|
|
|
"""
|
|
|
|
Takes the duration in seconds and formats it in hours, minutes and seconds.
|
|
|
|
Returns the formatted string.
|
|
|
|
"""
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
hours = sec // 3600
|
|
|
|
sec %= 3600
|
|
|
|
minutes = sec // 60
|
|
|
|
sec %= 60
|
|
|
|
seconds = sec
|
|
|
|
if hours:
|
2023-05-04 17:21:54 -03:00
|
|
|
return f"{hours:.0f}h {minutes:.0f}m {seconds:.2f}s"
|
|
|
|
if minutes:
|
|
|
|
return f"{minutes:.0f}m {seconds:.2f}s"
|
|
|
|
return f"{seconds:.2f}s"
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
|
|
|
|
def set_kpi_metric(metric, duration):
|
2023-05-04 17:21:54 -03:00
|
|
|
"""Sets the duration of a metric and adds the metric to the global list of STAGES."""
|
|
|
|
|
2023-06-06 12:22:06 -03:00
|
|
|
global METRICS, STAGES # pylint: disable=global-statement, global-variable-not-assigned
|
2019-02-19 13:09:32 -05:00
|
|
|
METRICS[metric] = duration
|
|
|
|
STAGES.append(metric)
|
|
|
|
|
2023-05-04 17:21:54 -03:00
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
def print_kpi(metric):
|
2023-05-04 17:21:54 -03:00
|
|
|
"""Takes a metric as input and prints the duration of that metric using the LOG module."""
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
if metric in STAGES:
|
|
|
|
sec = METRICS[metric]
|
|
|
|
LOG.info(" Time in stage '%s': %s ", metric, get_formated_time(sec))
|
2023-05-04 17:21:54 -03:00
|
|
|
elif metric == 'total' and START:
|
|
|
|
duration = time.time() - START
|
2019-02-19 13:09:32 -05:00
|
|
|
LOG.info(" Total time: %s", get_formated_time(duration))
|
|
|
|
|
2023-05-04 17:21:54 -03:00
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
def get_kpi_str(metric):
|
2023-05-04 17:21:54 -03:00
|
|
|
"""Takes a metric as input and returns the duration of that metric as a formatted string."""
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
msg = ""
|
|
|
|
if metric in STAGES:
|
|
|
|
sec = METRICS[metric]
|
2023-06-06 12:22:06 -03:00
|
|
|
msg += f" Time in stage '{metric}': {get_formated_time(sec)} \n"
|
2023-05-04 17:21:54 -03:00
|
|
|
elif metric == 'total' and START:
|
|
|
|
duration = time.time() - START
|
2023-06-06 12:22:06 -03:00
|
|
|
msg += f" Total time: {get_formated_time(duration)}\n"
|
2019-02-19 13:09:32 -05:00
|
|
|
return msg
|
|
|
|
|
2023-05-04 17:21:54 -03:00
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
def get_kpi_metrics_str():
|
2023-05-04 17:21:54 -03:00
|
|
|
"""Returns a formatted string with all the metrics and their durations."""
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
msg = "===================== Metrics ====================\n"
|
|
|
|
for stage in STAGES:
|
|
|
|
msg += get_kpi_str(stage)
|
|
|
|
msg += get_kpi_str('total')
|
|
|
|
msg += "===============================================\n"
|
|
|
|
return msg
|
|
|
|
|
2023-05-04 17:21:54 -03:00
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
def print_kpi_metrics():
|
2023-05-04 17:21:54 -03:00
|
|
|
"""Prints all the metrics and their durations using the LOG module."""
|
|
|
|
|
2019-02-19 13:09:32 -05:00
|
|
|
LOG.info("===================== Metrics ====================")
|
|
|
|
for stage in STAGES:
|
|
|
|
print_kpi(stage)
|
|
|
|
print_kpi('total')
|
|
|
|
LOG.info("==================================================")
|