Refactor format_elapsed_time function to improve readability and add comments
This commit is contained in:
parent
f7e5eb8133
commit
d9abe93c42
1 changed files with 17 additions and 13 deletions
|
|
@ -171,19 +171,23 @@ async def check_langflow_version(component: StoreComponentCreate):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def format_elapsed_time(elapsed_time) -> str:
|
def format_elapsed_time(elapsed_time: float) -> str:
|
||||||
# Format elapsed time to human readable format coming from
|
"""Format elapsed time to a human-readable format coming from perf_counter().
|
||||||
# perf_counter()
|
|
||||||
# If the elapsed time is less than 1 second, return ms
|
- Less than 1 second: returns milliseconds
|
||||||
# If the elapsed time is less than 1 minute, return seconds rounded to 2 decimals
|
- Less than 1 minute: returns seconds rounded to 2 decimals
|
||||||
time_str = ""
|
- 1 minute or more: returns minutes and seconds
|
||||||
|
"""
|
||||||
if elapsed_time < 1:
|
if elapsed_time < 1:
|
||||||
elapsed_time = int(round(elapsed_time * 1000))
|
milliseconds = int(round(elapsed_time * 1000))
|
||||||
time_str = f"{elapsed_time} ms"
|
return f"{milliseconds} ms"
|
||||||
elif elapsed_time < 60:
|
elif elapsed_time < 60:
|
||||||
elapsed_time = round(elapsed_time, 2)
|
seconds = round(elapsed_time, 2)
|
||||||
time_str = f"{elapsed_time} seconds"
|
unit = "second" if seconds == 1 else "seconds"
|
||||||
|
return f"{seconds} {unit}"
|
||||||
else:
|
else:
|
||||||
elapsed_time = round(elapsed_time / 60, 2)
|
minutes = int(elapsed_time // 60)
|
||||||
time_str = f"{elapsed_time} minutes"
|
seconds = round(elapsed_time % 60, 2)
|
||||||
return time_str
|
minutes_unit = "minute" if minutes == 1 else "minutes"
|
||||||
|
seconds_unit = "second" if seconds == 1 else "seconds"
|
||||||
|
return f"{minutes} {minutes_unit}, {seconds} {seconds_unit}"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue