Back to index

moin  1.9.0~rc2
pagesize.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Pagesize Statistics
00004 
00005     This macro creates a bar graph of page size classes.
00006 
00007     @copyright: 2002-2004 Juergen Hermann <jh@web.de>
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 _debug = 0
00012 
00013 from MoinMoin import wikiutil
00014 from MoinMoin.Page import Page
00015 
00016 
00017 def linkto(pagename, request, params=''):
00018     _ = request.getText
00019 
00020     if not request.cfg.chart_options:
00021         return (request.formatter.sysmsg(1) +
00022                 request.formatter.text(_('Charts are not available!')) +
00023                 request.formatter.sysmsg(0))
00024 
00025     if _debug:
00026         return draw(pagename, request)
00027 
00028     page = Page(request, pagename)
00029 
00030     # Create escaped query string from dict and params
00031     querystr = {'action': 'chart', 'type': 'pagesize'}
00032     querystr = wikiutil.makeQueryString(querystr)
00033     querystr = wikiutil.escape(querystr)
00034     if params:
00035         querystr += '&amp;' + params
00036 
00037     data = {'url': page.url(request, querystr)}
00038     data.update(request.cfg.chart_options)
00039     result = ('<img src="%(url)s" width="%(width)d" height="%(height)d"'
00040               ' alt="pagesize chart">') % data
00041     return result
00042 
00043 
00044 def _slice(data, lo, hi):
00045     data = data[:]
00046     if lo: data[:lo] = [None] * lo
00047     if hi < len(data):
00048         data[hi:] = [None] * (len(data)-hi)
00049     return data
00050 
00051 
00052 def draw(pagename, request):
00053     import bisect, shutil, cStringIO
00054     from MoinMoin.stats.chart import Chart, ChartData, Color
00055 
00056     _ = request.getText
00057     style = Chart.GDC_3DBAR
00058 
00059     # get data
00060     pages = request.rootpage.getPageDict()
00061     sizes = []
00062     for name, page in pages.items():
00063         sizes.append((page.size(), name.encode('iso-8859-1', 'replace')) ) # gdchart does no utf-8
00064     sizes.sort()
00065 
00066     upper_bound = sizes[-1][0]
00067     bounds = [s*128 for s in range(1, 9)]
00068     if upper_bound >= 1024:
00069         bounds.extend([s*1024 for s in range(2, 9)])
00070     if upper_bound >= 8192:
00071         bounds.extend([s*8192 for s in range(2, 9)])
00072     if upper_bound >= 65536:
00073         bounds.extend([s*65536 for s in range(2, 9)])
00074 
00075     data = [None] * len(bounds)
00076     for size, name in sizes:
00077         idx = bisect.bisect(bounds, size)
00078         ##idx = int((size / upper_bound) * classes)
00079         data[idx] = (data[idx] or 0) + 1
00080 
00081     labels = ["%d" % b for b in bounds]
00082 
00083     # give us a chance to develop this
00084     if _debug:
00085         return "<p>data = %s</p>" % \
00086             '<br>'.join([wikiutil.escape(repr(x)) for x in [labels, data]])
00087 
00088     # create image
00089     image = cStringIO.StringIO()
00090     c = Chart()
00091     ##c.addData(ChartData(data, 'magenta'))
00092     c.addData(ChartData(_slice(data, 0, 7), 'blue'))
00093     if upper_bound >= 1024:
00094         c.addData(ChartData(_slice(data, 7, 14), 'green'))
00095     if upper_bound >= 8192:
00096         c.addData(ChartData(_slice(data, 14, 21), 'red'))
00097     if upper_bound >= 65536:
00098         c.addData(ChartData(_slice(data, 21, 28), 'magenta'))
00099     title = ''
00100     if request.cfg.sitename: title = "%s: " % request.cfg.sitename
00101     title = title + _('Page Size Distribution')
00102     c.option(
00103         annotation=(bisect.bisect(bounds, upper_bound), Color('black'), "%d %s" % sizes[-1]),
00104         title=title.encode('iso-8859-1', 'replace'), # gdchart can't do utf-8
00105         xtitle=_('page size upper bound [bytes]').encode('iso-8859-1', 'replace'),
00106         ytitle=_('# of pages of this size').encode('iso-8859-1', 'replace'),
00107         title_font=c.GDC_GIANT,
00108         threed_depth=2.0,
00109         requested_yinterval=1.0,
00110         stack_type=c.GDC_STACK_LAYER,
00111     )
00112     c.draw(style,
00113         (request.cfg.chart_options['width'], request.cfg.chart_options['height']),
00114         image, labels)
00115 
00116     request.content_type = 'image/gif'
00117     request.content_length = len(image.getvalue())
00118 
00119     # copy the image
00120     image.reset()
00121     shutil.copyfileobj(image, request, 8192)
00122