comparison 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
comparison
equal deleted inserted replaced
3063:726f96c0f12e 3064:af5872a8f022
90 fmt = request.formatter 90 fmt = request.formatter
91 _ = request.getText 91 _ = request.getText
92 92
93 data = get_data(request) 93 data = get_data(request)
94 94
95 sum = 0.0 95 total = 0.0
96 for cnt, ua in data: 96 for cnt, ua in data:
97 sum += cnt 97 total += cnt
98 98
99 99
100 agents = TupleDataset() 100 agents = TupleDataset()
101 agents.columns = [Column('agent', label=_("User agent"), align='left'), 101 agents.columns = [Column('agent', label=_("User agent"), align='left'),
102 Column('value', label='%', align='right')] 102 Column('value', label='%', align='right')]
103 103
104 cnt_printed = 0 104 cnt_printed = 0
105 data = data[:10] 105 data = data[:10]
106 for cnt, ua in data: 106
107 try: 107 if total:
108 ua = unicode(ua) 108 for cnt, ua in data:
109 agents.addRow((ua, "%.2f" % (100.0*cnt/sum))) 109 try:
110 cnt_printed += cnt 110 ua = unicode(ua)
111 except UnicodeError: 111 agents.addRow((ua, "%.2f" % (100.0 * cnt / total)))
112 pass 112 cnt_printed += cnt
113 agents.addRow((_('Others'), "%.2f" % (100*(sum-cnt_printed)/sum))) 113 except UnicodeError:
114 pass
115 if total > cnt_printed:
116 agents.addRow((_('Others', formatted=False), "%.2f" % (100 * (total - cnt_printed) / total)))
114 117
115 table = DataBrowserWidget(request) 118 table = DataBrowserWidget(request)
116 table.setData(agents) 119 table.setData(agents)
117 return table.toHTML() 120 return table.toHTML()
118
119 121
120 def draw(pagename, request): 122 def draw(pagename, request):
121 import shutil, cStringIO 123 import shutil, cStringIO
122 from MoinMoin.stats.chart import Chart, ChartData, Color 124 from MoinMoin.stats.chart import Chart, ChartData, Color
123 125