Mercurial > moin > 1.9
changeset 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 | 726f96c0f12e |
children | 89f21a66d5cb |
files | MoinMoin/stats/languages.py MoinMoin/stats/useragents.py |
diffstat | 2 files changed, 24 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/MoinMoin/stats/languages.py Thu Feb 21 21:14:57 2008 +0100 +++ b/MoinMoin/stats/languages.py Thu Feb 21 21:28:02 2008 +0100 @@ -48,9 +48,9 @@ data = get_data(request) - sum = 0.0 + total = 0.0 for cnt, lang in data: - sum += cnt + total += cnt languages = TupleDataset() @@ -63,29 +63,30 @@ # Preparing "<Browser setting>" browserlang = _('<Browser setting>', formatted=False) browserlang = browserlang[1:len(browserlang) - 1].capitalize() - if sum <> 0: + if total: for cnt, lang in data: try: if lang == u'browser': languages.addRow((browserlang, "%(percent).2f%% (%(count)d)" % { - 'percent': 100.0 * cnt / sum, + 'percent': 100.0 * cnt / total, 'count': cnt})) else: lang = i18n.wikiLanguages()[lang]['x-language-in-english'] languages.addRow((lang, "%(percent).2f%% (%(count)d)" % { - 'percent': 100.0 * cnt / sum, + 'percent': 100.0 * cnt / total, 'count': cnt})) cnt_printed += cnt except UnicodeError: pass + if total > cnt_printed: + languages.addRow((_('Others', formatted=False), "%(percent).2f%% (%(count)d)" % { + 'percent': 100.0 * (total - cnt_printed) / total, + 'count': total - cnt_printed})) + else: # If we don't have any users, we can safely assume that the only real user is the visitor (who is normally ignored, though) who is using "Browser setting" languages.addRow((browserlang, "100% (1)")) - if sum > cnt_printed: - languages.addRow((_('Others', formatted=False), "%(percent).2f%% (%(count)d)" % { - 'percent': 100.0 * (sum - cnt_printed) / sum, - 'count': sum - cnt_printed})) - table = DataBrowserWidget(request) table.setData(languages) return table.toHTML() +
--- 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