[Moin-devel] CVS: MoinMoin/stats useragents.py,NONE,1.1
J?rgen Hermann
jhermann at users.sourceforge.net
Fri Feb 8 16:44:03 EST 2002
Update of /cvsroot/moin/MoinMoin/stats
In directory usw-pr-cvs1:/tmp/cvs-serv12374/MoinMoin/stats
Added Files:
useragents.py
Log Message:
User-Agent chart
--- NEW FILE: useragents.py ---
"""
MoinMoin - User-Agent Statistics
Copyright (c) 2002 by Jürgen Hermann <jh at web.de>
All rights reserved, see COPYING for details.
This macro creates a pie chart of the type of user agents
accessing the wiki.
$Id: useragents.py,v 1.1 2002/02/09 00:42:52 jhermann Exp $
"""
_debug = 0
import string
from MoinMoin import config, wikiutil
from MoinMoin.Page import Page
from MoinMoin.i18n import _
def linkto(pagename, params=''):
if not config.chart_options:
return _('<div class="message"><b>Charts are not available!</b></div>')
if _debug:
return draw(pagename, {})
page = Page(pagename)
result = []
data = {
'url': page.url("action=chart&type=useragents"),
}
data.update(config.chart_options)
result.append('<img src="%(url)s" border="0" '
'width="%(width)d" height="%(height)d">' % data)
return string.join(result, '')
def draw(pagename, form):
import cgi, sys, shutil, cStringIO, operator
from MoinMoin import config, webapi, eventlog
from MoinMoin.stats.chart import Chart, ChartData, Color
style = Chart.GDC_3DPIE
# get data
colors = ['red', 'mediumblue', 'yellow', 'deeppink', 'aquamarine', 'purple', 'beige',
'blue', 'forestgreen', 'orange', 'cyan', 'fuchsia', 'lime']
colors = ([Color(c) for c in colors])
data = {}
logdata = eventlog.logger.read(['VIEWPAGE', 'SAVEPAGE'])
for event in logdata:
ua = event[2].get('HTTP_USER_AGENT')
if ua:
pos = ua.find(" (compatible; ")
if pos >= 0: ua = ua[pos:].split(';')[1].strip()
else: ua = ua.split()[0]
#ua = ua.replace(';', '\n')
data[ua] = data.get(ua, 0) + 1
data = [(cnt, ua) for ua, cnt in data.items()]
data.sort()
data.reverse()
maxdata = len(colors) - 1
if len(data) > maxdata:
others = [x[0] for x in data[maxdata:]]
data = data[:maxdata] + [(reduce(operator.add, others, 0), _('Others'))]
labels = [x[1] for x in data]
data = [x[0] for x in data]
# give us a chance to develop this
if _debug:
return "<p>data = %s</p>" % \
string.join(map(cgi.escape, map(repr, [labels, data])), '<br>')
# create image
image = cStringIO.StringIO()
c = Chart()
c.addData(data)
title = ''
if config.sitename: title = "%s: " % config.sitename
title = title + _('Distribution of User-Agent Types')
c.option(
pie_color = colors,
label_font = Chart.GDC_SMALL,
label_line = 1,
label_dist = 20,
threed_depth = 20,
threed_angle = 225,
percent_labels = Chart.GDCPIE_PCT_RIGHT,
title_font = c.GDC_GIANT,
title = title)
c.draw(style,
(config.chart_options['width'], config.chart_options['height']),
image, labels)
# send HTTP headers
headers = [
"Content-Type: image/gif",
"Content-Length: %d" % len(image.getvalue()),
]
webapi.http_headers(headers)
# copy the image
image.reset()
shutil.copyfileobj(image, sys.stdout, 8192)
sys.exit(0)
More information about the Moin-devel
mailing list