diff MoinMoin/stats/useragents.py @ 3064:af5872a8f022

useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 21 Feb 2008 21:28:02 +0100
parents 2620aa4811a4
children a1322262398a
line wrap: on
line diff
--- a/MoinMoin/stats/useragents.py	Thu Feb 21 21:14:57 2008 +0100
+++ b/MoinMoin/stats/useragents.py	Thu Feb 21 21:28:02 2008 +0100
@@ -92,9 +92,9 @@
 
     data = get_data(request)
 
-    sum = 0.0
+    total = 0.0
     for cnt, ua in data:
-        sum += cnt
+        total += cnt
 
 
     agents = TupleDataset()
@@ -103,20 +103,22 @@
 
     cnt_printed = 0
     data = data[:10]
-    for cnt, ua in data:
-        try:
-            ua = unicode(ua)
-            agents.addRow((ua, "%.2f" % (100.0*cnt/sum)))
-            cnt_printed += cnt
-        except UnicodeError:
-            pass
-    agents.addRow((_('Others'), "%.2f" % (100*(sum-cnt_printed)/sum)))
+
+    if total:
+        for cnt, ua in data:
+            try:
+                ua = unicode(ua)
+                agents.addRow((ua, "%.2f" % (100.0 * cnt / total)))
+                cnt_printed += cnt
+            except UnicodeError:
+                pass
+        if total > cnt_printed:
+            agents.addRow((_('Others', formatted=False), "%.2f" % (100 * (total - cnt_printed) / total)))
 
     table = DataBrowserWidget(request)
     table.setData(agents)
     return table.toHTML()
 
-
 def draw(pagename, request):
     import shutil, cStringIO
     from MoinMoin.stats.chart import Chart, ChartData, Color