Permalink
Browse files

perhaps overly clever way of getting the current number of members an…

…d/or guests
  • Loading branch information...
1 parent 2af4af5 commit 8d16d2731a05867d356b766e5fa02884aba845c8 @progrium progrium committed Dec 27, 2010
Showing with 32 additions and 2 deletions.
  1. +32 −2 main.py
View
@@ -8,6 +8,7 @@
import logging
import base64
import urllib
+import time
DOMAIN = 'hackerdojo.com'
MEMBER_DOWNLOAD = 0
@@ -36,6 +37,23 @@ def is_suspended(user):
""" Convenience function for if a user is suspended """
return dojo('/users/%s' % user)['suspended']
+def touch_stat(name, resolution=60):
+ """ Keeps a backlog of counts for calculating rate """
+ bucket = '%s-%s' % (name, int(time.time()) - int(time.time()) % resolution)
+ if not memcache.add(bucket, 0, time=resolution*5):
+ memcache.incr(bucket)
+
+def get_stat(name, resolution=60):
+ """ Calculate running average of a stat from counts """
+ since = time.time() - (resolution*5)
+ since = int(since) - int(since) % resolution
+ keys = ['%s-%s' % (name, since + (i * resolution)) for i in range(5-1)]
+ counts = memcache.get_multi(keys).values()
+ if len(counts):
+ return sum(counts) / len(counts)
+ else:
+ return 0
+
class MacAddressMapping(db.Model):
""" Member MAC address mapping
@@ -52,6 +70,16 @@ class MacAddressMapping(db.Model):
def get_by_mac(cls, address):
return cls.all().filter('address =', address).get()
+class StatHandler(webapp.RequestHandler):
+ def get(self, name=None):
+ if name in ['members', 'guests']:
+ stat = get_stat(name)
+ else:
+ members = get_stat('members')
+ guests = get_stat('guests')
+ stat = members+guests
+ self.response.out.write(str(stat))
+
class EntryHandler(webapp.RequestHandler):
""" Entry point for the wifi app
@@ -110,21 +138,23 @@ def get(self, mac):
guest = memcache.get(mac)
if guest:
self.response.out.write("%s,," % guest)
+ touch_stat('guests')
else:
mapping = MacAddressMapping.get_by_mac(mac)
if mapping and not is_suspended(mapping.username):
self.response.out.write("%s,%s,%s" % (mapping.username, MEMBER_DOWNLOAD, MEMBER_UPLOAD))
+ touch_stat('members')
else:
self.error(404)
self.response.out.write("not found")
-
-
+
def main():
application = webapp.WSGIApplication([
('/api/mac/(.+)', MacHandler),
('/guest', GuestHandler),
('/member', MemberHandler),
+ ('/stat/(.+)', StatHandler),
('/(.+)', EntryHandler),] ,debug=True)
util.run_wsgi_app(application)

0 comments on commit 8d16d27

Please sign in to comment.