Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.stats.useragents Namespace Reference

Functions

def linkto
def get_data
def text
def draw

Variables

int _debug = 0

Function Documentation

def MoinMoin.stats.useragents.draw (   pagename,
  request 
)

Definition at line 121 of file useragents.py.

00121 
00122 def draw(pagename, request):
00123     import shutil, cStringIO
00124     from MoinMoin.stats.chart import Chart, ChartData, Color
00125 
00126     _ = request.getText
00127 
00128     style = Chart.GDC_3DPIE
00129 
00130     # get data
00131     colors = ['red', 'mediumblue', 'yellow', 'deeppink', 'aquamarine', 'purple', 'beige',
00132               'blue', 'forestgreen', 'orange', 'cyan', 'fuchsia', 'lime']
00133     colors = ([Color(c) for c in colors])
00134 
00135     data = get_data(request)
00136 
00137     maxdata = len(colors) - 1
00138     if len(data) > maxdata:
00139         others = [x[0] for x in data[maxdata:]]
00140         data = data[:maxdata] + [(sum(others), _('Others').encode('iso-8859-1', 'replace'))] # gdchart can't do utf-8
00141 
00142     # shift front to end if others is very small
00143     if data[-1][0] * 10 < data[0][0]:
00144         data = data[1:] + data[0:1]
00145 
00146     labels = [x[1] for x in data]
00147     data = [x[0] for x in data]
00148 
00149     # give us a chance to develop this
00150     if _debug:
00151         return "<p>data = %s</p>" % \
00152             '<br>'.join([wikiutil.escape(repr(x)) for x in [labels, data]])
00153 
00154     # create image
00155     image = cStringIO.StringIO()
00156     c = Chart()
00157     c.addData(data)
00158 
00159     title = ''
00160     if request.cfg.sitename: title = "%s: " % request.cfg.sitename
00161     title = title + _('Distribution of User-Agent Types')
00162     c.option(
00163         pie_color=colors,
00164         label_font=Chart.GDC_SMALL,
00165         label_line=1,
00166         label_dist=20,
00167         threed_depth=20,
00168         threed_angle=225,
00169         percent_labels=Chart.GDCPIE_PCT_RIGHT,
00170         title_font=c.GDC_GIANT,
00171         title=title.encode('iso-8859-1', 'replace')) # gdchart can't do utf-8
00172     labels = [label.encode('iso-8859-1', 'replace') for label in labels]
00173     c.draw(style,
00174         (request.cfg.chart_options['width'], request.cfg.chart_options['height']),
00175         image, labels)
00176 
00177     request.content_type = 'image/gif'
00178     request.content_length = len(image.getvalue())
00179 
00180     # copy the image
00181     image.reset()
00182     shutil.copyfileobj(image, request, 8192)
00183 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 47 of file useragents.py.

00047 
00048 def get_data(request):
00049     # get results from cache
00050     cache = caching.CacheEntry(request, 'charts', 'useragents', scope='wiki', use_pickle=True)
00051     cache_date, data = 0, {}
00052     if cache.exists():
00053         try:
00054             cache_date, data = cache.content()
00055         except:
00056             cache.remove() # cache gone bad
00057 
00058     log = eventlog.EventLog(request)
00059     try:
00060         new_date = log.date()
00061     except logfile.LogMissing:
00062         new_date = None
00063 
00064     if new_date is not None:
00065         log.set_filter(['VIEWPAGE', 'SAVEPAGE'])
00066         for event in log.reverse():
00067             if event[0] <= cache_date:
00068                 break
00069             ua = event[2].get('HTTP_USER_AGENT')
00070             if ua:
00071                 try:
00072                     pos = ua.index(" (compatible; ")
00073                     ua = ua[pos:].split(';')[1].strip()
00074                 except ValueError:
00075                     ua = ua.split()[0]
00076                 #ua = ua.replace(';', '\n')
00077                 data[ua] = data.get(ua, 0) + 1
00078 
00079         # write results to cache
00080         cache.update((new_date, data))
00081 
00082     data = [(cnt, ua) for ua, cnt in data.items()]
00083     data.sort()
00084     data.reverse()
00085     return data

Here is the caller graph for this function:

def MoinMoin.stats.useragents.linkto (   pagename,
  request,
  params = '' 
)

Definition at line 22 of file useragents.py.

00022 
00023 def linkto(pagename, request, params=''):
00024     _ = request.getText
00025 
00026     if not request.cfg.chart_options:
00027         return text(pagename, request)
00028     if _debug:
00029         return draw(pagename, request)
00030 
00031     page = Page(request, pagename)
00032 
00033     # Create escaped query string from dict and params
00034     querystr = {'action': 'chart', 'type': 'useragents'}
00035     querystr = wikiutil.makeQueryString(querystr)
00036     querystr = wikiutil.escape(querystr)
00037     if params:
00038         querystr += '&amp;' + params
00039 
00040     data = {'url': page.url(request, querystr)}
00041     data.update(request.cfg.chart_options)
00042     result = ('<img src="%(url)s" width="%(width)d" height="%(height)d"'
00043               ' alt="useragents chart">') % data
00044 
00045     return result
00046 

Here is the call graph for this function:

def MoinMoin.stats.useragents.text (   pagename,
  request 
)

Definition at line 86 of file useragents.py.

00086 
00087 def text(pagename, request):
00088     from MoinMoin.util.dataset import TupleDataset, Column
00089     from MoinMoin.widget.browser import DataBrowserWidget
00090 
00091     _ = request.getText
00092 
00093     data = get_data(request)
00094 
00095     total = 0.0
00096     for cnt, ua in data:
00097         total += cnt
00098 
00099 
00100     agents = TupleDataset()
00101     agents.columns = [Column('agent', label=_("User agent"), align='left'),
00102                       Column('value', label='%', align='right')]
00103 
00104     cnt_printed = 0
00105     data = data[:10]
00106 
00107     if total:
00108         for cnt, ua in data:
00109             try:
00110                 ua = unicode(ua)
00111                 agents.addRow((ua, "%.2f" % (100.0 * cnt / total)))
00112                 cnt_printed += cnt
00113             except UnicodeError:
00114                 pass
00115         if total > cnt_printed:
00116             agents.addRow((_('Others'), "%.2f" % (100 * (total - cnt_printed) / total)))
00117 
00118     table = DataBrowserWidget(request)
00119     table.setData(agents)
00120     return table.render(method="GET")

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 15 of file useragents.py.