Skip to content

Commit

Permalink
Moved some stuff to util
Browse files Browse the repository at this point in the history
  • Loading branch information
matterkkila committed May 7, 2011
1 parent e59dc84 commit 198ee6b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 75 deletions.
78 changes: 5 additions & 73 deletions kestrelweb/main.py
Expand Up @@ -8,76 +8,8 @@
import kestrel_actions
import util

App = dream.App()

QUEUE_SORT = {
'server': lambda x: x['server'].lower(),
'name': lambda x: x['queue'].lower(),
'items': lambda x: x['items'],
'bytes': lambda x: x['bytes'],
'total_items': lambda x: x['total_items'],
'logsize': lambda x: x['logsize'],
'expired_items': lambda x: x['expired_items'],
'mem_items': lambda x: x['mem_items'],
'mem_bytes': lambda x: x['mem_bytes'],
'age': lambda x: x['age'],
'discarded': lambda x: x['discarded'],
'waiters': lambda x: x['waiters'],
'open_transactions': lambda x: x['open_transactions'],
}

FILTER_COMP = {
'=': lambda x, y: x == y,
'>': lambda x, y: x > y,
'<': lambda x, y: x < y,
'>=': lambda x, y: x >= y,
'<=': lambda x, y: x <= y,
'!=': lambda x, y: x != y,
}

def queue_filter(pattern, queue, qstats):
"""A filter to restrict queues"""
try:
if (pattern is not None) and isinstance(pattern, (str, unicode)) and (len(pattern) > 0):
_m = re.match('(.*?)(>=|<=|!=|=|<|>)(.*)', pattern)
if not _m:
return True

(field, comp, filter_value) = _m.groups()

value = None
if field == 'queue':
value = queue
elif field in qstats:
value = qstats[field]
else:
raise Exception('Unknown field')

if comp not in FILTER_COMP:
raise Exception('Unknown comparitor')

if isinstance(value, (int, long, float)):
if FILTER_COMP[comp](value, float(filter_value)):
return True
elif isinstance(value, (str, unicode)):
if comp not in ['!=', '=']:
raise Exception('Invalid comparitor')

qmatch = re.match(filter_value, value, re.I)
if qmatch and (comp == '='):
return True
elif not qmatch and (comp == '!='):
return True

return False
else:
raise Exception('Unknown type')

return False
except:
pass

return True
App = dream.App()


@App.expose('/')
Expand Down Expand Up @@ -130,18 +62,18 @@ def ajax_stats(request):
queue_stats.extend([
dict(server=server, queue=queue, **qstats)
for queue, qstats in _data['queues'].iteritems()
if queue_filter(qfilter, queue, qstats)
if util.queue_filter(qfilter, queue, qstats)
])

response['servers'] = [
{'server': server, 'stats': _stats}
for server, _stats in server_stats.iteritems()
]
response['servers'].sort(key=QUEUE_SORT['server'])
response['servers'].sort(key=util.QUEUE_SORT['server'])

response['queues'] = queue_stats
response['queues'].sort(key=QUEUE_SORT['server'])
response['queues'].sort(key=QUEUE_SORT[qsort] if qsort in QUEUE_SORT else QUEUE_SORT['name'], reverse=qreverse)
response['queues'].sort(key=util.QUEUE_SORT['server'])
response['queues'].sort(key=util.QUEUE_SORT[qsort] if qsort in util.QUEUE_SORT else util.QUEUE_SORT['name'], reverse=qreverse)

return dream.JSONResponse(callback=callback, body=response)

Expand Down
72 changes: 70 additions & 2 deletions kestrelweb/util.py
@@ -1,10 +1,78 @@


import os
import re

PREFIX = os.path.normpath('%s/../..' % (os.path.realpath(__file__)))

prefix = os.path.normpath('%s/../..' % (os.path.realpath(__file__)))
QUEUE_SORT = {
'server': lambda x: x['server'].lower(),
'name': lambda x: x['queue'].lower(),
'items': lambda x: x['items'],
'bytes': lambda x: x['bytes'],
'total_items': lambda x: x['total_items'],
'logsize': lambda x: x['logsize'],
'expired_items': lambda x: x['expired_items'],
'mem_items': lambda x: x['mem_items'],
'mem_bytes': lambda x: x['mem_bytes'],
'age': lambda x: x['age'],
'discarded': lambda x: x['discarded'],
'waiters': lambda x: x['waiters'],
'open_transactions': lambda x: x['open_transactions'],
}

FILTER_COMP = {
'=': lambda x, y: x == y,
'>': lambda x, y: x > y,
'<': lambda x, y: x < y,
'>=': lambda x, y: x >= y,
'<=': lambda x, y: x <= y,
'!=': lambda x, y: x != y,
}

def queue_filter(pattern, queue, qstats):
"""A filter to restrict queues"""
try:
if (pattern is not None) and isinstance(pattern, (str, unicode)) and (len(pattern) > 0):
_m = re.match('(.*?)(>=|<=|!=|=|<|>)(.*)', pattern)
if not _m:
return True

(field, comp, filter_value) = _m.groups()

value = None
if field == 'queue':
value = queue
elif field in qstats:
value = qstats[field]
else:
raise Exception('Unknown field')

if comp not in FILTER_COMP:
raise Exception('Unknown comparitor')

if isinstance(value, (int, long, float)):
if FILTER_COMP[comp](value, float(filter_value)):
return True
elif isinstance(value, (str, unicode)):
if comp not in ['!=', '=']:
raise Exception('Invalid comparitor')

qmatch = re.match(filter_value, value, re.I)
if qmatch and (comp == '='):
return True
elif not qmatch and (comp == '!='):
return True

return False
else:
raise Exception('Unknown type')

return False
except:
pass

return True

def static(filepath):
return readfile('/static/%s' % filepath)
Expand All @@ -13,7 +81,7 @@ def template(filepath):
return readfile('/templates/%s' % filepath)

def readfile(filepath):
fp = open('%s%s' % (prefix, filepath))
fp = open('%s%s' % (PREFIX, filepath))
body = fp.read()
fp.close()
return body

0 comments on commit 198ee6b

Please sign in to comment.