Display works

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

88
iwla.py
View File

@ -16,6 +16,7 @@ meta_visit = {}
analyse_started = False
current_visits = {}
cache_plugins = {}
display = {}
log_format = '$server_name:$server_port $remote_addr - $remote_user [$time_local] ' +\
'"$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']
viewed_http_codes = [200]
PRE_HOOK_DIRECTORY = './hooks/pre_analysis/*.py'
POST_HOOK_DIRECTORY = './hooks/post_analysis/*.py'
HOOKS_ROOT = './hooks/'
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/'
DISPLAY_ROOT = './output/'
META_PATH = DB_ROOT + 'meta.db'
DB_FILENAME = 'iwla.db'
@ -48,9 +52,21 @@ def createEmptyVisits():
return visits
def createEmptyMeta():
meta = {'last_time':None}
meta = {'last_time' : None}
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):
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)
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):
stats = {}
@ -211,6 +289,8 @@ def generateStats(visits):
return stats
def generateMonthStats():
display = createEmptyDisplay()
visits = current_visits['visits']
stats = generateStats(visits)
@ -232,6 +312,8 @@ def generateMonthStats():
serialize(current_visits, path)
generateDisplay()
def generateDayStats():
visits = current_visits['visits']