Display works

This commit is contained in:
Gregory Soutade 2014-11-20 11:50:06 +01:00
parent 8ba8c99b3c
commit 7bd5a42962

86
iwla.py
View File

@ -16,6 +16,7 @@ meta_visit = {}
analyse_started = False analyse_started = False
current_visits = {} current_visits = {}
cache_plugins = {} cache_plugins = {}
display = {}
log_format = '$server_name:$server_port $remote_addr - $remote_user [$time_local] ' +\ log_format = '$server_name:$server_port $remote_addr - $remote_user [$time_local] ' +\
'"$request" $status $body_bytes_sent ' +\ '"$request" $status $body_bytes_sent ' +\
@ -33,9 +34,12 @@ uri_re = re.compile(r'(?P<extract_uri>[^\?]*)[\?(?P<extract_parameters>.*)]?')
pages_extensions = ['/', 'html', 'xhtml', 'py', 'pl', 'rb', 'php'] pages_extensions = ['/', 'html', 'xhtml', 'py', 'pl', 'rb', 'php']
viewed_http_codes = [200] viewed_http_codes = [200]
PRE_HOOK_DIRECTORY = './hooks/pre_analysis/*.py' HOOKS_ROOT = './hooks/'
POST_HOOK_DIRECTORY = './hooks/post_analysis/*.py' PRE_HOOK_DIRECTORY = HOOKS_ROOT + 'pre_analysis/*.py'
POST_HOOK_DIRECTORY = HOOKS_ROOT + 'post_analysis/*.py'
DISPLAY_HOOK_DIRECTORY = HOOKS_ROOT + 'display/*.py'
DB_ROOT = './output/' DB_ROOT = './output/'
DISPLAY_ROOT = './output/'
META_PATH = DB_ROOT + 'meta.db' META_PATH = DB_ROOT + 'meta.db'
DB_FILENAME = 'iwla.db' DB_FILENAME = 'iwla.db'
@ -51,6 +55,18 @@ def createEmptyMeta():
meta = {'last_time' : None} meta = {'last_time' : None}
return meta return meta
def createEmptyDisplay():
display = {}
return display
def createPage(filename, title):
page = {}
page['title'] = title;
page['blocks'] = []
display[filename] = page
return page
def getDBFilename(time): def getDBFilename(time):
return (DB_ROOT + '%d/%d_%s') % (time.tm_year, time.tm_mon, DB_FILENAME) return (DB_ROOT + '%d/%d_%s') % (time.tm_year, time.tm_mon, DB_FILENAME)
@ -180,6 +196,68 @@ def decodeTime(hit):
hit['time_decoded'] = time.strptime(t, time_format) 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)
days = {'type' : 'table', 'title' : 'By day'}
days['cols'] = ['Day', 'Visits', 'Pages', 'Hits', 'Bandwith', 'Robot Bandwith']
days['rows'] = []
keys = current_visits['days_stats'].keys()
keys.sort()
nb_visits = 0
for k in keys:
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)
nb_visits += stats['nb_visitors']
stats = current_visits['month_stats']
nb_days = len(keys)
row = [0, nb_visits, stats['viewed_pages'], stats['viewed_hits'], stats['viewed_bandwidth'], stats['not_viewed_bandwidth']]
if nb_days:
row = map(lambda(v): str(int(v/nb_days)), row)
else:
row = map(lambda(v): '0', row)
row[0] = 'Average'
days['rows'].append(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)
def generateDisplay():
generateDisplayDaysStat()
callPlugins(DISPLAY_HOOK_DIRECTORY, current_visits, display)
buildPages()
def generateStats(visits): def generateStats(visits):
stats = {} stats = {}
@ -211,6 +289,8 @@ def generateStats(visits):
return stats return stats
def generateMonthStats(): def generateMonthStats():
display = createEmptyDisplay()
visits = current_visits['visits'] visits = current_visits['visits']
stats = generateStats(visits) stats = generateStats(visits)
@ -232,6 +312,8 @@ def generateMonthStats():
serialize(current_visits, path) serialize(current_visits, path)
generateDisplay()
def generateDayStats(): def generateDayStats():
visits = current_visits['visits'] visits = current_visits['visits']