iwla/plugins/display/top_visitors.py

54 lines
2.0 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()
display = self.iwla.getDisplay()
display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
2014-12-05 16:03:09 +01:00
total = [0]*5
for super_hit in hits.values():
total[1] += super_hit['viewed_pages']
total[2] += super_hit['viewed_hits']
total[3] += super_hit['bandwidth']
top_bandwidth = [(k,v['bandwidth']) for (k,v) in hits.items()]
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()
table = display.createBlock(DisplayHTMLBlockTable, 'Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen'])
2014-11-28 16:02:04 +01:00
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
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'])
]
2014-12-05 16:03:09 +01:00
total[1] -= super_hit['viewed_pages']
total[2] -= super_hit['viewed_hits']
total[3] -= super_hit['bandwidth']
2014-11-24 17:13:59 +01:00
table.appendRow(row)
2014-12-05 16:03:09 +01:00
if total[1] or total[2] or total[3]:
total[0] = 'Others'
total[3] = bytesToStr(total[3])
total[4] = ''
table.appendRow(total)
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
2014-11-24 17:13:59 +01:00
index.appendBlock(table)