comparison MoinMoin/stats/pagesize.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children 55ff4feb0f59
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - Pagesize Statistics
4
5 This macro creates a bar graph of page size classes.
6
7 @copyright: 2002-2004 by Jürgen Hermann <jh@web.de>
8 @license: GNU GPL, see COPYING for details.
9 """
10
11 _debug = 0
12
13 from MoinMoin import wikiutil
14 from MoinMoin.Page import Page
15 from MoinMoin.util import MoinMoinNoFooter
16
17
18 def linkto(pagename, request, params=''):
19 from MoinMoin.util import web
20 _ = request.getText
21
22 if not request.cfg.chart_options:
23 return (request.formatter.sysmsg(1) +
24 request.formatter.text(_('Charts are not available!')) +
25 request.formatter.sysmsg(0))
26
27 if _debug:
28 return draw(pagename, request)
29
30 page = Page(request, pagename)
31
32 # Create escaped query string from dict and params
33 querystr = {'action': 'chart', 'type': 'pagesize'}
34 querystr = web.makeQueryString(querystr)
35 querystr = wikiutil.escape(querystr)
36 if params:
37 querystr += '&amp;' + params
38
39 # TODO: remove escape=0 in 1.4
40 data = {'url': page.url(request, querystr, escape=0)}
41 data.update(request.cfg.chart_options)
42 result = ('<img src="%(url)s" width="%(width)d" height="%(height)d"'
43 ' alt="pagesize chart">') % data
44 return result
45
46
47 def _slice(data, lo, hi):
48 data = data[:]
49 if lo: data[:lo] = [None] * lo
50 if hi < len(data): data[hi:] = [None] * (len(data)-hi)
51 return data
52
53
54 def draw(pagename, request):
55 import bisect, shutil, cStringIO
56 from MoinMoin.stats.chart import Chart, ChartData, Color
57
58 _ = request.getText
59 style = Chart.GDC_3DBAR
60
61 # get data
62 pages = request.rootpage.getPageDict()
63 sizes = []
64 for name, page in pages.items():
65 sizes.append( (page.size(), name.encode('iso-8859-1', 'replace')) ) # gdchart does no utf-8
66 sizes.sort()
67
68 upper_bound = sizes[-1][0]
69 bounds = [s*128 for s in range(1, 9)]
70 if upper_bound >= 1024:
71 bounds.extend([s*1024 for s in range(2, 9)])
72 if upper_bound >= 8192:
73 bounds.extend([s*8192 for s in range(2, 9)])
74 if upper_bound >= 65536:
75 bounds.extend([s*65536 for s in range(2, 9)])
76
77 data = [None] * len(bounds)
78 for size, name in sizes:
79 idx = bisect.bisect(bounds, size)
80 ##idx = int((size / upper_bound) * classes)
81 data[idx] = (data[idx] or 0) + 1
82
83 labels = ["%d" %b for b in bounds]
84
85 # give us a chance to develop this
86 if _debug:
87 return "<p>data = %s</p>" % \
88 '<br>'.join(map(wikiutil.escape, map(repr, [labels, data])))
89
90 # create image
91 image = cStringIO.StringIO()
92 c = Chart()
93 ##c.addData(ChartData(data, 'magenta'))
94 c.addData(ChartData(_slice(data, 0, 7), 'blue'))
95 if upper_bound >= 1024:
96 c.addData(ChartData(_slice(data, 7, 14), 'green'))
97 if upper_bound >= 8192:
98 c.addData(ChartData(_slice(data, 14, 21), 'red'))
99 if upper_bound >= 65536:
100 c.addData(ChartData(_slice(data, 21, 28), 'magenta'))
101 title = ''
102 if request.cfg.sitename: title = "%s: " % request.cfg.sitename
103 title = title + _('Page Size Distribution')
104 c.option(
105 annotation = (bisect.bisect(bounds, upper_bound), Color('black'), "%d %s" % sizes[-1]),
106 title = title.encode('iso-8859-1', 'replace'), # gdchart can't do utf-8
107 xtitle = _('page size upper bound [bytes]').encode('iso-8859-1', 'replace'),
108 ytitle = _('# of pages of this size').encode('iso-8859-1', 'replace'),
109 title_font = c.GDC_GIANT,
110 threed_depth = 2.0,
111 requested_yinterval = 1.0,
112 stack_type = c.GDC_STACK_LAYER
113 )
114 c.draw(style,
115 (request.cfg.chart_options['width'], request.cfg.chart_options['height']),
116 image, labels)
117
118 # send HTTP headers
119 headers = [
120 "Content-Type: image/gif",
121 "Content-Length: %d" % len(image.getvalue()),
122 ]
123 request.http_headers(headers)
124
125 # copy the image
126 image.reset()
127 shutil.copyfileobj(image, request, 8192)
128 raise MoinMoinNoFooter
129