Add parameter to do strToBytes in BlockTable and fix some errors with string and int

This commit is contained in:
Grégory Soutadé 2020-12-11 09:18:50 +01:00
parent 3ed8ba40be
commit a6f8c71bf2
7 changed files with 42 additions and 52 deletions

View File

@ -98,13 +98,14 @@ class DisplayHTMLBlock(DisplayHTMLRaw):
class DisplayHTMLBlockTable(DisplayHTMLBlock): class DisplayHTMLBlockTable(DisplayHTMLBlock):
def __init__(self, iwla, title, cols): def __init__(self, iwla, title, cols, human_readable_cols=None):
super(DisplayHTMLBlockTable, self).__init__(iwla=iwla, title=title) super(DisplayHTMLBlockTable, self).__init__(iwla=iwla, title=title)
self.cols = listToStr(cols) self.cols = listToStr(cols)
self.rows = [] self.rows = []
self.cols_cssclasses = [u''] * len(cols) self.cols_cssclasses = [u''] * len(cols)
self.rows_cssclasses = [] self.rows_cssclasses = []
self.table_css = u'iwla_table' self.table_css = u'iwla_table'
self.human_readable_cols = human_readable_cols or []
def appendRow(self, row): def appendRow(self, row):
self.rows.append(listToStr(row)) self.rows.append(listToStr(row))
@ -226,6 +227,8 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
html += u'<tr>' html += u'<tr>'
for j in range(0, len(row)): for j in range(0, len(row)):
v = row[j] v = row[j]
if j in self.human_readable_cols:
v = bytesToStr(v)
style = self.getCellCSSClass(i, j) style = self.getCellCSSClass(i, j)
if style: style = u' class="%s"' % (style) if style: style = u' class="%s"' % (style)
html += u'<td%s>%s</td>' % (style, v) html += u'<td%s>%s</td>' % (style, v)
@ -238,22 +241,19 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
def __init__(self, iwla, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None): 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) human_readable_cols=None):
super(DisplayHTMLBlockTableWithGraph, self).__init__(iwla=iwla, title=title, cols=cols,
human_readable_cols=human_readable_cols)
self.short_titles = short_titles or [] self.short_titles = short_titles or []
self.short_titles = listToStr(self.short_titles) self.short_titles = listToStr(self.short_titles)
self.nb_valid_rows = nb_valid_rows self.nb_valid_rows = nb_valid_rows
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.maxes = [0] * len(cols)
self.table_graph_css = u'iwla_graph_table' self.table_graph_css = u'iwla_graph_table'
self.td_img_css = u'iwla_td_img' self.td_img_css = u'iwla_td_img'
self.graph_cols = graph_cols or [] self.graph_cols = graph_cols or []
def appendRow(self, row):
self.raw_rows.append(row)
super(DisplayHTMLBlockTableWithGraph, self).appendRow(row)
def appendShortTitle(self, short_title): def appendShortTitle(self, short_title):
self.short_titles.append(short_title) self.short_titles.append(short_title)
@ -265,13 +265,12 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
def _computeMax(self): def _computeMax(self):
for i in range(0, self.nb_valid_rows): for i in range(0, self.nb_valid_rows):
row = self.raw_rows[i] row = self.row[i]
for j in range(1, len(row)): for j in range(1, len(row)):
try: if type(row[j]) != int:
continue
if row[j] > self.maxes[j]: if row[j] > self.maxes[j]:
self.maxes[j] = row[j] self.maxes[j] = row[j]
except:
continue
def _getIconFromStyle(self, style): def _getIconFromStyle(self, style):
if style.startswith(u'iwla_page'): icon = u'vp.png' if style.startswith(u'iwla_page'): icon = u'vp.png'
@ -302,7 +301,7 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable):
if style: style = u' class="%s"' % (style) if style: style = u' class="%s"' % (style)
alt = u'%s: %s' % (row[j], self.cols[j]) alt = u'%s: %s' % (row[j], self.cols[j])
if self.maxes[j]: if self.maxes[j]:
height = int((self.raw_rows[i][j] * 100) / self.maxes[j]) or 1 height = int((self.rows[i][j] * 100) / self.maxes[j]) or 1
else: else:
height = 1 height = 1
html += u'<img%s src="%s" height="%d" width="6" alt="%s" title="%s" />' % (style, icon, height, alt, alt) html += u'<img%s src="%s" height="%d" width="6" alt="%s" title="%s" />' % (style, icon, height, alt, alt)
@ -427,20 +426,14 @@ class DisplayHTMLBuild(object):
def bytesToStr(_bytes): def bytesToStr(_bytes):
suffixes = [u'', u' kB', u' MB', u' GB', u' TB'] suffixes = [u'', u' kB', u' MB', u' GB', u' TB']
try:
if type(_bytes) != int:
_bytes = int(_bytes, 10)
except:
return _bytes
for i in range(0, len(suffixes)): for i in range(0, len(suffixes)):
if _bytes < 1024: break if _bytes < 1024: break
_bytes /= 1024.0 _bytes /= 1024.0
if i: if i:
return u'%.02f%s' % (_bytes, suffixes[i]) return '%.02f%s' % (_bytes, suffixes[i])
else: else:
return u'%d%s' % (_bytes, suffixes[i]) return '%d%s' % (_bytes, suffixes[i])
def _toStr(v): def _toStr(v):
return v return v

28
iwla.py
View File

@ -449,7 +449,7 @@ class IWLA(object):
page.appendBlock(link) page.appendBlock(link)
_, nb_month_days = monthrange(cur_time.tm_year, cur_time.tm_mon) _, nb_month_days = monthrange(cur_time.tm_year, cur_time.tm_mon)
days = self.display.createBlock(DisplayHTMLBlockTableWithGraph, self._('By day'), [self._('Day'), self._('Visits'), self._('Pages'), self._('Hits'), self._('Bandwidth'), self._('Not viewed Bandwidth')], None, nb_month_days, range(1,6)) days = self.display.createBlock(DisplayHTMLBlockTableWithGraph, self._('By day'), [self._('Day'), self._('Visits'), self._('Pages'), self._('Hits'), self._('Bandwidth'), self._('Not viewed Bandwidth')], None, nb_month_days, range(1,6), [4, 5])
days.setColsCSSClass(['', 'iwla_visit', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth']) days.setColsCSSClass(['', 'iwla_visit', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth'])
nb_visits = 0 nb_visits = 0
nb_days = 0 nb_days = 0
@ -465,8 +465,10 @@ class IWLA(object):
else: else:
row = [full_day, 0, 0, 0, 0, 0] row = [full_day, 0, 0, 0, 0, 0]
days.appendRow(row) days.appendRow(row)
days.setCellValue(i-1, 4, bytesToStr(row[4])) viewed_bandwidth = row[4]
days.setCellValue(i-1, 5, bytesToStr(row[5])) not_viewed_bandwidth = row[5]
days.setCellValue(i-1, 4, viewed_bandwidth)
days.setCellValue(i-1, 5, not_viewed_bandwidth)
days.appendShortTitle(day) days.appendShortTitle(day)
adate = date(cur_time.tm_year, cur_time.tm_mon, i) adate = date(cur_time.tm_year, cur_time.tm_mon, i)
week_day = adate.weekday() week_day = adate.weekday()
@ -487,13 +489,9 @@ class IWLA(object):
average_row = list(map(lambda v: 0, row)) average_row = list(map(lambda v: 0, row))
average_row[0] = self._('Average') average_row[0] = self._('Average')
average_row[4] = bytesToStr(average_row[4])
average_row[5] = bytesToStr(average_row[5])
days.appendRow(average_row) days.appendRow(average_row)
row[0] = self._('Total') row[0] = self._('Total')
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)
@ -504,9 +502,9 @@ class IWLA(object):
title = '%s %d' % (self._('Summary'), year) title = '%s %d' % (self._('Summary'), year)
cols = [self._('Month'), self._('Visitors'), self._('Visits'), self._('Pages'), self._('Hits'), self._('Bandwidth'), self._('Not viewed Bandwidth'), self._('Details')] cols = [self._('Month'), self._('Visitors'), self._('Visits'), self._('Pages'), self._('Hits'), self._('Bandwidth'), self._('Not viewed Bandwidth'), self._('Details')]
graph_cols=range(1,7) graph_cols=range(1,7)
months = self.display.createBlock(DisplayHTMLBlockTableWithGraph, title, cols, None, 12, graph_cols) months = self.display.createBlock(DisplayHTMLBlockTableWithGraph, title, cols, None, 12, graph_cols, [5, 6])
months.setColsCSSClass(['', 'iwla_visitor', 'iwla_visit', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth', '']) months.setColsCSSClass(['', 'iwla_visitor', 'iwla_visit', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth', ''])
months_ = self.display.createBlock(DisplayHTMLBlockTableWithGraph, title, cols[:-1], None, 12, graph_cols[:-1]) months_ = self.display.createBlock(DisplayHTMLBlockTableWithGraph, title, cols[:-1], None, 12, graph_cols[:-1], [5, 6])
months_.setColsCSSClass(['', 'iwla_visitor', 'iwla_visit', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth']) months_.setColsCSSClass(['', 'iwla_visitor', 'iwla_visit', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', 'iwla_bandwidth'])
total = [0] * len(cols) total = [0] * len(cols)
for i in range(1, 13): for i in range(1, 13):
@ -522,12 +520,14 @@ class IWLA(object):
else: else:
row = [full_month, 0, 0, 0, 0, 0, 0, ''] row = [full_month, 0, 0, 0, 0, 0, 0, '']
months.appendRow(row) months.appendRow(row)
months.setCellValue(i-1, 5, bytesToStr(row[5])) viewed_bandwidth = row[5]
months.setCellValue(i-1, 6, bytesToStr(row[6])) not_viewed_bandwidth = row[6]
months.setCellValue(i-1, 5, viewed_bandwidth)
months.setCellValue(i-1, 6, not_viewed_bandwidth)
months.appendShortTitle(month) months.appendShortTitle(month)
months_.appendRow(row[:-1]) months_.appendRow(row[:-1])
months_.setCellValue(i-1, 5, bytesToStr(row[5])) months_.setCellValue(i-1, 5, viewed_bandwidth)
months_.setCellValue(i-1, 6, bytesToStr(row[6])) months_.setCellValue(i-1, 6, not_viewed_bandwidth)
months_.appendShortTitle(month) months_.appendShortTitle(month)
if year == cur_time.tm_year and i == cur_time.tm_mon: if year == cur_time.tm_year and i == cur_time.tm_mon:
css = months.getCellCSSClass(i-1, 0) css = months.getCellCSSClass(i-1, 0)
@ -537,8 +537,6 @@ class IWLA(object):
months_.setCellCSSClass(i-1, 0, css) months_.setCellCSSClass(i-1, 0, css)
total[0] = self._('Total') total[0] = self._('Total')
total[5] = bytesToStr(total[5])
total[6] = bytesToStr(total[6])
total[7] = u'' total[7] = u''
months.appendRow(total) months.appendRow(total)
page.appendBlock(months) page.appendBlock(months)

View File

@ -67,7 +67,7 @@ class IWLADisplayAllVisits(IPlugin):
path = self.iwla.getCurDisplayPath(filename) path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Last seen'), [self.iwla._(u'Host'), self.iwla._(u'Pages'), self.iwla._(u'Hits'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')]) table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Last seen'), [self.iwla._(u'Host'), self.iwla._(u'Pages'), self.iwla._(u'Hits'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [3])
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', '']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
for super_hit in last_access: for super_hit in last_access:
@ -80,7 +80,7 @@ class IWLADisplayAllVisits(IPlugin):
address, address,
super_hit['viewed_pages'][0], super_hit['viewed_pages'][0],
super_hit['viewed_hits'][0], super_hit['viewed_hits'][0],
bytesToStr(super_hit['bandwidth'][0]), super_hit['bandwidth'][0],
time.asctime(super_hit['last_access']) time.asctime(super_hit['last_access'])
] ]
table.appendRow(row) table.appendRow(row)

View File

@ -152,7 +152,7 @@ class IWLADisplayFilterUsers(IPlugin):
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Filtered users'), [self.iwla._(u'Pages'), self.iwla._(u'Last Access')]) table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Filtered users'), [self.iwla._(u'Pages'), self.iwla._(u'Last Access')])
table.setColsCSSClass(['iwla_page', '']) table.setColsCSSClass(['iwla_page', ''])
for filtered_user in self.filtered_users: for filtered_user in self.filtered_users:
ip = filtered_user['remote_addr'] ip = filtered_user['remote_ip']
if 'dns_name_replaced' in hits[ip].keys(): if 'dns_name_replaced' in hits[ip].keys():
ip_title = '<b>%s [%s]</b>' % (hits[ip]['remote_addr'], ip) ip_title = '<b>%s [%s]</b>' % (hits[ip]['remote_addr'], ip)
else: else:
@ -188,7 +188,7 @@ class IWLADisplayFilterUsers(IPlugin):
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')]) table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
table.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit']) table.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit'])
for filtered_user in self.filtered_users: for filtered_user in self.filtered_users:
ip = filtered_user['remote_addr'] ip = filtered_user['remote_ip']
if 'dns_name_replaced' in hits[ip].keys(): if 'dns_name_replaced' in hits[ip].keys():
ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip) ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip)
else: else:

View File

@ -70,19 +70,19 @@ class IWLADisplayHoursStats(IPlugin):
# By Day # By Day
title = self.iwla._(u'By day') title = self.iwla._(u'By day')
days = [self.iwla._('Mon'), self.iwla._('Tue'), self.iwla._('Wed'), self.iwla._('Thu'), self.iwla._('Fri'), self.iwla._('Sat'), self.iwla._('Sun')] days = [self.iwla._('Mon'), self.iwla._('Tue'), self.iwla._('Wed'), self.iwla._('Thu'), self.iwla._('Fri'), self.iwla._('Sat'), self.iwla._('Sun')]
table = display.createBlock(DisplayHTMLBlockTableWithGraph, title, [self.iwla._('Day'), self.iwla._('Pages'), self.iwla._('Hits'), self.iwla._('Bandwidth')], days, 7, range(1,4)) table = display.createBlock(DisplayHTMLBlockTableWithGraph, title, [self.iwla._('Day'), self.iwla._('Pages'), self.iwla._('Hits'), self.iwla._('Bandwidth')], days, 7, range(1,4), [3])
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth'])
for i in range(0,7): for i in range(0,7):
table.appendRow([days[i], days_stats[i]['pages'], days_stats[i]['hits'], days_stats[i]['bandwidth']]) table.appendRow([days[i], days_stats[i]['pages'], days_stats[i]['hits'], days_stats[i]['bandwidth']])
table.setCellValue(i, 3, bytesToStr(days_stats[i]['bandwidth'])) table.setCellValue(i, 3, days_stats[i]['bandwidth'])
index.appendBlock(table) index.appendBlock(table)
# By Hours # By Hours
title = self.iwla._(u'By Hours') title = self.iwla._(u'By Hours')
hours = ['%02d' % i for i in range(0, 24)] hours = ['%02d' % i for i in range(0, 24)]
table = display.createBlock(DisplayHTMLBlockTableWithGraph, title, [self.iwla._('Hours'), self.iwla._('Pages'), self.iwla._('Hits'), self.iwla._('Bandwidth')], hours, 24, range(1,4)) table = display.createBlock(DisplayHTMLBlockTableWithGraph, title, [self.iwla._('Hours'), self.iwla._('Pages'), self.iwla._('Hits'), self.iwla._('Bandwidth')], hours, 24, range(1,4), [3])
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth'])
for i in range(0,24): for i in range(0,24):
table.appendRow([hours[i], hours_stats[i]['pages'], hours_stats[i]['hits'], hours_stats[i]['bandwidth']]) table.appendRow([hours[i], hours_stats[i]['pages'], hours_stats[i]['hits'], hours_stats[i]['bandwidth']])
table.setCellValue(i, 3, bytesToStr(hours_stats[i]['bandwidth'])) table.setCellValue(i, 3, hours_stats[i]['bandwidth'])
index.appendBlock(table) index.appendBlock(table)

View File

@ -78,7 +78,7 @@ class IWLADisplayRobotBandwidth(IPlugin):
path = self.iwla.getCurDisplayPath(filename) path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', [])) page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')]) table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [1])
table.setColsCSSClass(['', 'iwla_bandwidth', '']) table.setColsCSSClass(['', 'iwla_bandwidth', ''])
for (super_hit, bandwidth) in bandwidths: for (super_hit, bandwidth) in bandwidths:
address = super_hit['remote_addr'] address = super_hit['remote_addr']
@ -88,7 +88,7 @@ class IWLADisplayRobotBandwidth(IPlugin):
row = [ row = [
address, address,
bytesToStr(bandwidth), bandwidth,
time.asctime(super_hit['last_access']) time.asctime(super_hit['last_access'])
] ]
table.appendRow(row) table.appendRow(row)
@ -103,7 +103,7 @@ class IWLADisplayRobotBandwidth(IPlugin):
# Top in index # Top in index
index = self.iwla.getDisplayIndex() index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')]) table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [1])
table.setColsCSSClass(['', 'iwla_bandwidth', '']) table.setColsCSSClass(['', 'iwla_bandwidth', ''])
for (super_hit, bandwidth) in bandwidths[:10]: for (super_hit, bandwidth) in bandwidths[:10]:
@ -114,7 +114,7 @@ class IWLADisplayRobotBandwidth(IPlugin):
row = [ row = [
address, address,
bytesToStr(bandwidth), bandwidth,
time.asctime(super_hit['last_access']) time.asctime(super_hit['last_access'])
] ]
table.appendRow(row) table.appendRow(row)

View File

@ -69,7 +69,7 @@ class IWLADisplayTopVisitors(IPlugin):
top_visitors = [hits[h[0]] for h in top_bandwidth[:10]] top_visitors = [hits[h[0]] for h in top_bandwidth[:10]]
index = self.iwla.getDisplayIndex() index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Top visitors'), [self.iwla._(u'Host'), self.iwla._(u'Pages'), self.iwla._(u'Hits'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')]) table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Top visitors'), [self.iwla._(u'Host'), self.iwla._(u'Pages'), self.iwla._(u'Hits'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [3])
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', '']) table.setColsCSSClass(['', 'iwla_page', 'iwla_hit', 'iwla_bandwidth', ''])
for super_hit in top_visitors: for super_hit in top_visitors:
address = super_hit['remote_addr'] address = super_hit['remote_addr']
@ -81,7 +81,7 @@ class IWLADisplayTopVisitors(IPlugin):
address, address,
super_hit['viewed_pages'][0], super_hit['viewed_pages'][0],
super_hit['viewed_hits'][0], super_hit['viewed_hits'][0],
bytesToStr(super_hit['bandwidth'][0]), super_hit['bandwidth'][0],
time.asctime(super_hit['last_access']) time.asctime(super_hit['last_access'])
] ]
total[1] -= super_hit['viewed_pages'][0] total[1] -= super_hit['viewed_pages'][0]
@ -90,7 +90,6 @@ class IWLADisplayTopVisitors(IPlugin):
table.appendRow(row) table.appendRow(row)
if total[1] or total[2] or total[3]: if total[1] or total[2] or total[3]:
total[0] = self.iwla._(u'Others') total[0] = self.iwla._(u'Others')
total[3] = bytesToStr(total[3])
total[4] = '' total[4] = ''
table.appendRow(total) table.appendRow(total)
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others') table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')