Commit 31723edb authored by Vincent Pelletier's avatar Vincent Pelletier

Move graph-related code to top level functions for reuse.

parent fdb023e7
......@@ -126,6 +126,77 @@ def getApdexTableHeader(overall=False):
return '<th>apdex</th><th>hits</th><th>avg (s)</th>' \
'<th%s>max (s)</th>' % extra
def getDataPoints(apdex_dict):
return [
(date, apdex.getApdex() * 100, apdex.hit) for date, apdex
in sorted(apdex_dict.iteritems(), key=ITEMGETTER0)]
def prepareDataForGraph(daily_data, date_format, placeholder_delta):
current_date = datetime.strptime(daily_data[0][0], date_format)
new_daily_data = []
append = new_daily_data.append
for measure in daily_data:
measure_date = datetime.strptime(measure[0], date_format)
while current_date < measure_date:
append((current_date.strftime(date_format), 100, 0))
current_date += placeholder_delta
append(measure)
current_date = measure_date + placeholder_delta
return new_daily_data
def graphPair(daily_data, date_format, graph_period):
date_list = [int(time.mktime(time.strptime(x[0], date_format)) * 1000)
for x in daily_data]
timeformat = '%Y<br/>%m/%d<br/>%H:%M'
# There is room for about 10 labels on the X axis.
minTickSize = (max(1,
(date_list[-1] - date_list[0]) / (60 * 60 * 1000 * 10)), 'hour')
# Guesstimation: 6px per digit. If only em were allowed...
yLabelWidth = max(int(math.log10(max(x[2] for x in daily_data))) + 1,
3) * 6
return graph('apdex',
[zip(date_list, (x[1] for x in daily_data))],
{
'xaxis': {
'mode': 'time',
'timeformat': timeformat,
'minTickSize': minTickSize,
},
'yaxis': {
'max': 100,
'axisLabel': '%',
'labelWidth': yLabelWidth,
},
'lines': {'show': True},
},
) + graph('Hits (per %s)' % graph_period,
[zip(date_list, (x[2] for x in daily_data))],
{
'xaxis': {
'mode': 'time',
'timeformat': timeformat,
'minTickSize': minTickSize,
},
'yaxis': {
'axisLabel': 'Hits',
'labelWidth': yLabelWidth,
'tickDecimals': 0,
},
'lines': {'show': True},
},
)
def graph(title, data, options={}):
result = []
append = result.append
append('<h2>%s</h2><div class="graph" '
'style="width:600px;height:300px" data-points="' % title)
append(escape(json.dumps(data), quote=True))
append('" data-options="')
append(escape(json.dumps(options), quote=True))
append('"></div>')
return ''.join(result)
class APDEXStats(object):
def __init__(self, threshold, getDuration):
threshold *= US_PER_S
......@@ -196,9 +267,7 @@ class GenericSiteStats(object):
self.error_url_count[status][url].append(match.group('referer'))
def getApdexData(self):
return [
(date, apdex.getApdex() * 100, apdex.hit) for date, apdex
in sorted(self.apdex.iteritems(), key=ITEMGETTER0)]
return getDataPoints(self.apdex)
def asHTML(self, stat_filter=lambda x: x):
result = []
......@@ -670,72 +739,21 @@ def main():
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>')
def graph(title, data, options={}):
result = []
append = result.append
append('<h2>%s</h2><div class="graph" '
'style="width:600px;height:300px" data-points="' % title)
append(escape(json.dumps(data), quote=True))
append('" data-options="')
append(escape(json.dumps(options), quote=True))
append('"></div>')
return ''.join(result)
for site_id, data in per_site.iteritems():
out.write('<h1>Site: %s</h1>' % site_id)
daily_data = data.getApdexData()
current_date = datetime.strptime(daily_data[0][0], date_format)
new_daily_data = []
append = new_daily_data.append
for measure in daily_data:
measure_date = datetime.strptime(measure[0], date_format)
while current_date < measure_date:
append((current_date.strftime(date_format), 100, 0))
current_date += placeholder_delta
append(measure)
current_date = measure_date + placeholder_delta
daily_data = new_daily_data
date_list = [int(time.mktime(time.strptime(x[0], date_format)) * 1000)
for x in daily_data]
timeformat = '%Y<br/>%m/%d<br/>%H:%M'
# There is room for about 10 labels on the X axis.
minTickSize = (max(1,
(date_list[-1] - date_list[0]) / (60 * 60 * 1000 * 10)), 'hour')
# Guesstimation: 6px per digit. If only em were allowed...
yLabelWidth = max(int(math.log10(max(x[2] for x in daily_data))) + 1,
3) * 6
out.write(graph('apdex',
[zip(date_list, (x[1] for x in daily_data))],
{
'xaxis': {
'mode': 'time',
'timeformat': timeformat,
'minTickSize': minTickSize,
},
'yaxis': {
'max': 100,
'axisLabel': '%',
'labelWidth': yLabelWidth,
},
'lines': {'show': True},
},
))
out.write(graph('Hits (per %s)' % graph_period,
[zip(date_list, (x[2] for x in daily_data))],
{
'xaxis': {
'mode': 'time',
'timeformat': timeformat,
'minTickSize': minTickSize,
},
'yaxis': {
'axisLabel': 'Hits',
'labelWidth': yLabelWidth,
'tickDecimals': 0,
},
'lines': {'show': True},
},
))
out.write(data.asHTML(decimator))
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">')
......
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