Commit 57731c74 authored by Vincent Pelletier's avatar Vincent Pelletier

Make user agent section optional.

parent 35aaa2c9
......@@ -361,7 +361,8 @@ _APDEXDateDictAsJSONState = lambda date_dict: dict(((y, z.asJSONState())
for y, z in date_dict.iteritems()))
class GenericSiteStats(object):
def __init__(self, threshold, getDuration, suffix, error_detail=False):
def __init__(self, threshold, getDuration, suffix, error_detail=False,
user_agent_detail=False):
self.threshold = threshold
self.suffix = suffix
self.error_detail = error_detail
......@@ -371,6 +372,7 @@ class GenericSiteStats(object):
self.error_url_count = defaultdict(partial(defaultdict, Counter))
self.url_apdex = defaultdict(partial(APDEXStats, threshold, getDuration))
self.apdex = defaultdict(partial(APDEXStats, threshold, getDuration))
self.user_agent_detail = user_agent_detail
self.user_agent_counter = Counter()
def rescale(self, convert, getDuration):
......@@ -397,6 +399,7 @@ class GenericSiteStats(object):
if self.error_detail and statusIsError(status):
# XXX: can eat memory if there are many errors on many different urls
self.error_url_count[status][url][match.group('referer')] += 1
if self.user_agent_detail:
self.user_agent_counter[match.group('agent')] += 1
def getApdexData(self):
......@@ -426,6 +429,7 @@ class GenericSiteStats(object):
append(data.asHTML(self.threshold))
append('<td class="text">%s</td></tr>' % unquoteToHtml(url))
append('</table>')
if self.user_agent_detail:
append('<h2>User agents</h2><table class="stats"><tr><th>hits</th>'
'<th>user agent</th></tr>')
for user_agent, hit in self.user_agent_counter.most_common(N_USER_AGENT):
......@@ -498,7 +502,8 @@ class GenericSiteStats(object):
@classmethod
def fromJSONState(cls, state, getDuration, suffix):
error_detail = state['error_detail']
result = cls(state['threshold'], getDuration, suffix, error_detail)
result = cls(state['threshold'], getDuration, suffix, error_detail,
state.get('user_agent_detail', True))
if error_detail:
error_url_count = result.error_url_count
for state_status, state_url_dict in state['error_url_count'].iteritems():
......@@ -524,10 +529,12 @@ class GenericSiteStats(object):
'apdex': _APDEXDateDictAsJSONState(self.apdex),
'status': self.status,
'user_agent_counter': self.user_agent_counter,
'user_agent_detail': self.user_agent_detail,
}
def accumulateFrom(self, other):
# XXX: ignoring: threshold, getDuration, suffix, error_detail.
# XXX: ignoring: threshold, getDuration, suffix, error_detail,
# user_agent_detail.
# Assuming they are consistently set.
if self.error_detail:
for status, other_url_dict in other.error_url_count.iteritems():
......@@ -554,9 +561,10 @@ class ERP5SiteStats(GenericSiteStats):
- If a line belongs to a module and has at least 2 slashes after module,
count line as belonging to a document of that module
"""
def __init__(self, threshold, getDuration, suffix, error_detail=False):
def __init__(self, threshold, getDuration, suffix, error_detail=False,
user_agent_detail=False):
super(ERP5SiteStats, self).__init__(threshold, getDuration, suffix,
error_detail=error_detail)
error_detail=error_detail, user_agent_detail=user_agent_detail)
# Key levels:
# - module id (string)
# - is document (bool)
......@@ -1188,6 +1196,9 @@ def main():
'Default: %(default).2fs')
group.add_argument('-e', '--error-detail', action='store_true',
help='Include detailed report (url & referers) for error statuses.')
group.add_argument('-u', '--user-agent-detail', action='store_true',
help='Include report of most frequent user agents.')
group.add_argument('-f', '--format', choices=format_generator,
default='html', help='Format in which output should be generated.')
group.add_argument('-p', '--period', choices=period_parser,
......@@ -1307,6 +1318,7 @@ def main():
quiet = args.quiet
threshold = args.apdex
error_detail = args.error_detail
user_agent_detail = args.user_agent_detail
file_count = len(infile_list)
per_site = {}
if '-' in args.state_file and '-' in infile_list:
......@@ -1437,7 +1449,7 @@ def main():
site_data = per_site[site]
except KeyError:
site_data = per_site[site] = action(threshold, getDuration,
error_detail=error_detail)
error_detail=error_detail, user_agent_detail=user_agent_detail)
try:
site_data.accumulate(match, url_match, hit_date)
except Exception:
......
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