Commit 64dcab76 authored by Vincent Pelletier's avatar Vincent Pelletier

Move output generation out of main.

To plug more output formats.
parent 24b0a51c
......@@ -634,6 +634,99 @@ period_parser = {
),
}
def asHTML(out, per_site, args, period_parameter_dict, stats):
period = period_parameter_dict['period']
decimator = period_parameter_dict['decimator']
date_format = period_parameter_dict['date_format']
placeholder_delta = period_parameter_dict['placeholder_delta']
graph_period = period_parameter_dict['graph_period']
out.write('<!DOCTYPE html>\n<html><head><meta charset="utf-8">'
'<title>Stats</title>')
js_embed = getattr(args, 'js_embed', True)
js_path = getattr(args, 'js', None)
if js_embed:
out.write('<style>')
out.write(getResource('apachedex.css'))
out.write('</style>')
else:
out.write('<link rel="stylesheet" type="text/css" '
'href="%s/apachedex.css"/>' % js_path)
for script in ('jquery.js', 'jquery.flot.js', 'jquery.flot.time.js',
'jquery.flot.axislabels.js', 'jquery-ui.js', 'apachedex.js'):
if js_embed:
out.write('<script type="text/javascript">//<![CDATA[\n')
out.write(getResource(script))
out.write('\n//]]></script>')
else:
out.write('<script type="text/javascript" src="%s/%s"></script>' % (
js_path, script))
out.write('</head><body><h1>Overall</h1><h2>Parameters</h2>'
'<table class="stats">')
for caption, value in (
('apdex threshold', '%.2fs' % args.apdex),
('period', args.period or (period + ' (auto)')),
):
out.write('<tr><th class="text">%s</th><td>%s</td></tr>' % (
caption, value))
out.write('</table><h2>Hits per period</h2><table class="stats">'
'<tr><th>date</th><th>hits</th></tr>')
hit_per_day = defaultdict(int)
for site_data in per_site.itervalues():
for date, _, hit in site_data.getApdexData():
hit_per_day[decimator(date)] += hit
for date, hit in sorted(hit_per_day.iteritems(), key=ITEMGETTER0):
out.write('<tr><td>%s</td><td>%s</td></tr>' % (date, hit))
out.write('</table>')
for site_id, data in per_site.iteritems():
out.write('<h1>Site: %s</h1>' % site_id)
out.write(
graphPair(
prepareDataForGraph(
data.getApdexData(),
date_format,
placeholder_delta,
),
date_format,
graph_period,
)
)
out.write(data.asHTML(date_format, placeholder_delta, graph_period,
decimator))
end_stat_time = time.time()
if args.stats:
out.write('<h1>Parsing stats</h1><table class="stats">')
buildno, builddate = platform.python_build()
end_parsing_time = stats['end_parsing_time']
parsing_time = end_parsing_time - stats['start_time']
all_lines = stats['all_lines']
for caption, value in (
('Execution date', datetime.now().isoformat()),
('Interpreter', '%s %s build %s (%s)' % (
platform.python_implementation(),
platform.python_version(),
buildno,
builddate,
)),
('File count', stats['file_count']),
('Lines', all_lines),
('... malformed', stats['malformed_lines']),
('... URL-less', stats['no_url_lines']),
('... skipped (URL)', stats['skipped_lines']),
('... skipped (user agent)', stats['skipped_user_agent']),
('Parsing time', timedelta(seconds=parsing_time)),
('Parsing rate', '%i line/s' % (all_lines / parsing_time)),
('Rendering time', timedelta(seconds=(
end_stat_time - end_parsing_time))),
):
out.write('<tr><th class="text">%s</th><td>%s</td></tr>' % (
caption, value))
out.write('</table>')
out.write('</body></html>')
format_generator = {
'html': asHTML,
}
def main():
global abs_file_container
parser = argparse.ArgumentParser(description='Compute Apdex out of '
......@@ -846,84 +939,22 @@ def main():
else:
out = open(args.out, 'w')
with out:
out.write('<!DOCTYPE html>\n<html><head><meta charset="utf-8">'
'<title>Stats</title>')
if getattr(args, 'js_embed', True):
out.write('<style>')
out.write(getResource('apachedex.css'))
out.write('</style>')
else:
out.write('<link rel="stylesheet" type="text/css" '
'href="%s/apachedex.css"/>' % args.js)
for script in ('jquery.js', 'jquery.flot.js', 'jquery.flot.time.js',
'jquery.flot.axislabels.js', 'jquery-ui.js', 'apachedex.js'):
if getattr(args, 'js_embed', True):
out.write('<script type="text/javascript">//<![CDATA[\n')
out.write(getResource(script))
out.write('\n//]]></script>')
else:
out.write('<script type="text/javascript" src="%s/%s"></script>' % (
args.js, script))
out.write('</head><body><h1>Overall</h1><h2>Parameters</h2>'
'<table class="stats">')
for caption, value in (
('apdex threshold', '%.2fs' % args.apdex),
('period', args.period or (period + ' (auto)')),
):
out.write('<tr><th class="text">%s</th><td>%s</td></tr>' % (
caption, value))
out.write('</table><h2>Hits per period</h2><table class="stats">'
'<tr><th>date</th><th>hits</th></tr>')
hit_per_day = defaultdict(int)
for site_data in per_site.itervalues():
for date, _, hit in site_data.getApdexData():
hit_per_day[decimator(date)] += hit
for date, hit in sorted(hit_per_day.iteritems(), key=ITEMGETTER0):
out.write('<tr><td>%s</td><td>%s</td></tr>' % (date, hit))
out.write('</table>')
for site_id, data in per_site.iteritems():
out.write('<h1>Site: %s</h1>' % site_id)
out.write(
graphPair(
prepareDataForGraph(
data.getApdexData(),
date_format,
placeholder_delta,
),
date_format,
graph_period,
)
)
out.write(data.asHTML(date_format, placeholder_delta, graph_period,
decimator))
end_stat_time = time.time()
if args.stats:
out.write('<h1>Parsing stats</h1><table class="stats">')
buildno, builddate = platform.python_build()
parsing_time = end_parsing_time - start_time
for caption, value in (
('Execution date', datetime.now().isoformat()),
('Interpreter', '%s %s build %s (%s)' % (
platform.python_implementation(),
platform.python_version(),
buildno,
builddate,
)),
('File count', file_count),
('Lines', all_lines),
('... malformed', malformed_lines),
('... URL-less', no_url_lines),
('... skipped (URL)', skipped_lines),
('... skipped (user agent)', skipped_user_agent),
('Parsing time', timedelta(seconds=parsing_time)),
('Parsing rate', '%i line/s' % (all_lines / parsing_time)),
('Rendering time', timedelta(seconds=(
end_stat_time - end_parsing_time))),
):
out.write('<tr><th class="text">%s</th><td>%s</td></tr>' % (
caption, value))
out.write('</table>')
out.write('</body></html>')
format_generator[args.format](out, per_site, args, {
'period': period,
'decimator': decimator,
'date_format': date_format,
'placeholder_delta': placeholder_delta,
'graph_period': graph_period,
}, {
'start_time': start_time,
'end_parsing_time': end_parsing_time,
'file_count': file_count,
'all_lines': all_lines,
'malformed_lines': malformed_lines,
'no_url_lines': no_url_lines,
'skipped_lines': skipped_lines,
'skipped_user_agent': skipped_user_agent,
})
if __name__ == '__main__':
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment