iwla/plugins/display/top_visitors.py

39 lines
1.5 KiB
Python
Raw Normal View History

import time
2014-11-24 17:13:59 +01:00
from iwla import IWLA
from iplugin import IPlugin
from display import *
2014-11-24 17:13:59 +01:00
class IWLADisplayTopVisitors(IPlugin):
2014-11-24 21:42:57 +01:00
def __init__(self, iwla):
super(IWLADisplayTopVisitors, self).__init__(iwla)
2014-11-24 17:13:59 +01:00
self.API_VERSION = 1
2014-11-22 19:23:56 +01:00
def hook(self):
hits = self.iwla.getValidVisitors()
count_hit_only = self.iwla.getConfValue('count_hit_only_visitors', False)
display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
top_bandwidth = [(k,v['bandwidth']) for (k,v) in hits.items() \
if count_hit_only or v['viewed_pages']]
top_bandwidth = sorted(top_bandwidth, key=lambda t: t[1], reverse=True)
top_visitors = [hits[h[0]] for h in top_bandwidth[:10]]
2014-11-22 19:23:56 +01:00
index = self.iwla.getDisplayIndex()
2014-11-24 17:13:59 +01:00
table = DisplayHTMLBlockTable('Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen'])
for super_hit in top_visitors:
2014-11-26 16:17:16 +01:00
address = super_hit['remote_addr']
if display_visitor_ip and\
2014-11-26 16:17:16 +01:00
super_hit.get('dns_name_replaced', False):
address = '%s [%s]' % (address, super_hit['remote_ip'])
2014-11-24 17:13:59 +01:00
row = [
2014-11-26 16:17:16 +01:00
address,
2014-11-24 17:13:59 +01:00
super_hit['viewed_pages'],
super_hit['viewed_hits'],
bytesToStr(super_hit['bandwidth']),
time.asctime(super_hit['last_access'])
]
table.appendRow(row)
index.appendBlock(table)