From 055424a5c3b0a5e5a6f9b9094f7ffb9d0b90af22 Mon Sep 17 00:00:00 2001 From: MEHRSHAD MIRSHEKARY Date: Fri, 4 Oct 2024 13:45:31 +0330 Subject: [PATCH] :zap::sparkles: feat(utils): Add ormat_elapsed_time Utility for Human-Readable Time Formatting #### Key Features: - Handles time durations less than a minute by returning only seconds. - For durations of one minute or more, returns the time in the format of 'X minute(s) and Y second(s)'. - Provides a clean and formatted output, ensuring clarity in time representation. This utility is useful for improving the readability of elapsed time in logging and performance measurement contexts. --- .../decorators/execution_tracking.py | 8 ++++---- django_logging/utils/time.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 django_logging/utils/time.py diff --git a/django_logging/decorators/execution_tracking.py b/django_logging/decorators/execution_tracking.py index b7dbff2..e1a9e10 100644 --- a/django_logging/decorators/execution_tracking.py +++ b/django_logging/decorators/execution_tracking.py @@ -2,11 +2,12 @@ import os import time from functools import wraps -from typing import Callable, Optional +from typing import Any, Callable, Optional from django.conf import settings from django.db import connection +from django_logging.utils.time import format_elapsed_time from django_logging.validators.config_validators import ( validate_boolean_setting, validate_integer_setting, @@ -69,7 +70,7 @@ def execution_tracker( def decorator(func: Callable) -> Callable: @wraps(func) - def wrapper(*args, **kwargs): + def wrapper(*args: Any, **kwargs: Any) -> Any: start_time = time.time() # Check if DEBUG is True and log_queries is enabled; if not, ignore query tracking @@ -82,7 +83,6 @@ def wrapper(*args, **kwargs): # Calculate execution time elapsed_time = time.time() - start_time - minutes, seconds = divmod(elapsed_time, 60) # Get detailed function information module_name = func.__module__ @@ -90,7 +90,7 @@ def wrapper(*args, **kwargs): file_path = os.path.abspath(func.__code__.co_filename) line_number = func.__code__.co_firstlineno - time_message = f"{minutes} minute(s) and {seconds:.4f} second(s)" + time_message = format_elapsed_time(elapsed_time) log_message = ( f"Performance Metrics for Function: '{function_name}'\n" f" Module: {module_name}\n" diff --git a/django_logging/utils/time.py b/django_logging/utils/time.py new file mode 100644 index 0000000..b15ef18 --- /dev/null +++ b/django_logging/utils/time.py @@ -0,0 +1,18 @@ +def format_elapsed_time(elapsed_time: float) -> str: + """Formats the elapsed time into a human-readable string. + + If the time is less than a minute, returns only seconds. Otherwise, + returns the time in minutes and seconds. + + Args: + elapsed_time: Time in seconds as a float. + + Returns: + A string representing the formatted time. + + """ + minutes, seconds = divmod(elapsed_time, 60) + + if minutes > 0: + return f"{int(minutes)} minute(s) and {seconds:.2f} second(s)" + return f"{seconds:.2f} second(s)"