From 010c16cacad0016d21ea439a124fb73a4e411627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 8 Dec 2014 14:13:26 +0100 Subject: [PATCH] Give iwla to each display class Add generic functions createPage() and createBlock() in DisplayHTMLBuild Add and display IWLA version --- default_conf.py | 2 +- display.py | 32 ++++++++++++++++++++------------ iwla.py | 14 +++++++++----- plugins/display/all_visits.py | 8 ++++---- plugins/display/referers.py | 14 +++++++------- plugins/display/top_downloads.py | 9 +++++---- plugins/display/top_hits.py | 9 +++++---- plugins/display/top_pages.py | 9 +++++---- plugins/display/top_visitors.py | 3 ++- 9 files changed, 58 insertions(+), 42 deletions(-) diff --git a/default_conf.py b/default_conf.py index 3812624..d9b445e 100644 --- a/default_conf.py +++ b/default_conf.py @@ -31,5 +31,5 @@ multimedia_files = ['png', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'] resources_path = ['resources'] -icon_path = ['%s/%s' % (os.path.basename(resources_path[0]), 'icon')] +icon_path = '%s/%s' % (os.path.basename(resources_path[0]), 'icon') css_path = ['%s/%s/%s' % (os.path.basename(resources_path[0]), 'css', 'iwla.css')] diff --git a/display.py b/display.py index 0ffd61d..5e8dbd4 100644 --- a/display.py +++ b/display.py @@ -3,7 +3,8 @@ import codecs class DisplayHTMLRaw(object): - def __init__(self, html=u''): + def __init__(self, iwla, html=u''): + self.iwla = iwla self.html = html def setRawHTML(self, html): @@ -21,8 +22,8 @@ class DisplayHTMLRaw(object): class DisplayHTMLBlock(DisplayHTMLRaw): - def __init__(self, title=''): - super(DisplayHTMLBlock, self).__init__(html='') + def __init__(self, iwla, title=''): + super(DisplayHTMLBlock, self).__init__(iwla, html='') self.title = title self.cssclass = u'iwla_block' self.title_cssclass = u'iwla_block_title' @@ -54,8 +55,8 @@ class DisplayHTMLBlock(DisplayHTMLRaw): class DisplayHTMLBlockTable(DisplayHTMLBlock): - def __init__(self, title, cols): - super(DisplayHTMLBlockTable, self).__init__(title=title) + def __init__(self, iwla, title, cols): + super(DisplayHTMLBlockTable, self).__init__(iwla=iwla, title=title) self.cols = listToStr(cols) self.rows = [] self.cols_cssclasses = [u''] * len(cols) @@ -153,14 +154,12 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock): class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): - def __init__(self, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None): - super(DisplayHTMLBlockTableWithGraph, self).__init__(title=title, cols=cols) + def __init__(self, iwla, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None): + super(DisplayHTMLBlockTableWithGraph, self).__init__(iwla=iwla, title=title, cols=cols) self.short_titles = short_titles or [] self.short_titles = listToStr(self.short_titles) self.nb_valid_rows = nb_valid_rows - # TOFIX - self.icon_path = u'/resources/icon' - # self.icon_path = self.iwla.getConfValue('icon_path', '/') + self.icon_path = self.iwla.getConfValue('icon_path', '/') self.raw_rows = [] self.maxes = [0] * len(cols) self.table_graph_css = u'iwla_graph_table' @@ -195,7 +194,7 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): elif style.startswith(u'iwla_visit'): icon = u'vv.png' else: return '' - return u'%s/%s' % (self.icon_path, icon) + return u'/%s/%s' % (self.icon_path, icon) def _buildHTML(self): self._computeMax() @@ -236,7 +235,8 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): class DisplayHTMLPage(object): - def __init__(self, title, filename, css_path): + def __init__(self, iwla, title, filename, css_path): + self.iwla = iwla self.title = unicode(title) self.filename = filename self.blocks = [] @@ -273,6 +273,8 @@ class DisplayHTMLPage(object): f.write(u'') for block in self.blocks: block.build(f) + f.write(u'
Generated by IWLA %s
' % + ("http://indefero.soutade.fr/p/iwla", self.iwla.getVersion())) f.write(u'') f.close() @@ -282,6 +284,12 @@ class DisplayHTMLBuild(object): self.pages = [] self.iwla = iwla + def createPage(self, *args): + return DisplayHTMLPage(self.iwla, *args) + + def createBlock(self, _class, *args): + return _class(self.iwla, *args) + def getPage(self, filename): for page in self.pages: if page.getFilename() == filename: diff --git a/iwla.py b/iwla.py index 72a9774..6ddf237 100755 --- a/iwla.py +++ b/iwla.py @@ -24,6 +24,7 @@ class IWLA(object): ANALYSIS_CLASS = 'HTTP' API_VERSION = 1 + IWLA_VERSION = '0.1' def __init__(self): print '==> Start' @@ -44,6 +45,9 @@ class IWLA(object): (conf.POST_HOOK_DIRECTORY , conf.post_analysis_hooks), (conf.DISPLAY_HOOK_DIRECTORY , conf.display_hooks)] + def getVersion(self): + return IWLA.IWLA_VERSION + def getConfValue(self, key, default=None): if not key in dir(conf): return default @@ -242,10 +246,10 @@ class IWLA(object): title = 'Stats %d/%d' % (cur_time.tm_mon, cur_time.tm_year) filename = self.getCurDisplayPath('index.html') print '==> Generate display (%s)' % (filename) - page = DisplayHTMLPage(title, filename, conf.css_path) + page = self.display.createPage(title, filename, conf.css_path) _, nb_month_days = monthrange(cur_time.tm_year, cur_time.tm_mon) - days = DisplayHTMLBlockTableWithGraph('By day', ['Day', 'Visitors', 'Pages', 'Hits', 'Bandwidth', 'Not viewed Bandwidth'], nb_valid_rows=nb_month_days, graph_cols=range(1,6)) + days = self.display.createBlock(DisplayHTMLBlockTableWithGraph, 'By day', ['Day', 'Visitors', 'Pages', 'Hits', 'Bandwidth', 'Not viewed Bandwidth'], None, nb_month_days, range(1,6)) days.setColsCSSClass(['', 'iwla_visitor', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth']) nb_visits = 0 nb_days = 0 @@ -300,7 +304,7 @@ class IWLA(object): title = 'Summary %d' % (year) cols = ['Month', 'Visitors', 'Pages', 'Hits', 'Bandwidth', 'Not viewed Bandwidth', 'Details'] graph_cols=range(1,6) - months = DisplayHTMLBlockTableWithGraph(title, cols, nb_valid_rows=12, graph_cols=graph_cols) + months = self.display.createBlock(DisplayHTMLBlockTableWithGraph, title, cols, None, 12, graph_cols) months.setColsCSSClass(['', 'iwla_visitor', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth', '']) total = [0] * len(cols) for i in range(1, 13): @@ -336,10 +340,10 @@ class IWLA(object): filename = 'index.html' print '==> Generate main page (%s)' % (filename) - page = DisplayHTMLPage(title, filename, conf.css_path) + page = self.display.createPage(title, filename, conf.css_path) last_update = 'Last update %s
' % (time.strftime('%d %b %Y %H:%M', time.localtime())) - page.appendBlock(DisplayHTMLRaw(last_update)) + page.appendBlock(self.display.createBlock(DisplayHTMLRaw, last_update)) for year in self.meta_infos['stats'].keys(): self._generateDisplayMonthStats(page, year, self.meta_infos['stats'][year]) diff --git a/plugins/display/all_visits.py b/plugins/display/all_visits.py index 42a5b3c..b327362 100644 --- a/plugins/display/all_visits.py +++ b/plugins/display/all_visits.py @@ -10,6 +10,7 @@ class IWLADisplayAllVisits(IPlugin): self.API_VERSION = 1 def hook(self): + display = self.iwla.getDisplay() hits = self.iwla.getValidVisitors() display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False) @@ -20,8 +21,8 @@ class IWLADisplayAllVisits(IPlugin): filename = 'all_visits.html' path = self.iwla.getCurDisplayPath(filename) - page = DisplayHTMLPage(title, path, self.iwla.getConfValue('css_path', [])) - table = DisplayHTMLBlockTable('Last seen', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen']) + page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) + table = display.createBlock(DisplayHTMLBlockTable, 'Last seen', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', '']) for super_hit in last_access: @@ -40,7 +41,6 @@ class IWLADisplayAllVisits(IPlugin): table.appendRow(row) page.appendBlock(table) - display = self.iwla.getDisplay() display.addPage(page) index = self.iwla.getDisplayIndex() @@ -49,6 +49,6 @@ class IWLADisplayAllVisits(IPlugin): if block: block.setTitle('%s - %s' % (block.getTitle(), link)) else: - block = DisplayHTMLRawBlock() + block = display.createBlock(DisplayHTMLRawBlock) block.setRawHTML(link) index.appendBlock(block) diff --git a/plugins/display/referers.py b/plugins/display/referers.py index be08acd..4cdfc79 100644 --- a/plugins/display/referers.py +++ b/plugins/display/referers.py @@ -11,6 +11,7 @@ class IWLADisplayReferers(IPlugin): self.requires = ['IWLAPostAnalysisReferers'] def hook(self): + display = self.iwla.getDisplay() month_stats = self.iwla.getMonthStats() referers = month_stats.get('referers', {}) robots_referers = month_stats.get('robots_referers', {}) @@ -38,8 +39,8 @@ class IWLADisplayReferers(IPlugin): filename = 'referers.html' path = self.iwla.getCurDisplayPath(filename) - page = DisplayHTMLPage(title, path, self.iwla.getConfValue('css_path', [])) - table = DisplayHTMLBlockTable('Connexion from', ['Origin', 'Pages', 'Hits']) + page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) + table = display.createBlock(DisplayHTMLBlockTable, 'Connexion from', ['Origin', 'Pages', 'Hits']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit']) total_search = [0]*3 @@ -68,7 +69,6 @@ class IWLADisplayReferers(IPlugin): page.appendBlock(table) - display = self.iwla.getDisplay() display.addPage(page) link = 'All referers' % (filename) @@ -76,7 +76,7 @@ class IWLADisplayReferers(IPlugin): # Top referers in index title = '%s - %s' % ('Connexion from', link) - table = DisplayHTMLBlockTable(title, ['Origin', 'Pages', 'Hits']) + table = display.createBlock(DisplayHTMLBlockTable, title, ['Origin', 'Pages', 'Hits']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit']) table.appendRow(['Search Engine', '', '']) @@ -121,8 +121,8 @@ class IWLADisplayReferers(IPlugin): path = self.iwla.getCurDisplayPath(filename) total_search = [0]*2 - page = DisplayHTMLPage(title, path, self.iwla.getConfValue('css_path', [])) - table = DisplayHTMLBlockTable('Top key phrases', ['Key phrase', 'Search']) + page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) + table = display.createBlock(DisplayHTMLBlockTable, 'Top key phrases', ['Key phrase', 'Search']) table.setColsCSSClass(['', 'iwla_search']) for phrase in top_key_phrases: table.appendRow([phrase[0], phrase[1]]) @@ -135,7 +135,7 @@ class IWLADisplayReferers(IPlugin): # Top key phrases in index title = '%s - %s' % ('Top key phrases', link) - table = DisplayHTMLBlockTable(title, ['Key phrase', 'Search']) + table = display.createBlock(DisplayHTMLBlockTable, title, ['Key phrase', 'Search']) table.setColsCSSClass(['', 'iwla_search']) for phrase in top_key_phrases[:10]: table.appendRow([phrase[0], phrase[1]]) diff --git a/plugins/display/top_downloads.py b/plugins/display/top_downloads.py index 0e42b8d..0be4fb4 100644 --- a/plugins/display/top_downloads.py +++ b/plugins/display/top_downloads.py @@ -11,6 +11,7 @@ class IWLADisplayTopDownloads(IPlugin): self.requires = ['IWLAPostAnalysisTopDownloads'] def hook(self): + display = self.iwla.getDisplay() top_downloads = self.iwla.getMonthStats()['top_downloads'] top_downloads = sorted(top_downloads.items(), key=lambda t: t[1], reverse=True) @@ -19,8 +20,8 @@ class IWLADisplayTopDownloads(IPlugin): path = self.iwla.getCurDisplayPath(filename) title = time.strftime('All Downloads - %B %Y', self.iwla.getCurTime()) - page = DisplayHTMLPage(title, path, self.iwla.getConfValue('css_path', [])) - table = DisplayHTMLBlockTable('All Downloads', ['URI', 'Hit']) + page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) + table = display.createBlock(DisplayHTMLBlockTable, 'All Downloads', ['URI', 'Hit']) table.setColsCSSClass(['', 'iwla_hit']) total_entrance = [0]*2 @@ -29,7 +30,7 @@ class IWLADisplayTopDownloads(IPlugin): total_entrance[1] += entrance page.appendBlock(table) - self.iwla.getDisplay().addPage(page) + display.addPage(page) link = 'All Downloads' % (filename) title = '%s - %s' % ('Top Downloads', link) @@ -37,7 +38,7 @@ class IWLADisplayTopDownloads(IPlugin): # Top in index index = self.iwla.getDisplayIndex() - table = DisplayHTMLBlockTable(title, ['URI', 'Hits']) + table = display.createBlock(DisplayHTMLBlockTable, title, ['URI', 'Hits']) table.setColsCSSClass(['', 'iwla_hit']) for (uri, entrance) in top_downloads[:10]: table.appendRow([generateHTMLLink(uri), entrance]) diff --git a/plugins/display/top_hits.py b/plugins/display/top_hits.py index 8128fc9..5cc24e0 100644 --- a/plugins/display/top_hits.py +++ b/plugins/display/top_hits.py @@ -11,6 +11,7 @@ class IWLADisplayTopHits(IPlugin): self.requires = ['IWLAPostAnalysisTopHits'] def hook(self): + display = self.iwla.getDisplay() top_hits = self.iwla.getMonthStats()['top_hits'] top_hits = sorted(top_hits.items(), key=lambda t: t[1], reverse=True) @@ -19,8 +20,8 @@ class IWLADisplayTopHits(IPlugin): filename = 'top_hits.html' path = self.iwla.getCurDisplayPath(filename) - page = DisplayHTMLPage(title, path, self.iwla.getConfValue('css_path', [])) - table = DisplayHTMLBlockTable('All Hits', ['URI', 'Entrance']) + page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) + table = display.createBlock(DisplayHTMLBlockTable, 'All Hits', ['URI', 'Entrance']) table.setColsCSSClass(['', 'iwla_hit']) total_hits = [0]*2 for (uri, entrance) in top_hits: @@ -28,7 +29,7 @@ class IWLADisplayTopHits(IPlugin): total_hits[1] += entrance page.appendBlock(table) - self.iwla.getDisplay().addPage(page) + display.addPage(page) link = 'All hits' % (filename) title = '%s - %s' % ('Top Hits', link) @@ -36,7 +37,7 @@ class IWLADisplayTopHits(IPlugin): # Top in index index = self.iwla.getDisplayIndex() - table = DisplayHTMLBlockTable(title, ['URI', 'Entrance']) + table = display.createBlock(DisplayHTMLBlockTable, title, ['URI', 'Entrance']) table.setColsCSSClass(['', 'iwla_hit']) for (uri, entrance) in top_hits[:10]: table.appendRow([generateHTMLLink(uri), entrance]) diff --git a/plugins/display/top_pages.py b/plugins/display/top_pages.py index 0c7211e..524ad49 100644 --- a/plugins/display/top_pages.py +++ b/plugins/display/top_pages.py @@ -11,6 +11,7 @@ class IWLADisplayTopPages(IPlugin): self.requires = ['IWLAPostAnalysisTopPages'] def hook(self): + display = self.iwla.getDisplay() top_pages = self.iwla.getMonthStats()['top_pages'] top_pages = sorted(top_pages.items(), key=lambda t: t[1], reverse=True) @@ -19,8 +20,8 @@ class IWLADisplayTopPages(IPlugin): filename = 'top_pages.html' path = self.iwla.getCurDisplayPath(filename) - page = DisplayHTMLPage(title, path, self.iwla.getConfValue('css_path', [])) - table = DisplayHTMLBlockTable('All Pages', ['URI', 'Entrance']) + page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) + table = display.createBlock(DisplayHTMLBlockTable, 'All Pages', ['URI', 'Entrance']) table.setColsCSSClass(['', 'iwla_hit']) total_hits = [0]*2 for (uri, entrance) in top_pages: @@ -28,7 +29,7 @@ class IWLADisplayTopPages(IPlugin): total_hits[1] += entrance page.appendBlock(table) - self.iwla.getDisplay().addPage(page) + display.addPage(page) link = 'All pages' % (filename) title = '%s - %s' % ('Top Pages', link) @@ -36,7 +37,7 @@ class IWLADisplayTopPages(IPlugin): # Top in index index = self.iwla.getDisplayIndex() - table = DisplayHTMLBlockTable(title, ['URI', 'Entrance']) + table = display.createBlock(DisplayHTMLBlockTable, title, ['URI', 'Entrance']) table.setColsCSSClass(['', 'iwla_hit']) for (uri, entrance) in top_pages[:10]: table.appendRow([generateHTMLLink(uri), entrance]) diff --git a/plugins/display/top_visitors.py b/plugins/display/top_visitors.py index 1711005..4e22af4 100644 --- a/plugins/display/top_visitors.py +++ b/plugins/display/top_visitors.py @@ -11,6 +11,7 @@ class IWLADisplayTopVisitors(IPlugin): def hook(self): hits = self.iwla.getValidVisitors() + display = self.iwla.getDisplay() display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False) total = [0]*5 @@ -24,7 +25,7 @@ class IWLADisplayTopVisitors(IPlugin): top_visitors = [hits[h[0]] for h in top_bandwidth[:10]] index = self.iwla.getDisplayIndex() - table = DisplayHTMLBlockTable('Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen']) + table = display.createBlock(DisplayHTMLBlockTable, 'Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', '']) for super_hit in top_visitors: address = super_hit['remote_addr']