-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRumbleStats.py
217 lines (185 loc) · 8.17 KB
/
RumbleStats.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
#import cgi
import datetime
import wsgiref.handlers
import time
#try:
# import json
#except:
# import simplejson as json
import string
import marshal
import zlib
import pickle
#from google.appengine.ext import db
from google.appengine.api import taskqueue
from google.appengine.ext import webapp
from google.appengine.api import memcache
import logging
#from operator import attrgetter
import structures
from structures import global_dict
def formatSecs(secs):
mins = int(round(secs/60.0 - 0.49999))
secs = int(round(secs%60))
hours =int(round(mins/60.0 - 0.49999))
mins = int(round(mins%60))
days = int(round(hours/24.0 - 0.49999))
#years = days/365.0
timeSince = ""
if days > 0:
timeSince = str(days) + " day"
if days != 1:
timeSince += "s"
elif hours > 0:
timeSince = str(hours) + " hour"
if hours != 1:
timeSince += "s"
else:# mins > 0:
timeSince = str(mins) + " minute"
if mins != 1:
timeSince += "s"
# else:
# timeSince = str(secs) + " second"
# if secs != 1:
# timeSince += "s"
#timeSince += " ago"
return timeSince
def timeSince(timestring):
t = datetime.datetime.strptime(timestring,"%Y-%m-%d %H:%M:%S")
secs = (datetime.datetime.now() - t).total_seconds()
return formatSecs(secs)
class RumbleStats(webapp.RequestHandler):
def get(self):
global global_dict
#global_dict = {}
starttime = time.time()
query = self.request.query_string
query = query.replace("%20"," ")
parts = query.split("&")
requests = {}
if parts[0] != "":
for pair in parts:
ab = pair.split('=')
requests[ab[0]] = ab[1]
timing = bool(requests.get("timing",False))
regen = bool(requests.get("regen",False))
extraArgs = ""
#if timing:
# extraArgs += "&timing=1"
outstr = None
if not regen:
outstr = global_dict.get("stats",None)
if outstr is None:
outstr = memcache.get("stats")
if outstr is None:
tq = taskqueue.Queue()
tqs_r = tq.fetch_statistics_async()
#gameHref = "<a href=Rankings?game=" + game + extraArgs + ">" + game + "</a>"
out = []
out.append(structures.html_header % ("Statistics","LiteRumble Statistics"))
out.append("\nStats generated: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " UTC")
out.append("\nAllowed Robocode versions: " + ", ".join(structures.allowed_clients) + "\n<br><br>\n")
q = structures.Rumble.all()
rumbles = [[],[],[]]
categories = ["1v1","Melee","Teams"]
for r in q.run():
memr = global_dict.get(r.Name,None)
if memr is None:
memr = memcache.get(r.Name)
if memr is not None:
r = memr
if r.Melee:
rumbles[1].append(r)
elif r.Teams:
rumbles[2].append(r)
else:
rumbles[0].append(r)
for cat,rumbs in zip(categories,rumbles):
for r in rumbs:
try:
scores = pickle.loads(zlib.decompress(r.ParticipantsScores))
except:
logging.error("cannot load ParticipantsScores from " + r.Name)
try:
scores = marshal.loads(zlib.decompress(r.ParticipantsScores))
except:
scores = {}
entries = len(scores)
if r.LastUpload is None:
latest = None
for s in scores.values():
t = s.LastUpload
if latest is None or t > latest:
latest = t
r.LastUpload = latest
r.__dict__["entries"] = entries
#r.__dict__["scores"] = scores
rumbs.sort(key = lambda r: -r.__dict__["entries"])
out.append( "<table class=\"rumble\">\n<tr>")
out.append( "<th>" + cat + "</th>\n<th>Participants</th>\n<th>Total Uploads</th>\n<th>Last Upload</th></tr>")
for i,r in enumerate(rumbs):
game = r.Name
gameHref = "<a href=\"Rankings?game=" + game + extraArgs + "\" ><b>" + game + "</b></a>"
lastTimeSince = timeSince(r.LastUpload) + " ago"
out.append( "\n<tr>\n<td>" + gameHref + "</td>\n<th>" + str(r.__dict__["entries"]) + "</th>\n<th>")
out.append(str(r.TotalUploads) + "</th><th>"+lastTimeSince+"</th>\n</tr>")
try:
uploaders = pickle.loads(zlib.decompress(r.Uploaders))
if len(uploaders) == 0:
uploaders = {}
except TypeError:
uploaders = {}
#out.append("\n<tr><td></td><td><i><u>Uploader Name</u></i></td><td></td><td></td></tr>")
uv = uploaders.values()
cutoff = datetime.datetime.now() - datetime.timedelta(31)
uv = filter(lambda u: datetime.datetime.strptime(u.latest,"%Y-%m-%d %H:%M:%S") > cutoff, uv)
uv.sort(key = lambda u: u.latest, reverse=True)
for j,u in enumerate(uv):
# if j == 0:
# out.append("\n<tr><td><i>Uploader Name</i></td><td>")
# else:
out.append("\n<tr><td></td><td>")
name = u.name
if name == "Put_Your_Name_Here":
name = "Anonymous"
out.append(name)
out.append("</td><td>")
out.append(str(u.total))
out.append("</td><td>")
out.append(timeSince(u.latest) + " ago")
out.append("</td></tr>")
for r in rumbs:
r.__dict__.pop("entries",1)
#r.__dict__.pop("scores",1)
out.append( "\n</table>")
tqs = tqs_r.get_result()
tasks = tqs.tasks
last_min = tqs.executed_last_minute
if last_min is None or last_min == 0:
last_min = 1
if tasks is None:
tasks is 0
backlog = float(tasks)*60.0/last_min
#out.append()
tq_string = "<table>\n<tr><th colspan=\"2\">Upload Queue</th></tr>\n<tr><td>Projected Processing Time</td><td>" + formatSecs(backlog) + "</td></tr>"
tq_string += "\n<tr><td>Current Size</td><td>" + str(tasks) + " pairing"
if tasks != 1:
tq_string += "s"
tq_string += "</td></tr>\n</table>\n<br>"
out.insert(2,tq_string)
outstr = string.join(out,"")
memcache.set("stats",outstr)
global_dict["stats"] = outstr
elapsed = time.time() - starttime
if timing:
outstr += "<br>\n Page served in " + str(int(round(elapsed*1000))) + "ms."
outstr += "</body></html>"
self.response.out.write(outstr)
application = webapp.WSGIApplication([
('/RumbleStats', RumbleStats)
], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()