forked from youtify/youtify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.py
67 lines (55 loc) · 1.84 KB
/
ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# http://blog.notdot.net/2010/11/Storage-options-on-App-Engine
import os
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.api import users
import webapp2
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import json as simplejson
from model import PingStats
def get_or_create_pings():
pings = memcache.get('pings')
if pings is None:
pings = 0
memcache.add('pings', pings)
return pings
class PingHandler(webapp2.RequestHandler):
""" Increment pings """
def post(self):
get_or_create_pings()
memcache.incr('pings');
current_user = users.get_current_user()
if current_user == None:
self.response.out.write('logged_out')
else:
self.response.out.write('ok')
def get(self):
get_or_create_pings()
memcache.incr('pings');
self.response.out.write('')
class PingCronHandler(webapp2.RequestHandler):
""" Move pings from memcache to DB """
def get(self):
m = PingStats(pings=get_or_create_pings())
m.put()
memcache.set('pings', 0)
class PingGraphHandler(webapp2.RequestHandler):
""" Get pings for the last 24h """
def get(self):
path = os.path.join(os.path.dirname(__file__), 'html', 'usersonline.html')
json = []
for m in PingStats.all().order('-date').fetch(6*24*7):
json.append({
'date': str(m.date),
'pings': m.pings,
})
self.response.out.write(template.render(path, {
'pings': simplejson.dumps(json),
'npings': len(json),
}))
app = webapp2.WSGIApplication([
('/cron/store_pings', PingCronHandler),
('/stats', PingGraphHandler),
('/ping', PingHandler),
], debug=False)