Fix duration change being unknown (fixes #89)

This commit is contained in:
R. Miles McCain 2021-01-23 22:40:19 -05:00
parent 2f778dc4b4
commit 6d7292a60a
No known key found for this signature in database
GPG Key ID: F1053629E2905557

View File

@ -1,3 +1,4 @@
from datetime import timedelta
from urllib.parse import urlparse from urllib.parse import urlparse
import flag import flag
@ -61,28 +62,25 @@ def relative_stat_tone(
@register.simple_tag @register.simple_tag
def percent_change_display(start, end): def percent_change_display(start, end):
try: if start == None or end == None:
if start == None or end == None: return SafeString("Δ n/a")
return SafeString("Δ n/a") if start == end:
if start == end: direction = "Δ "
direction = "Δ " else:
else: direction = "↑ " if end > start else "↓ "
direction = "↑ " if end > start else "↓ "
if start == 0 and end != 0: if start == 0 and end != 0:
pct_change = "100%" pct_change = "100%"
elif start == 0: elif start == 0:
pct_change = "0%" pct_change = "0%"
else:
change = int(round(100 * abs(end - start) / max(start, 1)))
if change > 999:
return "> 999%"
else: else:
change = int(round(100 * abs(end - start) / max(start, 1))) pct_change = str(change) + "%"
if change > 999:
return "> 999%"
else:
pct_change = str(change) + "%"
return SafeString(direction + pct_change) return SafeString(direction + pct_change)
except: # TODO: filter for specific issues
return SafeString("Δ ?")
@register.inclusion_tag("dashboard/includes/sidebar_footer.html") @register.inclusion_tag("dashboard/includes/sidebar_footer.html")
@ -100,6 +98,12 @@ def compare(
bad_classes=None, bad_classes=None,
neutral_classes=None, neutral_classes=None,
): ):
if isinstance(start, timedelta):
start = start.seconds
if isinstance(end, timedelta):
end = end.seconds
return { return {
"start": start, "start": start,
"end": end, "end": end,