Refactor sub table render into def and add geoip results

This commit is contained in:
Brian Read 2024-06-03 17:31:24 +01:00
parent b2440be6d0
commit 5db9ddf82f
2 changed files with 38 additions and 28 deletions

View File

@ -1,5 +1,4 @@
<h2>${title}</h2> <h2>${title}</h2>
<h1>${title}</h1>
<table border="1"> <table border="1">
<thead> <thead>
<tr> <tr>

View File

@ -9,6 +9,10 @@
# #
# Todo # Todo
# 1. Make "yesterday" parameterised # 1. Make "yesterday" parameterised
# 2. Get data from SMEServer DB records
# 3. Other stats
# 4. Percentages for main table
# 5. Percentages and sort for Sub tables
# #
from datetime import datetime, timedelta from datetime import datetime, timedelta
import sys import sys
@ -353,6 +357,26 @@ def split_timestamp_and_data(log_entry: str) -> list:
#print(f"{timestamp} {rest_of_data}") #print(f"{timestamp} {rest_of_data}")
return [timestamp, rest_of_data] return [timestamp, rest_of_data]
def render_sub_table(table_title,table_headers,found_values):
# NeedNOTE: also need to compute the percentages here.
template_path = data_file_path+'mailstats-sub-table.html.pt'
# Load the template
with open(template_path, 'r') as template_file:
template_content = template_file.read()
# Create a Chameleon template instance
try:
template = PageTemplate(template_content)
# Render the template with the 2D array data and column headers
try:
rendered_html = template(array_2d=found_values, column_headers=table_headers, title=table_title)
except Exception as e:
print(f"{table_title}: A chameleon controller render error occurred: {e}")
raise ValueError
except Exception as e:
print(f"{table_title}: A chameleon controller template error occurred: {e}")
raise ValueError
return rendered_html
if __name__ == "__main__": if __name__ == "__main__":
try: try:
@ -557,8 +581,8 @@ if __name__ == "__main__":
# 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)
geoip_pattern = re.compile(r"check_badcountries: GeoIP Country: (.*)") geoip_pattern = re.compile(r".*check_badcountries: GeoIP Country: (.*)")
dmarc_pattern = re.compile(r"dmarc: pass") dmarc_pattern = re.compile(r".*dmarc: pass")
total_countries = 0 total_countries = 0
DMARCOkCount = 0 DMARCOkCount = 0
# Pick up all log_entries = read_yesterday_log_file(data_file) # Pick up all log_entries = read_yesterday_log_file(data_file)
@ -571,18 +595,17 @@ if __name__ == "__main__":
i += 1 i += 1
print_progress_bar(i, sorted_len, prefix='Scanning for sub tables:', suffix='Complete', length=50) print_progress_bar(i, sorted_len, prefix='Scanning for sub tables:', suffix='Complete', length=50)
#Pull out Geoip countries for analysis table #Pull out Geoip countries for analysis table
match = geoip_pattern.match(data) match = geoip_pattern.match(data)
if match: if match:
country = match.group(1) country = match.group(1)
found_countries[country] += 1 found_countries[country] += 1
total_countries += 1 total_countries += 1
break continue
#Pull out DMARC approvals #Pull out DMARC approvals
match = dmarc_pattern.match(data) match = dmarc_pattern.match(data)
if match: if match:
DMARCOkCount += 1 DMARCOkCount += 1
break continue
#Now apply the results to the chameleon template - main table #Now apply the results to the chameleon template - main table
# Path to the template file # Path to the template file
@ -597,31 +620,19 @@ if __name__ == "__main__":
total_html = rendered_html total_html = rendered_html
#Now apply the results to the chameleon template - subservient tables #Now apply the results to the chameleon template - subservient tables
#qpsmtd codes
# Path to the template file qpsmtpd_headers = ["Code",'Count','Percent','Reason']
qpsmtpd_headers = ["Code",'Count'] #,'Percent','Reason']
qpsmtpd_title = 'Qpsmtpd codes league table:' qpsmtpd_title = 'Qpsmtpd codes league table:'
#and found_qpcodes rendered_html = render_sub_table(qpsmtpd_title,qpsmtpd_headers,found_qpcodes)
# Need to compute the percentages here. # Add it to the total
template_path = data_file_path+'mailstats-sub-table.html.pt' total_html = insert_string_after(total_html,rendered_html, "<!---Add in sub tables here -->")
# Load the template
with open(template_path, 'r') as template_file: #Geoip Country codes
template_content = template_file.read() geoip_headers = ['Country','Count','Percent','Rejected?']
# Create a Chameleon template instance geoip_title = 'Geoip results:'
try: rendered_html = render_sub_table(geoip_title,geoip_headers,found_countries)
template = PageTemplate(template_content)
# Render the template with the 2D array data and column headers
try:
rendered_html = template(array_2d=found_qpcodes, column_headers=qpsmtpd_headers, title=qpsmtpd_title)
except Exception as e:
print(f"An chameleon controller render error occurred: {e}")
quit(1)
except Exception as e:
print(f"An chameleon controller template error occurred: {e}")
quit(1)
# Add it to the total # Add it to the total
total_html = insert_string_after(total_html,rendered_html, "<!---Add in sub tables here -->") total_html = insert_string_after(total_html,rendered_html, "<!---Add in sub tables here -->")
# Write the rendered HTML to a file # Write the rendered HTML to a file
output_path = data_file_path+'mailstats_for_'+formatted_yesterday output_path = data_file_path+'mailstats_for_'+formatted_yesterday