Give iwla to each display class

Add generic functions createPage() and createBlock() in DisplayHTMLBuild
Add and display IWLA version
This commit is contained in:
Grégory Soutadé 2014-12-08 14:13:26 +01:00
parent fd858034fb
commit 010c16caca
9 changed files with 58 additions and 42 deletions

View File

@ -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')]

View File

@ -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'</head>')
for block in self.blocks:
block.build(f)
f.write(u'<center>Generated by <a href="%s">IWLA %s</a></center>' %
("http://indefero.soutade.fr/p/iwla", self.iwla.getVersion()))
f.write(u'</body></html>')
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:

14
iwla.py
View File

@ -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 = '<b>Last update</b> %s<br />' % (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])

View File

@ -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)

View File

@ -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 = '<a href=\'%s\'>All referers</a>' % (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(['<b>Search Engine</b>', '', ''])
@ -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]])

View File

@ -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 = '<a href=\'%s\'>All Downloads</a>' % (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])

View File

@ -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 = '<a href=\'%s\'>All hits</a>' % (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])

View File

@ -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 = '<a href=\'%s\'>All pages</a>' % (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])

View File

@ -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']