From a6f8c71bf26f4a10781a7db20e8a7c3b7075ab7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Fri, 11 Dec 2020 09:18:50 +0100 Subject: [PATCH] Add parameter to do strToBytes in BlockTable and fix some errors with string and int --- display.py | 37 ++++++++++++------------------ iwla.py | 28 +++++++++++----------- plugins/display/all_visits.py | 4 ++-- plugins/display/filter_users.py | 4 ++-- plugins/display/hours_stats.py | 8 +++---- plugins/display/robot_bandwidth.py | 8 +++---- plugins/display/top_visitors.py | 5 ++-- 7 files changed, 42 insertions(+), 52 deletions(-) diff --git a/display.py b/display.py index f48022f..08f1f51 100644 --- a/display.py +++ b/display.py @@ -98,13 +98,14 @@ class DisplayHTMLBlock(DisplayHTMLRaw): 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) self.cols = listToStr(cols) self.rows = [] self.cols_cssclasses = [u''] * len(cols) self.rows_cssclasses = [] self.table_css = u'iwla_table' + self.human_readable_cols = human_readable_cols or [] def appendRow(self, row): self.rows.append(listToStr(row)) @@ -226,6 +227,8 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock): html += u'' for j in range(0, len(row)): v = row[j] + if j in self.human_readable_cols: + v = bytesToStr(v) style = self.getCellCSSClass(i, j) if style: style = u' class="%s"' % (style) html += u'%s' % (style, v) @@ -238,22 +241,19 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock): class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): - 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) + def __init__(self, iwla, title, cols, short_titles=None, nb_valid_rows=0, graph_cols=None, + 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 = listToStr(self.short_titles) self.nb_valid_rows = nb_valid_rows self.icon_path = self.iwla.getConfValue('icon_path', '/') - self.raw_rows = [] self.maxes = [0] * len(cols) self.table_graph_css = u'iwla_graph_table' self.td_img_css = u'iwla_td_img' 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): self.short_titles.append(short_title) @@ -265,13 +265,12 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): def _computeMax(self): 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)): - try: - if row[j] > self.maxes[j]: - self.maxes[j] = row[j] - except: + if type(row[j]) != int: continue + if row[j] > self.maxes[j]: + self.maxes[j] = row[j] def _getIconFromStyle(self, style): if style.startswith(u'iwla_page'): icon = u'vp.png' @@ -302,7 +301,7 @@ class DisplayHTMLBlockTableWithGraph(DisplayHTMLBlockTable): if style: style = u' class="%s"' % (style) alt = u'%s: %s' % (row[j], self.cols[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: height = 1 html += u'' % (style, icon, height, alt, alt) @@ -427,20 +426,14 @@ class DisplayHTMLBuild(object): def bytesToStr(_bytes): 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)): if _bytes < 1024: break _bytes /= 1024.0 if i: - return u'%.02f%s' % (_bytes, suffixes[i]) + return '%.02f%s' % (_bytes, suffixes[i]) else: - return u'%d%s' % (_bytes, suffixes[i]) + return '%d%s' % (_bytes, suffixes[i]) def _toStr(v): return v diff --git a/iwla.py b/iwla.py index 0f54fca..8c70ecc 100755 --- a/iwla.py +++ b/iwla.py @@ -449,7 +449,7 @@ class IWLA(object): page.appendBlock(link) _, 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']) nb_visits = 0 nb_days = 0 @@ -465,8 +465,10 @@ class IWLA(object): else: row = [full_day, 0, 0, 0, 0, 0] days.appendRow(row) - days.setCellValue(i-1, 4, bytesToStr(row[4])) - days.setCellValue(i-1, 5, bytesToStr(row[5])) + viewed_bandwidth = row[4] + not_viewed_bandwidth = row[5] + days.setCellValue(i-1, 4, viewed_bandwidth) + days.setCellValue(i-1, 5, not_viewed_bandwidth) days.appendShortTitle(day) adate = date(cur_time.tm_year, cur_time.tm_mon, i) week_day = adate.weekday() @@ -487,13 +489,9 @@ class IWLA(object): average_row = list(map(lambda v: 0, row)) average_row[0] = self._('Average') - average_row[4] = bytesToStr(average_row[4]) - average_row[5] = bytesToStr(average_row[5]) days.appendRow(average_row) row[0] = self._('Total') - row[4] = bytesToStr(row[4]) - row[5] = bytesToStr(row[5]) days.appendRow(row) page.appendBlock(days) self.display.addPage(page) @@ -504,9 +502,9 @@ class IWLA(object): 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')] 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_ = 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']) total = [0] * len(cols) for i in range(1, 13): @@ -522,12 +520,14 @@ class IWLA(object): else: row = [full_month, 0, 0, 0, 0, 0, 0, ''] months.appendRow(row) - months.setCellValue(i-1, 5, bytesToStr(row[5])) - months.setCellValue(i-1, 6, bytesToStr(row[6])) + viewed_bandwidth = row[5] + not_viewed_bandwidth = row[6] + months.setCellValue(i-1, 5, viewed_bandwidth) + months.setCellValue(i-1, 6, not_viewed_bandwidth) months.appendShortTitle(month) months_.appendRow(row[:-1]) - months_.setCellValue(i-1, 5, bytesToStr(row[5])) - months_.setCellValue(i-1, 6, bytesToStr(row[6])) + months_.setCellValue(i-1, 5, viewed_bandwidth) + months_.setCellValue(i-1, 6, not_viewed_bandwidth) months_.appendShortTitle(month) if year == cur_time.tm_year and i == cur_time.tm_mon: css = months.getCellCSSClass(i-1, 0) @@ -537,8 +537,6 @@ class IWLA(object): months_.setCellCSSClass(i-1, 0, css) total[0] = self._('Total') - total[5] = bytesToStr(total[5]) - total[6] = bytesToStr(total[6]) total[7] = u'' months.appendRow(total) page.appendBlock(months) diff --git a/plugins/display/all_visits.py b/plugins/display/all_visits.py index d1b5b72..b957213 100644 --- a/plugins/display/all_visits.py +++ b/plugins/display/all_visits.py @@ -67,7 +67,7 @@ class IWLADisplayAllVisits(IPlugin): path = self.iwla.getCurDisplayPath(filename) 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', '']) for super_hit in last_access: @@ -80,7 +80,7 @@ class IWLADisplayAllVisits(IPlugin): address, super_hit['viewed_pages'][0], super_hit['viewed_hits'][0], - bytesToStr(super_hit['bandwidth'][0]), + super_hit['bandwidth'][0], time.asctime(super_hit['last_access']) ] table.appendRow(row) diff --git a/plugins/display/filter_users.py b/plugins/display/filter_users.py index 92bdbcd..8151106 100644 --- a/plugins/display/filter_users.py +++ b/plugins/display/filter_users.py @@ -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.setColsCSSClass(['iwla_page', '']) 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(): ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip) 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.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit']) 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(): ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip) else: diff --git a/plugins/display/hours_stats.py b/plugins/display/hours_stats.py index 68dfaac..8b21720 100644 --- a/plugins/display/hours_stats.py +++ b/plugins/display/hours_stats.py @@ -70,19 +70,19 @@ class IWLADisplayHoursStats(IPlugin): # 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')] - 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']) for i in range(0,7): 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) # By Hours title = self.iwla._(u'By Hours') 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']) for i in range(0,24): 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) diff --git a/plugins/display/robot_bandwidth.py b/plugins/display/robot_bandwidth.py index 3e10f1c..d350246 100644 --- a/plugins/display/robot_bandwidth.py +++ b/plugins/display/robot_bandwidth.py @@ -78,7 +78,7 @@ class IWLADisplayRobotBandwidth(IPlugin): path = self.iwla.getCurDisplayPath(filename) 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', '']) for (super_hit, bandwidth) in bandwidths: address = super_hit['remote_addr'] @@ -88,7 +88,7 @@ class IWLADisplayRobotBandwidth(IPlugin): row = [ address, - bytesToStr(bandwidth), + bandwidth, time.asctime(super_hit['last_access']) ] table.appendRow(row) @@ -103,7 +103,7 @@ class IWLADisplayRobotBandwidth(IPlugin): # Top in index 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', '']) for (super_hit, bandwidth) in bandwidths[:10]: @@ -114,7 +114,7 @@ class IWLADisplayRobotBandwidth(IPlugin): row = [ address, - bytesToStr(bandwidth), + bandwidth, time.asctime(super_hit['last_access']) ] table.appendRow(row) diff --git a/plugins/display/top_visitors.py b/plugins/display/top_visitors.py index 06760d7..3cc72eb 100644 --- a/plugins/display/top_visitors.py +++ b/plugins/display/top_visitors.py @@ -69,7 +69,7 @@ class IWLADisplayTopVisitors(IPlugin): top_visitors = [hits[h[0]] for h in top_bandwidth[:10]] 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', '']) for super_hit in top_visitors: address = super_hit['remote_addr'] @@ -81,7 +81,7 @@ class IWLADisplayTopVisitors(IPlugin): address, super_hit['viewed_pages'][0], super_hit['viewed_hits'][0], - bytesToStr(super_hit['bandwidth'][0]), + super_hit['bandwidth'][0], time.asctime(super_hit['last_access']) ] total[1] -= super_hit['viewed_pages'][0] @@ -90,7 +90,6 @@ class IWLADisplayTopVisitors(IPlugin): table.appendRow(row) if total[1] or total[2] or total[3]: total[0] = self.iwla._(u'Others') - total[3] = bytesToStr(total[3]) total[4] = '' table.appendRow(total) table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')