comparison MoinMoin/stats/languages.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 6d9bdb12163f
children a1322262398a
comparison
equal deleted inserted replaced
3063:726f96c0f12e 3064:af5872a8f022
46 fmt = request.formatter 46 fmt = request.formatter
47 _ = request.getText 47 _ = request.getText
48 48
49 data = get_data(request) 49 data = get_data(request)
50 50
51 sum = 0.0 51 total = 0.0
52 for cnt, lang in data: 52 for cnt, lang in data:
53 sum += cnt 53 total += cnt
54 54
55 55
56 languages = TupleDataset() 56 languages = TupleDataset()
57 languages.columns = [Column('language', label=_("Language", formatted=False), align='left'), 57 languages.columns = [Column('language', label=_("Language", formatted=False), align='left'),
58 Column('value', label='%', align='right')] 58 Column('value', label='%', align='right')]
61 data = data[:10] 61 data = data[:10]
62 62
63 # Preparing "<Browser setting>" 63 # Preparing "<Browser setting>"
64 browserlang = _('<Browser setting>', formatted=False) 64 browserlang = _('<Browser setting>', formatted=False)
65 browserlang = browserlang[1:len(browserlang) - 1].capitalize() 65 browserlang = browserlang[1:len(browserlang) - 1].capitalize()
66 if sum <> 0: 66 if total:
67 for cnt, lang in data: 67 for cnt, lang in data:
68 try: 68 try:
69 if lang == u'browser': 69 if lang == u'browser':
70 languages.addRow((browserlang, "%(percent).2f%% (%(count)d)" % { 70 languages.addRow((browserlang, "%(percent).2f%% (%(count)d)" % {
71 'percent': 100.0 * cnt / sum, 71 'percent': 100.0 * cnt / total,
72 'count': cnt})) 72 'count': cnt}))
73 else: 73 else:
74 lang = i18n.wikiLanguages()[lang]['x-language-in-english'] 74 lang = i18n.wikiLanguages()[lang]['x-language-in-english']
75 languages.addRow((lang, "%(percent).2f%% (%(count)d)" % { 75 languages.addRow((lang, "%(percent).2f%% (%(count)d)" % {
76 'percent': 100.0 * cnt / sum, 76 'percent': 100.0 * cnt / total,
77 'count': cnt})) 77 'count': cnt}))
78 cnt_printed += cnt 78 cnt_printed += cnt
79 except UnicodeError: 79 except UnicodeError:
80 pass 80 pass
81 if total > cnt_printed:
82 languages.addRow((_('Others', formatted=False), "%(percent).2f%% (%(count)d)" % {
83 'percent': 100.0 * (total - cnt_printed) / total,
84 'count': total - cnt_printed}))
85
81 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" 86 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"
82 languages.addRow((browserlang, "100% (1)")) 87 languages.addRow((browserlang, "100% (1)"))
83
84 if sum > cnt_printed:
85 languages.addRow((_('Others', formatted=False), "%(percent).2f%% (%(count)d)" % {
86 'percent': 100.0 * (sum - cnt_printed) / sum,
87 'count': sum - cnt_printed}))
88 88
89 table = DataBrowserWidget(request) 89 table = DataBrowserWidget(request)
90 table.setData(languages) 90 table.setData(languages)
91 return table.toHTML() 91 return table.toHTML()
92