From 6505ca3ee5d90fcad377ca89c1abc435cbc08095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Tue, 25 Nov 2014 16:59:29 +0100 Subject: [PATCH] Add all_visits plugin --- conf.py | 2 +- display.py | 14 +++++++++++- iwla.py | 3 +++ plugins/display/all_visits.py | 41 +++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 plugins/display/all_visits.py diff --git a/conf.py b/conf.py index e4c6378..574c47d 100644 --- a/conf.py +++ b/conf.py @@ -13,7 +13,7 @@ DISPLAY_ROOT = './output/' pre_analysis_hooks = ['page_to_hit', 'robots'] post_analysis_hooks = ['top_visitors', 'reverse_dns'] -display_hooks = ['top_visitors'] +display_hooks = ['top_visitors', 'all_visits'] reverse_dns_timeout = 0.2 page_to_hit_conf = [r'^.+/logo/$'] diff --git a/display.py b/display.py index 8356e49..d414604 100644 --- a/display.py +++ b/display.py @@ -1,12 +1,24 @@ class DisplayHTMLBlock(object): - def __init__(self, title): + def __init__(self, title=''): self.title = title def build(self, f): pass +class DisplayHTMLRawBlock(DisplayHTMLBlock): + + def __init__(self, title=''): + super(DisplayHTMLRawBlock, self).__init__(title) + self.html = '' + + def setRawHTML(self, html): + self.html = html + + def build(self, f): + f.write(self.html) + class DisplayHTMLBlockTable(DisplayHTMLBlock): def __init__(self, title, cols): diff --git a/iwla.py b/iwla.py index 62deb66..b321620 100755 --- a/iwla.py +++ b/iwla.py @@ -69,6 +69,9 @@ class IWLA(object): def getDisplay(self): return self.display + def getCurTime(self): + return self.meta_infos['last_time'] + def _clearMeta(self): self.meta_infos = { 'last_time' : None diff --git a/plugins/display/all_visits.py b/plugins/display/all_visits.py new file mode 100644 index 0000000..28a0534 --- /dev/null +++ b/plugins/display/all_visits.py @@ -0,0 +1,41 @@ +import time + +from iwla import IWLA +from iplugin import IPlugin +from display import * + +class IWLADisplayAllVisits(IPlugin): + def __init__(self, iwla): + super(IWLADisplayAllVisits, self).__init__(iwla) + self.API_VERSION = 1 + + def hook(self, iwla): + hits = iwla.getValidVisitors() + last_access = sorted(hits.values(), key=lambda t: t['last_access'], reverse=True) + + cur_time = self.iwla.getCurTime() + title = time.strftime('All visits %B %Y', cur_time) + + filename = 'all_visits_%d.html' % (cur_time.tm_mon) + path = '%d/%s' % (cur_time.tm_year, filename) + + page = DisplayHTMLPage(title, path) + table = DisplayHTMLBlockTable('Last seen', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen']) + for super_hit in last_access: + row = [ + super_hit['remote_addr'], + super_hit['viewed_pages'], + super_hit['viewed_hits'], + bytesToStr(super_hit['bandwidth']), + time.asctime(super_hit['last_access']) + ] + table.appendRow(row) + page.appendBlock(table) + + display = self.iwla.getDisplay() + display.addPage(page) + + index = iwla.getDisplayIndex() + block = DisplayHTMLRawBlock() + block.setRawHTML('All visits' % (filename)) + index.appendBlock(block)