Improve stat deltas, division by zero

This commit is contained in:
R. Miles McCain 2020-04-22 12:46:26 -04:00
parent 937a545b23
commit 1762dc5035
No known key found for this signature in database
GPG Key ID: 91CB47BDDF2671A5
3 changed files with 17 additions and 15 deletions

View File

@ -29,10 +29,10 @@
<p class="label">
{% if stats.bounce_rate_pct != None %}
{{stats.bounce_rate_pct|floatformat:"-1"}}%
{% compare stats.compare.bounce_rate_pct stats.bounce_rate_pct "DOWN" %}
{% else %}
?
{% endif %}
{% compare stats.compare.bounce_rate_pct stats.bounce_rate_pct "DOWN" %}
</p>
</div>
<div>
@ -40,10 +40,10 @@
<p class="label">
{% if stats.avg_session_duration != None %}
{{stats.avg_session_duration|naturaldelta}}
{% compare stats.compare.avg_session_duration stats.avg_session_duration "UP" %}
{% else %}
?
{% endif %}
{% compare stats.compare.avg_session_duration stats.avg_session_duration "UP" %}
</p>
</div>
</div>

View File

@ -36,12 +36,12 @@
<p class="heading">
{% if stats.avg_load_time %}
{{stats.avg_load_time|floatformat:"0"}}ms
<div>
{% compare stats.compare.avg_load_time stats.avg_load_time "DOWN" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
{% else %}
?
{% endif %}
<div>
{% compare stats.compare.avg_load_time stats.avg_load_time "DOWN" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
</p>
</article>
<article class="">
@ -49,12 +49,12 @@
<p class="heading">
{% if stats.bounce_rate_pct %}
{{stats.bounce_rate_pct|floatformat:"-1"}}%
<div>
{% compare stats.compare.bounce_rate_pct stats.bounce_rate_pct "DOWN" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
{% else %}
?
{% endif %}
<div>
{% compare stats.compare.bounce_rate_pct stats.bounce_rate_pct "DOWN" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
</p>
</article>
<article class="">
@ -62,12 +62,12 @@
<p class="heading">
{% if stats.avg_session_duration %}
{{stats.avg_session_duration|naturaldelta}}
<div>
{% compare stats.compare.avg_session_duration stats.avg_session_duration "UP" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
{% else %}
?
{% endif %}
<div>
{% compare stats.compare.avg_session_duration stats.avg_session_duration "UP" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
</p>
</article>
<article class="">
@ -75,12 +75,12 @@
<p class="heading">
{% if stats.avg_hits_per_session %}
{{stats.avg_hits_per_session|floatformat:"-1"}}
<div>
{% compare stats.compare.avg_hits_per_session stats.avg_hits_per_session "UP" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
{% else %}
?
{% endif %}
<div>
{% compare stats.compare.avg_hits_per_session stats.avg_hits_per_session "UP" classes=classes good_classes=good_classes bad_classes=bad_classes neutral_classes=neutral_classes %}
</div>
</p>
</article>
{% endwith %}

View File

@ -67,12 +67,14 @@ def percent_change_display(start, end):
if start == None or end == None:
return SafeString("&Delta; n/a")
if start == end:
direction = ""
direction = "&Delta; "
else:
direction = "&uarr; " if end > start else "&darr; "
if start == 0 and end != 0:
pct_change = "100%"
elif start == 0:
pct_change = "0%"
else:
change = int(round(100 * abs(end - start) / start))
if change > 999: