Separate display functions into display.py

This commit is contained in:
Gregory Soutade 2014-11-20 16:31:00 +01:00
parent f3cb04b16c
commit 34aec57c46
2 changed files with 49 additions and 38 deletions

40
display.py Normal file
View File

@ -0,0 +1,40 @@
def createPage(display, filename, title):
page = {}
page['title'] = title;
page['blocks'] = []
display[filename] = page
return page
def appendBlockToPage(page, block):
page['blocks'].append(block)
def createTable(title, cols):
table = {'type' : 'table', 'title' : title}
table['cols'] = cols
table['rows'] = []
def appendRowToTable(table, row):
table['rows'].append(row)
def buildPages(display):
for filename in display.keys():
page = display[filename]
with open(DISPLAY_ROOT + filename, 'w') as f:
f.write('<html><title>%s</title><body>' % (page['title']))
for block in page['blocks']:
if block['type'] == 'html':
f.write(block['value'])
elif block['type'] == 'table':
f.write('<table>')
f.write('<tr>')
for title in block['cols']:
f.write('<th>%s</th>' % (title))
f.write('</tr>')
for row in block['rows']:
f.write('<tr>')
for v in row:
f.write('<td>%s</td>' % (v))
f.write('</tr>')
f.write('</table>')
f.write('</body></html>')

47
iwla.py
View File

@ -8,6 +8,8 @@ import imp
import pickle
import gzip
from display import *
# Default configuration
DB_ROOT = './output/'
@ -85,14 +87,6 @@ def createEmptyDisplay():
display = {}
return display
def createPage(filename, title):
page = {}
page['title'] = title;
page['blocks'] = []
display[filename] = page
return page
def getDBFilename(time):
return (DB_ROOT + '%d/%d_%s') % (time.tm_year, time.tm_mon, DB_FILENAME)
@ -211,37 +205,14 @@ def decodeTime(hit):
hit['time_decoded'] = time.strptime(t, time_format)
def buildPages():
for filename in display.keys():
page = display[filename]
with open(DISPLAY_ROOT + filename, 'w') as f:
f.write('<html><title>%s</title><body>' % (page['title']))
for block in page['blocks']:
if block['type'] == 'html':
f.write(block['value'])
elif block['type'] == 'table':
f.write('<table>')
f.write('<tr>')
for title in block['cols']:
f.write('<th>%s</th>' % (title))
f.write('</tr>')
for row in block['rows']:
f.write('<tr>')
for v in row:
f.write('<td>%s</td>' % (v))
f.write('</tr>')
f.write('</table>')
f.write('</body></html>')
def generateDisplayDaysStat():
cur_time = meta_visit['last_time']
title = 'Stats %d/%d' % (cur_time.tm_mon, cur_time.tm_year)
filename = '%d/index_%d.html' % (cur_time.tm_year, cur_time.tm_mon)
page = createPage(filename, title)
page = createPage(display, filename, title)
days = {'type' : 'table', 'title' : 'By day'}
days['cols'] = ['Day', 'Visits', 'Pages', 'Hits', 'Bandwith', 'Robot Bandwith']
days['rows'] = []
days = createTable('By day', ['Day', 'Visits', 'Pages', 'Hits', 'Bandwith', 'Robot Bandwith'])
keys = current_visits['days_stats'].keys()
keys.sort()
nb_visits = 0
@ -249,7 +220,7 @@ def generateDisplayDaysStat():
stats = current_visits['days_stats'][k]
row = [k, stats['nb_visitors'], stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']]
row = map(lambda(v): str(v), row)
days['rows'].append(row)
appendRowToTable(days, row)
nb_visits += stats['nb_visitors']
stats = current_visits['month_stats']
@ -262,12 +233,12 @@ def generateDisplayDaysStat():
row = map(lambda(v): '0', row)
row[0] = 'Average'
days['rows'].append(row)
appendRowToTable(days, row)
row = ['Total', nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']]
row = map(lambda(v): str(v), row)
days['rows'].append(row)
page['blocks'].append(days)
appendRowToTable(days, row)
appendBlockToPage(page, days)
def generateDisplay():
generateDisplayDaysStat()