forked from youtify/youtify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
107 lines (88 loc) · 3.53 KB
/
stats.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
from datetime import datetime
import webapp2
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
import json as simplejson
from model import Stats
from model import YoutifyUser
from model import Playlist
from model import Activity
from model import FollowRelation
from model import PingStats
def get_flattr_stats_json():
json = memcache.get('flattr_stats')
if json is None:
json = '{nr_of_users: 0, nr_of_flattrs: 0}'
return json
class FlattrStatsCronJobHandler(webapp2.RequestHandler):
def get(self):
json = simplejson.dumps({
'nr_of_users': len([i for i in YoutifyUser.all().filter('flattr_access_token !=', None)]),
'nr_of_flattrs': len([i for i in Activity.all().filter('type =', 'outgoing').filter('verb =', 'flattr')]),
})
memcache.delete('flattr_stats')
memcache.add('flattr_stats', json, 3600*24)
class StatsPageHandler(webapp2.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'html', 'stats.html')
self.response.out.write(template.render(path, {
'stats': Stats.all().order('-date').get()
}))
class CronJobHandler(webapp2.RequestHandler):
def get(self):
stats = Stats()
stats.nr_of_users = 0
stats.nr_of_active_users = 0
try:
stats.nr_of_playlists = Playlist.all(keys_only=True).count(read_policy=EVENTUAL_CONSISTENCY)
except:
pass
stats.nr_of_users_with_flattr_account = 0
stats.nr_of_users_with_dropbox = 0
try:
stats.nr_of_flattrs = Activity.all().filter('type =', 'outgoing').filter('verb =', 'flattr').count(read_policy=EVENTUAL_CONSISTENCY)
except:
pass
stats.nr_of_playlist_subscriptions = 0
try:
stats.nr_of_follow_relations = FollowRelation.all(keys_only=True).count(read_policy=EVENTUAL_CONSISTENCY)
except:
pass
try:
for user in YoutifyUser.all():
stats.nr_of_users += 1
if user.flattr_user_name:
stats.nr_of_users_with_flattr_account += 1
if user.dropbox_user_name:
stats.nr_of_users_with_dropbox += 1
if user.playlist_subscriptions:
stats.nr_of_playlist_subscriptions += len(user.playlist_subscriptions)
if user.last_login:
delta = datetime.now() - user.last_login
if delta.seconds < 3600 * 24 * 7:
stats.nr_of_active_users += 1
except:
pass
pings = []
last_ping = None
try:
for m in PingStats.all().order('-date').fetch(6*24*7):
if last_ping is not None and last_ping.date.hour is not m.date.hour:
pings.append({
'date': str(last_ping.date),
'pings': last_ping.pings
})
last_ping = m
elif last_ping is None or m.pings > last_ping.pings:
last_ping = m
except:
pass
stats.pings = simplejson.dumps(pings)
stats.put()
app = webapp2.WSGIApplication([
('/stats', StatsPageHandler),
('/cron/gather_stats', CronJobHandler),
('/cron/gather_flattr_stats', FlattrStatsCronJobHandler),
], debug=True)