From 6d7292a60aa2f513a49bae81efe1d90fe7b6ba89 Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Sat, 23 Jan 2021 22:40:19 -0500 Subject: [PATCH] Fix duration change being unknown (fixes #89) --- shynet/dashboard/templatetags/helpers.py | 42 +++++++++++++----------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/shynet/dashboard/templatetags/helpers.py b/shynet/dashboard/templatetags/helpers.py index ae1bf2d..042c1e9 100644 --- a/shynet/dashboard/templatetags/helpers.py +++ b/shynet/dashboard/templatetags/helpers.py @@ -1,3 +1,4 @@ +from datetime import timedelta from urllib.parse import urlparse import flag @@ -61,28 +62,25 @@ def relative_stat_tone( @register.simple_tag def percent_change_display(start, end): - try: - if start == None or end == None: - return SafeString("Δ n/a") - if start == end: - direction = "Δ " - else: - direction = "↑ " if end > start else "↓ " + if start == None or end == None: + return SafeString("Δ n/a") + if start == end: + direction = "Δ " + else: + direction = "↑ " if end > start else "↓ " - if start == 0 and end != 0: - pct_change = "100%" - elif start == 0: - pct_change = "0%" + if start == 0 and end != 0: + pct_change = "100%" + elif start == 0: + pct_change = "0%" + else: + change = int(round(100 * abs(end - start) / max(start, 1))) + if change > 999: + return "> 999%" else: - change = int(round(100 * abs(end - start) / max(start, 1))) - if change > 999: - return "> 999%" - else: - pct_change = str(change) + "%" + pct_change = str(change) + "%" - return SafeString(direction + pct_change) - except: # TODO: filter for specific issues - return SafeString("Δ ?") + return SafeString(direction + pct_change) @register.inclusion_tag("dashboard/includes/sidebar_footer.html") @@ -100,6 +98,12 @@ def compare( bad_classes=None, neutral_classes=None, ): + if isinstance(start, timedelta): + start = start.seconds + + if isinstance(end, timedelta): + end = end.seconds + return { "start": start, "end": end,