Skip to content

Commit 0d27299

Browse files
committed
Support for +∞ and -100% in change columns
This fixes tobami#170.
1 parent 0f16149 commit 0d27299

4 files changed

Lines changed: 64 additions & 27 deletions

File tree

codespeed/models.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -267,41 +267,45 @@ def save(self, *args, **kwargs):
267267
# Save summary in order of priority
268268
# Average change
269269
if average_change_color != "none":
270-
#Substitute plus/minus with up/down
271-
direction = average_change >= 0 and "+" or "-"
272-
self.summary = "Average %s %s%.1f%%" % (
270+
self.summary = "Average %s %s" % (
273271
average_change_units.lower(),
274-
direction,
275-
round(abs(average_change), 1))
272+
self.updown(average_change))
276273
self.colorcode = average_change_color
277274
# Single benchmark change
278275
if max_change_color != "none" and self.colorcode != "red":
279-
#Substitute plus/minus with up/down
280-
direction = max_change >= 0 and "+" or "-"
281-
self.summary = "%s %s%.1f%%" % (
282-
max_change_ben, direction, round(abs(max_change), 1))
276+
self.summary = "%s %s" % (
277+
max_change_ben,
278+
self.updown(max_change))
283279
self.colorcode = max_change_color
284280

285281
# Average trend
286282
if average_trend_color != "none" and self.colorcode == "none":
287-
#Substitute plus/minus with up/down
288-
direction = average_trend >= 0 and "+" or ""
289-
self.summary = "Average %s trend %s%.1f%%" % (
290-
average_trend_units.lower(), direction, round(average_trend, 1))
283+
self.summary = "Average %s trend %s" % (
284+
average_trend_units.lower(),
285+
self.updown(average_trend))
291286
self.colorcode = average_trend_color == "red"\
292287
and "yellow" or average_trend_color
293288
# Single benchmark trend
294289
if max_trend_color != "none" and self.colorcode != "red":
295290
if (self.colorcode == "none" or
296291
(self.colorcode == "green" and "trend" not in self.summary)):
297-
direction = max_trend >= 0 and "+" or ""
298-
self.summary = "%s trend %s%.1f%%" % (
299-
max_trend_ben, direction, round(max_trend, 1))
292+
self.summary = "%s trend %s" % (
293+
max_trend_ben,
294+
self.updown(max_trend))
300295
self.colorcode = max_trend_color == "red"\
301296
and "yellow" or max_trend_color
302297

303298
super(Report, self).save(*args, **kwargs)
304299

300+
def updown(self,val):
301+
#Substitute plus/minus with up/down
302+
direction = val >= 0 and "up" or "down"
303+
aval = abs(val)
304+
if aval == float("inf"):
305+
return u"%s ∞%%" % direction
306+
else:
307+
return "%s %.1f%%" % (direction, aval)
308+
305309
def is_big_change(self, val, color, current_val, current_color):
306310
if color == "red" and current_color != "red":
307311
return True
@@ -316,12 +320,12 @@ def is_big_change(self, val, color, current_val, current_color):
316320
def getcolorcode(self, val, lessisbetter, threshold):
317321
if lessisbetter:
318322
val = -val
319-
colorcode = "none"
320323
if val < -threshold:
321-
colorcode = "red"
324+
return "red"
322325
elif val > threshold:
323-
colorcode = "green"
324-
return colorcode
326+
return "green"
327+
else:
328+
return "none"
325329

326330
def get_changes_table(self, trend_depth=10, force_save=False):
327331
# Determine whether required trend value is the default one
@@ -406,9 +410,16 @@ def get_changes_table(self, trend_depth=10, force_save=False):
406410
change = "-"
407411
if len(change_list):
408412
c = change_list.filter(benchmark=bench)
409-
if c.count() and c[0].value and result:
410-
change = (result - c[0].value) * 100 / c[0].value
411-
totals['change'].append(result / c[0].value)
413+
if c.count() and result is not None:
414+
if c[0].value != 0:
415+
change = (result - c[0].value) * 100 / c[0].value
416+
totals['change'].append(result / c[0].value)
417+
elif c[0].value == 0:
418+
change = float("inf")
419+
totals['change'].append(float("inf"))
420+
else:
421+
# no previous result, no change available
422+
pass
412423

413424
# Calculate trend:
414425
# percentage change relative to average of 3 previous results

codespeed/templates/codespeed/changes_table.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% load percentages %}
2+
13
<div class="leftcolumn">
24
{% for units in tablelist %}
35
<table class="tablesorter" data-lessisbetter="{{ units.lessisbetter }}">
@@ -13,25 +15,25 @@
1315
</tr>
1416
</thead>
1517
<tfoot>
16-
<tr data-change="{{ units.totals.change }}" data-trend="{{ units.totals.trend }}">
18+
<tr data-change="{{ units.totals.change|fix_infinity }}" data-trend="{{ units.totals.trend|fix_infinity }}">
1719
<td title="Arithmetic mean">Average</td>
1820
<td></td>
1921
{% if units.hasmin %}<td></td>
2022
{% endif%}{% if units.has_stddev %}<td>{{ units.totals.min }}</td>
2123
{% endif%}{% if units.hasmax %}<td>{{ units.totals.max }}</td>
22-
{% endif%}<td>{% ifnotequal units.totals.change "-" %}{{ units.totals.change|floatformat:2 }}%{% else %}{{ units.totals.change }}{% endifnotequal %}</td>
24+
{% endif%}<td>{{ units.totals.change|percentage }}</td>
2325
<td>{% ifnotequal units.totals.trend "-" %}{{ units.totals.trend|floatformat:2 }}%{% else %}{{ units.totals.trend }}{% endifnotequal %}</td>
2426
</tr>
2527
</tfoot>
2628
<tbody>
2729
{% for row in units.rows|dictsort:"bench_name" %}
28-
<tr data-change="{{ row.change }}" data-trend="{{ row.trend }}">
30+
<tr data-change="{{ row.change|fix_infinity }}" data-trend="{{ row.trend|fix_infinity }}">
2931
<td title="{{ row.bench_description }}">{{ row.bench_name }}</td>
3032
<td>{{ row.result|floatformat:units.precission }}</td>
3133
{% if units.has_stddev %}<td>{{ row.std_dev|floatformat:units.precission }}</td>
3234
{% endif%}{% if units.hasmin %}<td>{{ row.val_min|floatformat:units.precission }}</td>
3335
{% endif%}{% if units.hasmax %}<td>{{ row.val_max|floatformat:units.precission }}</td>
34-
{% endif%}<td>{% ifequal row.change "-" %}-{% else %}{{ row.change|floatformat:2 }}%{% endifequal %}</td>
36+
{% endif%}<td>{{ row.change|percentage }}</td>
3537
<td>{% ifequal row.trend "-" %}-{% else %}{{ row.trend|floatformat:2 }}%{% endifequal %}</td>
3638
</tr>{% endfor %}
3739
</tbody>

codespeed/templatetags/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from django import template
4+
5+
register = template.Library()
6+
7+
@register.filter
8+
def percentage(value):
9+
if value == "-":
10+
return "-"
11+
elif value == float("inf"):
12+
return "+∞%"
13+
else:
14+
return "%.2f" % value
15+
16+
@register.filter
17+
def fix_infinity(value):
18+
'''Python’s ∞ prints "inf", but JavaScript wants "Infinity"'''
19+
if value == float("inf"):
20+
return "Infinity"
21+
elif value == float("-inf"):
22+
return "-Infinity"
23+
else:
24+
return value

0 commit comments

Comments
 (0)