annotate MoinMoin/stats/useragents.py @ 3243:1ad6243cdf06

macros: refactor for new arg parser
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 12 Mar 2008 00:23:45 +0100
parents 8a1f1a9dd37f
children 0248e23277ef 85884c67228d
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin - User-Agent Statistics
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 This macro creates a pie chart of the type of user agents
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 accessing the wiki.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
7
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8 TODO: should be refactored after hitcounts.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9
2970
2620aa4811a4 stats: hitcounts: use fixed date format for cache, use utc in any case, use pickle, useragents: use pickle (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
10 @copyright: 2002-2004 Juergen Hermann <jh@web.de>,
2620aa4811a4 stats: hitcounts: use fixed date format for cache, use utc in any case, use pickle, useragents: use pickle (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
11 2007 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
13 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
14
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15 _debug = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
16
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
17 from MoinMoin import wikiutil, caching, logfile
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
18 from MoinMoin.Page import Page
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 682
diff changeset
19 from MoinMoin.logfile import eventlog
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
20
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
21
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 def linkto(pagename, request, params=''):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
23 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 if not request.cfg.chart_options:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26 return text(pagename, request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
27 if _debug:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
28 return draw(pagename, request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
29
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
30 page = Page(request, pagename)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
31
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
32 # Create escaped query string from dict and params
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
33 querystr = {'action': 'chart', 'type': 'useragents'}
102
a54cebaab73e move make / parse query string functions to wikiutil, make unicode-safe
Thomas Waldmann <tw@waldmann-edv.de>
parents: 68
diff changeset
34 querystr = wikiutil.makeQueryString(querystr)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
35 querystr = wikiutil.escape(querystr)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
36 if params:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
37 querystr += '&amp;' + params
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
38
1816
4a31739bc6e1 Page.url(): removed escaping support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1419
diff changeset
39 data = {'url': page.url(request, querystr)}
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40 data.update(request.cfg.chart_options)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41 result = ('<img src="%(url)s" width="%(width)d" height="%(height)d"'
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42 ' alt="useragents chart">') % data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
43
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44 return result
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
47 def get_data(request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
48 # get results from cache
2970
2620aa4811a4 stats: hitcounts: use fixed date format for cache, use utc in any case, use pickle, useragents: use pickle (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
49 cache = caching.CacheEntry(request, 'charts', 'useragents', scope='wiki', use_pickle=True)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
50 cache_date, data = 0, {}
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
51 if cache.exists():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
52 try:
2970
2620aa4811a4 stats: hitcounts: use fixed date format for cache, use utc in any case, use pickle, useragents: use pickle (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
53 cache_date, data = cache.content()
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
54 except:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
55 cache.remove() # cache gone bad
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
56
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
57 log = eventlog.EventLog(request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
58 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
59 new_date = log.date()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60 except logfile.LogMissing:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
61 new_date = None
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
62
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63 if new_date is not None:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
64 log.set_filter(['VIEWPAGE', 'SAVEPAGE'])
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
65 for event in log.reverse():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
66 if event[0] <= cache_date:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
67 break
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
68 ua = event[2].get('HTTP_USER_AGENT')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
69 if ua:
1419
f7f415cc1332 replace most find() calls by in/index()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
70 try:
f7f415cc1332 replace most find() calls by in/index()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
71 pos = ua.index(" (compatible; ")
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
72 ua = ua[pos:].split(';')[1].strip()
1419
f7f415cc1332 replace most find() calls by in/index()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1068
diff changeset
73 except ValueError:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
74 ua = ua.split()[0]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
75 #ua = ua.replace(';', '\n')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
76 data[ua] = data.get(ua, 0) + 1
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
77
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
78 # write results to cache
2970
2620aa4811a4 stats: hitcounts: use fixed date format for cache, use utc in any case, use pickle, useragents: use pickle (port from 1.6)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
79 cache.update((new_date, data))
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
80
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
81 data = [(cnt, ua) for ua, cnt in data.items()]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
82 data.sort()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
83 data.reverse()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
84 return data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
85
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
86 def text(pagename, request):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
87 from MoinMoin.util.dataset import TupleDataset, Column
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
88 from MoinMoin.widget.browser import DataBrowserWidget
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
89
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
90 _ = request.getText
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
91
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
92 data = get_data(request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
93
3064
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
94 total = 0.0
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
95 for cnt, ua in data:
3064
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
96 total += cnt
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
97
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
98
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
99 agents = TupleDataset()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
100 agents.columns = [Column('agent', label=_("User agent"), align='left'),
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
101 Column('value', label='%', align='right')]
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
102
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
103 cnt_printed = 0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
104 data = data[:10]
3064
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
105
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
106 if total:
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
107 for cnt, ua in data:
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
108 try:
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
109 ua = unicode(ua)
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
110 agents.addRow((ua, "%.2f" % (100.0 * cnt / total)))
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
111 cnt_printed += cnt
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
112 except UnicodeError:
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
113 pass
af5872a8f022 useragent/language stats: fixed division by zero, do not use 'sum' (reserved) (ported from 1.6)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 2970
diff changeset
114 if total > cnt_printed:
3122
a1322262398a refactored _() getText calls to match new api
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3064
diff changeset
115 agents.addRow((_('Others'), "%.2f" % (100 * (total - cnt_printed) / total)))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
116
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
117 table = DataBrowserWidget(request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
118 table.setData(agents)
68
0bbc39b09f95 optimize backup by sorting, refactor some easy stuff to use redirectedOutput
Thomas Waldmann <tw@waldmann-edv.de>
parents: 33
diff changeset
119 return table.toHTML()
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
120
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
121 def draw(pagename, request):
1914
eb68960b1dfb remove unused imports
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
122 import shutil, cStringIO
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
123 from MoinMoin.stats.chart import Chart, ChartData, Color
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
124
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
125 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
126
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
127 style = Chart.GDC_3DPIE
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
128
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
129 # get data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
130 colors = ['red', 'mediumblue', 'yellow', 'deeppink', 'aquamarine', 'purple', 'beige',
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
131 'blue', 'forestgreen', 'orange', 'cyan', 'fuchsia', 'lime']
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
132 colors = ([Color(c) for c in colors])
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
133
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
134 data = get_data(request)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
135
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
136 maxdata = len(colors) - 1
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
137 if len(data) > maxdata:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
138 others = [x[0] for x in data[maxdata:]]
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1816
diff changeset
139 data = data[:maxdata] + [(sum(others), _('Others').encode('iso-8859-1', 'replace'))] # gdchart can't do utf-8
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
140
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
141 # shift front to end if others is very small
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
142 if data[-1][0] * 10 < data[0][0]:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
143 data = data[1:] + data[0:1]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
144
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
145 labels = [x[1] for x in data]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
146 data = [x[0] for x in data]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
147
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
148 # give us a chance to develop this
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
149 if _debug:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
150 return "<p>data = %s</p>" % \
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1816
diff changeset
151 '<br>'.join([wikiutil.escape(repr(x)) for x in [labels, data]])
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
152
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
153 # create image
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
154 image = cStringIO.StringIO()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
155 c = Chart()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
156 c.addData(data)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
157
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
158 title = ''
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
159 if request.cfg.sitename: title = "%s: " % request.cfg.sitename
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
160 title = title + _('Distribution of User-Agent Types')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
161 c.option(
995
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
162 pie_color=colors,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
163 label_font=Chart.GDC_SMALL,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
164 label_line=1,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
165 label_dist=20,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
166 threed_depth=20,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
167 threed_angle=225,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
168 percent_labels=Chart.GDCPIE_PCT_RIGHT,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
169 title_font=c.GDC_GIANT,
b6ae650aec74 stats package whitespace-only and style cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
170 title=title.encode('iso-8859-1', 'replace')) # gdchart can't do utf-8
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
171 labels = [label.encode('iso-8859-1', 'replace') for label in labels]
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
172 c.draw(style,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
173 (request.cfg.chart_options['width'], request.cfg.chart_options['height']),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
174 image, labels)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
175
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
176 headers = [
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
177 "Content-Type: image/gif",
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
178 "Content-Length: %d" % len(image.getvalue()),
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
179 ]
1068
ecece5db5288 use emit_http_headers
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 995
diff changeset
180 request.emit_http_headers(headers)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
181
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
182 # copy the image
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
183 image.reset()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
184 shutil.copyfileobj(image, request, 8192)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
185