iwla

iwla Git Source Tree

Root/plugins/display/top_visitors.py

1import time
2
3from iwla import IWLA
4from iplugin import IPlugin
5from display import *
6
7#
8# Display hook
9#
10# Create TOP visitors block
11#
12# Plugin requirements :
13# None
14#
15# Conf values needed :
16# display_visitor_ip*
17#
18# Output files :
19# OUTPUT_ROOT/year/month/index.html
20#
21# Statistics creation :
22# None
23#
24# Statistics update :
25# None
26#
27# Statistics deletion :
28# None
29#
30
31class IWLADisplayTopVisitors(IPlugin):
32 def __init__(self, iwla):
33 super(IWLADisplayTopVisitors, self).__init__(iwla)
34 self.API_VERSION = 1
35
36 def hook(self):
37 hits = self.iwla.getValidVisitors()
38 display = self.iwla.getDisplay()
39 display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
40
41 total = [0]*5
42 for super_hit in hits.values():
43 total[1] += super_hit['viewed_pages']
44 total[2] += super_hit['viewed_hits']
45 total[3] += super_hit['bandwidth']
46
47 top_bandwidth = [(k,v['bandwidth']) for (k,v) in hits.items()]
48 top_bandwidth = sorted(top_bandwidth, key=lambda t: t[1], reverse=True)
49 top_visitors = [hits[h[0]] for h in top_bandwidth[:10]]
50
51 index = self.iwla.getDisplayIndex()
52 table = display.createBlock(DisplayHTMLBlockTable, 'Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen'])
53 table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
54 for super_hit in top_visitors:
55 address = super_hit['remote_addr']
56 if display_visitor_ip and\
57 super_hit.get('dns_name_replaced', False):
58 address = '%s [%s]' % (address, super_hit['remote_ip'])
59
60 row = [
61 address,
62 super_hit['viewed_pages'],
63 super_hit['viewed_hits'],
64 bytesToStr(super_hit['bandwidth']),
65 time.asctime(super_hit['last_access'])
66 ]
67 total[1] -= super_hit['viewed_pages']
68 total[2] -= super_hit['viewed_hits']
69 total[3] -= super_hit['bandwidth']
70 table.appendRow(row)
71 if total[1] or total[2] or total[3]:
72 total[0] = 'Others'
73 total[3] = bytesToStr(total[3])
74 total[4] = ''
75 table.appendRow(total)
76 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
77 index.appendBlock(table)

Archive Download this file

Branches

Tags