from time import time from os import environ from cgi import parse_qs, FieldStorage as FormPost import google.appengine.api.labs.taskqueue as tq from google.appengine.api.memcache import get as mget, set as mset, get_multi as mmget, delete as mdel, flush_all import google.appengine.api.memcache as m NS = 'MESTAT-DATA' def stat(): """Save current stats""" s = m.get_stats() t = int(time()) m.set(str(t), s, namespace=NS) # XXX Possible race if task scheduler messes up, but we don't care. sts = m.get('sample-times', namespace=NS) if sts == None: sts = [] sts.insert(0, t) sts = sts[:2*6*24] # Keep two days of data (at a rate of one sample/10min ) m.set('sample-times', sts, namespace=NS) def main(): args = parse_qs(environ['QUERY_STRING']) form = FormPost() if form.has_key('FLUSH'): flush_all() if 'update' in args: stat() return ats = ['items', 'bytes', 'oldest_item_age', 'hits', 'byte_hits', 'misses'] samples = mget('sample-times', namespace=NS) if not samples: stat() samples = mget('sample-times', namespace=NS) s = mmget([str(i) for i in samples], namespace=NS) # a = dict([(k, [int(s[d][k]) for d in s]) for k in ats]) # attr -> vals a = dict([(k, (max(a[k]), min(a[k]), a[k])) for k in a]) # attrs -> (max, min, vals) #a = dict([(k, [61*(v+1-a[k][1])/(a[k][0]+1-a[k][1]) for v in a[k][2]]) for k in a]) # attrs -> norml-vals a = dict([(k, ([61*(v+1-a[k][1])/(a[k][0]+1-a[k][1]) for v in a[k][2]], a[k][1], a[k][0])) for k in a]) # attrs -> norml-vals print "Content-type: text/html" print "" #l = ["rend('"+k+"', %s);"%str([int(s[d][k]) for d in s]) for k in ats] #l = ["rend('"+k+"', %s);"%str([int(d) for d in a[k]]) for k in a] print """

Memcache Stats
""" if __name__ == '__main__': main()