Add bytesToStr()

Automatically convert list into strings in appendRow()
Add package information
This commit is contained in:
Gregory Soutade 2014-11-24 13:44:04 +01:00
parent 38c041126d
commit 670f024905
8 changed files with 48 additions and 14 deletions

View File

@ -15,7 +15,7 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
self.rows = [] self.rows = []
def appendRow(self, row): def appendRow(self, row):
self.rows.append(row) self.rows.append(listToStr(row))
def build(self, f): def build(self, f):
f.write('<table>') f.write('<table>')
@ -68,3 +68,21 @@ class DisplayHTMLBuild(object):
def build(self, root): def build(self, root):
for page in self.pages: for page in self.pages:
page.build(root) page.build(root)
def bytesToStr(bytes):
suffixes = ['', ' kB', ' MB', ' GB', ' TB']
for i in range(0, len(suffixes)):
if bytes < 1024: break
bytes /= 1024.0
if i:
return '%.02f%s' % (bytes, suffixes[i])
else:
return '%d%s' % (bytes, suffixes[i])
def _toStr(v):
if type(v) != str: return str(v)
else: return v
def listToStr(l): return map(lambda(v) : _toStr(v), l)

19
iwla.py
View File

@ -237,8 +237,8 @@ class IWLA(object):
nb_visits = 0 nb_visits = 0
for k in keys: for k in keys:
stats = self.current_analysis['days_stats'][k] stats = self.current_analysis['days_stats'][k]
row = [k, stats['nb_visitors'], stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']] row = [k, stats['nb_visitors'], stats['viewed_pages'], stats['viewed_hits'],
row = map(lambda(v): str(v), row) bytesToStr(stats['viewed_bandwidth']), bytesToStr(stats['not_viewed_bandwidth'])]
days.appendRow(row) days.appendRow(row)
nb_visits += stats['nb_visitors'] nb_visits += stats['nb_visitors']
@ -247,15 +247,18 @@ class IWLA(object):
nb_days = len(keys) nb_days = len(keys)
row = [0, nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']] row = [0, nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']]
if nb_days: if nb_days:
row = map(lambda(v): str(int(v/nb_days)), row) average_row = map(lambda(v): str(int(v/nb_days)), row)
else: else:
row = map(lambda(v): '0', row) average_row = map(lambda(v): '0', row)
row[0] = 'Average' average_row[0] = 'Average'
days.appendRow(row) average_row[4] = bytesToStr(row[4])
average_row[5] = bytesToStr(row[5])
days.appendRow(average_row)
row = ['Total', nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']] row[0] = 'Total'
row = map(lambda(v): str(v), row) row[4] = bytesToStr(row[4])
row[5] = bytesToStr(row[5])
days.appendRow(row) days.appendRow(row)
page.appendBlock(days) page.appendBlock(days)
self.display.addPage(page) self.display.addPage(page)

2
plugins/__init__.py Normal file
View File

@ -0,0 +1,2 @@
__all__ = ['pre_analysis', 'post_analysis', 'display']

View File

@ -0,0 +1 @@
#

View File

@ -18,15 +18,20 @@ def load():
def hook(iwla): def hook(iwla):
stats = iwla.getMonthStats() stats = iwla.getMonthStats()
if not 'top_visitors' in stats.keys(): top_visitors = stats.get('top_visitors', None)
if not top_visitors:
print 'Top visitors post analysis plugin not installed' print 'Top visitors post analysis plugin not installed'
return return
index = iwla.getDisplayIndex() index = iwla.getDisplayIndex()
table = DisplayHTMLBlockTable('Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen']) table = DisplayHTMLBlockTable('Top visitors', ['Host', 'Pages', 'Hits', 'Bandwidth', 'Last seen'])
for super_hit in stats['top_visitors']: for super_hit in top_visitors:
row = [super_hit['remote_addr'], super_hit['viewed_pages'], super_hit['viewed_hits'], super_hit['bandwidth'], 0] row = [
row = map(lambda(v): str(v), row) super_hit['remote_addr'],
row[4] = time.asctime(super_hit['last_access']) super_hit['viewed_pages'],
super_hit['viewed_hits'],
bytesToStr(super_hit['bandwidth']),
time.asctime(super_hit['last_access'])
]
table.appendRow(row) table.appendRow(row)
index.appendBlock(table) index.appendBlock(table)

View File

@ -0,0 +1 @@
#

View File

@ -19,9 +19,12 @@ def load():
def hook(iwla): def hook(iwla):
hits = iwla.getValidVisitors() hits = iwla.getValidVisitors()
for (k, hit) in hits.items(): for (k, hit) in hits.items():
if hit.get('dns_analysed', False): continue
try: try:
name, _, _ = socket.gethostbyaddr(k) name, _, _ = socket.gethostbyaddr(k)
hit['remote_addr'] = name hit['remote_addr'] = name
except: except:
pass pass
finally:
hit['dns_analysed'] = True

View File

@ -0,0 +1 @@
#