Commit 78534fa5 authored by Hanno Schlichting's avatar Hanno Schlichting

PEP8 fixes

parent f1f04e44
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
############################################################################## ##############################################################################
import time import time
import logging
from thread import allocate_lock from thread import allocate_lock
from Products.PluginIndexes.interfaces import IUniqueValueIndex from Products.PluginIndexes.interfaces import IUniqueValueIndex
...@@ -22,8 +21,6 @@ reports = {} ...@@ -22,8 +21,6 @@ reports = {}
MAX_DISTINCT_VALUES = 10 MAX_DISTINCT_VALUES = 10
LOG = logging.getLogger('CatalogReport')
def determine_value_indexes(catalog): def determine_value_indexes(catalog):
# This function determines all indexes whose values should be respected # This function determines all indexes whose values should be respected
...@@ -45,9 +42,9 @@ def determine_value_indexes(catalog): ...@@ -45,9 +42,9 @@ def determine_value_indexes(catalog):
return frozenset(valueindexes) return frozenset(valueindexes)
def make_key(catalog,request): def make_key(catalog, request):
valueindexes = determine_value_indexes(catalog) valueindexes = determine_value_indexes(catalog)
if isinstance(request, dict): if isinstance(request, dict):
keydict = request.copy() keydict = request.copy()
else: else:
...@@ -56,7 +53,7 @@ def make_key(catalog,request): ...@@ -56,7 +53,7 @@ def make_key(catalog,request):
if isinstance(request.request, dict): if isinstance(request.request, dict):
keydict.update(request.request) keydict.update(request.request)
key = keys = keydict.keys() key = keys = keydict.keys()
values = [name for name in keys if name in valueindexes] values = [name for name in keys if name in valueindexes]
if values: if values:
# If we have indexes whose values should be considered, we first # If we have indexes whose values should be considered, we first
...@@ -66,30 +63,24 @@ def make_key(catalog,request): ...@@ -66,30 +63,24 @@ def make_key(catalog,request):
for name in values: for name in values:
v = keydict.get(name, []) v = keydict.get(name, [])
if type(v) in [type(tuple()),type(list())]: if type(v) in [type(tuple()), type(list())]:
v = list(v) v = list(v)
v.sort() v.sort()
# We need to make sure the key is immutable, repr() is an easy way # We need to make sure the key is immutable, repr() is an easy way
# to do this without imposing restrictions on the types of values # to do this without imposing restrictions on the types of values
key.append((name, repr(v))) key.append((name, repr(v)))
key = tuple(sorted(key)) key = tuple(sorted(key))
return key return key
#
#
#
#######################################################################
class StopWatch(object): class StopWatch(object):
""" Simple stopwatch class """ """ Simple stopwatch class """
def __init__(self): def __init__(self):
self.init() self.init()
def init(self): def init(self):
self.res = [] self.res = []
self.start_time = None self.start_time = None
...@@ -100,123 +91,112 @@ class StopWatch(object): ...@@ -100,123 +91,112 @@ class StopWatch(object):
self.init() self.init()
self.start_time = time.time() self.start_time = time.time()
def split(self,label): def split(self, label):
current = time.time() current = time.time()
start_time,stop_time = self.interim.get(label,(None,None)) start_time, stop_time = self.interim.get(label, (None, None))
if start_time is None: if start_time is None:
self.interim[label] = (current,None) self.interim[label] = (current, None)
return return
self.interim[label] = (start_time,current) self.interim[label] = (start_time, current)
self.res.append((label, current - start_time)) self.res.append((label, current - start_time))
def stop(self): def stop(self):
self.end_time = time.time() self.end_time = time.time()
def result(self): def result(self):
return (self.end_time-self.start_time,tuple(self.res)) return (self.end_time - self.start_time, tuple(self.res))
class CatalogReport(StopWatch): class CatalogReport(StopWatch):
""" catalog report class to meassure and to identify slow catalog queries """ """Catalog report class to meassure and identify catalog queries.
"""
def __init__(self, catalog, request=None, threshold=0): def __init__(self, catalog, request=None, threshold=0):
super(CatalogReport,self).__init__() super(CatalogReport, self).__init__()
self.catalog = catalog self.catalog = catalog
self.request = request self.request = request
self.threshold = threshold self.threshold = threshold
## TODO: how to get an unique id? # TODO: how to get an unique id?
getPhysicalPath = getattr(catalog.aq_parent,'getPhysicalPath',lambda: ['','DummyCatalog']) getPhysicalPath = getattr(catalog.aq_parent,
'getPhysicalPath',
lambda: ['', 'DummyCatalog'])
self.cid = tuple(getPhysicalPath()) self.cid = tuple(getPhysicalPath())
def stop(self): def stop(self):
super(CatalogReport,self).stop() super(CatalogReport, self).stop()
self.log() self.log()
def log(self):
# query key def log(self):
key = make_key(self.catalog,self.request) key = make_key(self.catalog, self.request)
# result of stopwatch # result of stopwatch
res = self.result() res = self.result()
if res[0] < self.threshold: if res[0] < self.threshold:
return return
reportlock.acquire() reportlock.acquire()
try: try:
if not reports.has_key(self.cid): if self.cid not in reports:
reports[self.cid] = {} reports[self.cid] = {}
previous = reports[self.cid].get(key) previous = reports[self.cid].get(key)
if previous: if previous:
counter , mean, last = previous counter, mean, last = previous
mean = (mean*counter + res[0])/float(counter+1) mean = (mean * counter + res[0]) / float(counter + 1)
reports[self.cid][key] = (counter+1,mean,res) reports[self.cid][key] = (counter + 1, mean, res)
else: else:
reports[self.cid][key] = (1,res[0],res) reports[self.cid][key] = (1, res[0], res)
finally: finally:
reportlock.release() reportlock.release()
def reset(self): def reset(self):
reportlock.acquire() reportlock.acquire()
try: try:
reports[self.cid] = {} reports[self.cid] = {}
finally: finally:
reportlock.release() reportlock.release()
def report(self): def report(self):
""" """Returns a statistic report of catalog queries as list of dicts as
returns a statistic report of catalog queries as list of dicts as follows: follows:
[ { 'query': <query_key>, [{'query': <query_key>,
'counter': <hits> 'counter': <hits>
'duration': <mean duration>, 'duration': <mean duration>,
'last': <details of recent query>, 'last': <details of recent query>,
}, },
...
...,
] ]
<details of recent query> := { 'duration': <duration of last query>, <details of recent query> := {'duration': <duration of last query>,
'details': <duration of single indexes>, 'details': <duration of single indexes>,
} }
<duration of single indexes> := [{'id': <index_name1>,
<duration of single indexes> := [ { 'id': <index_name1>, 'duration': <duration>,
'duration': <duration>, },
}, ...
...
] ]
scale unit of duration is [ms] The duration is provided in millisecond.
""" """
rval = [] rval = []
for k,v in reports.get(self.cid,{}).items(): for k, v in reports.get(self.cid, {}).items():
info = { info = {
'query': k, 'query': k,
'counter': v[0], 'counter': v[0],
'duration': v[1] * 1000, 'duration': v[1] * 1000,
'last' : { 'duration' : v[2][0] * 1000, 'last': {'duration': v[2][0] * 1000,
'details' : [dict(id=i[0],duration=i[1]*1000) for i in v[2][1]] 'details': [dict(id=i[0],
}, duration=i[1]*1000)
for i in v[2][1]],
},
} }
rval.append(info) rval.append(info)
return rval
return rval
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