Bold percent cells and add percentage sign

This commit is contained in:
Brian Read 2024-06-15 11:58:08 +01:00
parent cfe5d57656
commit 1917053811
3 changed files with 35 additions and 28 deletions

View File

@ -4,6 +4,12 @@ table {
} }
tr.row-total, tr.row-percent , td.col-15, td.col-16 {
font-weight: bold;
}
tr,td,th { tr,td,th {
border:1px solid; border:1px solid;
} }

View File

@ -52,11 +52,11 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr tal:repeat="row array_2d"> <tr tal:repeat="row array_2d" tal:attributes="class python: 'row-total' if repeat.row.index == 24 else 'row-percent' if repeat.row.index == 25 else None">
<td tal:condition="repeat.row.index == 24" tal:content="'TOTALS'">Totals</td> <td tal:condition="repeat.row.index == 24" tal:attributes="class python:'col-total'" tal:content="'TOTALS'">Totals</td>
<td tal:condition="repeat.row.index == 25" tal:content="'PERCENT'">Percent</td> <td tal:condition="repeat.row.index == 25" tal:attributes="class python:'col-percent'" tal:content="'PERCENT'">Percent</td>
<td tal:condition="repeat.row.index < 24" tal:content="string:${reporting_date}, ${repeat.row.index}">Hour</td> <td tal:condition="repeat.row.index < 24" tal:content="string:${reporting_date}, ${repeat.row.index}">Hour</td>
<td tal:repeat="cell row" tal:content="cell">Cell</td> <td tal:repeat="cell row" tal:attributes="class python: 'col-' + str(repeat.cell.index)" tal:content="cell">Cell</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -45,24 +45,25 @@ html_page_dir = data_file_path+"/opt/mailstats/html/"
template_dir = data_file_path+"/opt/mailstats/templates/" template_dir = data_file_path+"/opt/mailstats/templates/"
logs_dir = data_file_path+"/opt/mailstats/logs/" logs_dir = data_file_path+"/opt/mailstats/logs/"
# Column numbering # Column numbering (easy to renumber or add one in)
Hour = 0 Hour = 0
WebMail = 1 WebMail = Hour + 1
Local = 2 Local = WebMail + 1
MailMan = 3 MailMan = Local + 1
Relay = 4 Relay = MailMan + 1
DMARC = 5 DMARC = Relay + 1
Virus = 6 Virus = DMARC + 1
RBLDNS = 7 RBLDNS = Virus + 1
Geoip = 8 Geoip = RBLDNS + 1
NonConf = 9 NonConf = Geoip + 1
RejLoad = 10 RejLoad = NonConf + 1
Karma = 11 Karma = RejLoad + 1
DelSpam = 12 DelSpam = Karma + 1
QuedSpam = 13 QuedSpam = DelSpam + 1
Ham = 14 Ham = QuedSpam + 1
TOTALS = 15 TOTALS = Ham + 1
PERCENT = 16 PERCENT = TOTALS + 1
ColTotals = 24 ColTotals = 24
ColPercent = 25 ColPercent = 25
@ -452,7 +453,7 @@ def render_sub_table(table_title,table_headers,found_values):
total_sum = sum(found_values.values()) total_sum = sum(found_values.values())
# and add in list with second element the percentage # and add in list with second element the percentage
# Create a list of tuples with each tuple containing (key, value, percentage) # Create a list of tuples with each tuple containing (key, value, percentage)
sub_result = [(key, value, (round(round(value / total_sum,4) * 100,2))) for key, value in found_values.items()] sub_result = [(key, value, f"{round(value / total_sum * 100, 2)}%") for key, value in found_values.items()]
sub_result.sort(key=lambda x: x[2], reverse=True) # Sort by percentage in descending order sub_result.sort(key=lambda x: x[2], reverse=True) # Sort by percentage in descending order
@ -834,23 +835,23 @@ if __name__ == "__main__":
# Compute percentages # Compute percentages
total_Count = columnCounts_2d[ColTotals][TOTALS] total_Count = columnCounts_2d[ColTotals][TOTALS]
#Column of percentages #Column of percentages
for row in range(24): for row in range(ColTotals):
if total_Count == 0: if total_Count == 0:
percentage_of_total = 0 percentage_of_total = 0
else: else:
percentage_of_total = round(round(columnCounts_2d[row][TOTALS] / total_Count,4) * 100,2) percentage_of_total = f"{round(round(columnCounts_2d[row][TOTALS] / total_Count,4) * 100,1)}%"
columnCounts_2d[row][PERCENT] = percentage_of_total columnCounts_2d[row][PERCENT] = percentage_of_total
#Row of percentages #Row of percentages
for col in range(TOTALS): for col in range(TOTALS):
if total_Count == 0: if total_Count == 0:
percentage_of_total = 0 percentage_of_total = 0
else: else:
percentage_of_total = round(round(columnCounts_2d[ColTotals][col] / total_Count,4) * 100,2) percentage_of_total = f"{round(round(columnCounts_2d[ColTotals][col] / total_Count,4) * 100,1)}%"
columnCounts_2d[ColPercent][col] = percentage_of_total columnCounts_2d[ColPercent][col] = percentage_of_total
# and drop in the 100% to make it look correct! # and drop in the 100% to make it look correct!
columnCounts_2d[ColPercent][PERCENT] = 100 columnCounts_2d[ColPercent][PERCENT] = '100%'
columnCounts_2d[ColTotals][PERCENT] = 100 columnCounts_2d[ColTotals][PERCENT] = '100%'
columnCounts_2d[ColPercent][TOTALS] = 100 columnCounts_2d[ColPercent][TOTALS] = '100%'
# Now scan for the other lines in the log of interest # Now scan for the other lines in the log of interest
found_countries = defaultdict(int) found_countries = defaultdict(int)