-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRumbleStats.py
More file actions
165 lines (140 loc) · 5.83 KB
/
RumbleStats.py
File metadata and controls
165 lines (140 loc) · 5.83 KB
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
#!/usr/bin/env python
import datetime
import time
from flask import request
from google.appengine.api import memcache, taskqueue
import structures
from structures import global_dict, load_blob
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))
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:
timeSince = str(mins) + " minute"
if mins != 1:
timeSince += "s"
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)
def rumble_stats():
starttime = time.time()
query = request.query_string.decode('utf-8').replace("%20", " ")
parts = query.split("&")
requests = {}
if parts[0] != "":
for pair in parts:
ab = pair.split('=', 1)
requests[ab[0]] = ab[1] if len(ab) > 1 else ""
timing = bool(requests.get("timing", False))
regen = bool(requests.get("regen", False))
dark = requests.get("theme", "") == "dark"
extraArgs = ""
if dark:
extraArgs += "&theme=dark"
cacheKey = "stats_dark" if dark else "stats"
outstr = None
if not regen:
outstr = global_dict.get(cacheKey, None)
if outstr is None:
outstr = memcache.get(cacheKey)
if outstr is None:
tq = taskqueue.Queue()
tqs_r = tq.fetch_statistics_async()
out = []
out.append(structures.header("Statistics", structures.home_link("LiteRumble", dark) + " Statistics", dark))
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:
scores = load_blob(r.ParticipantsScores, {})
if not isinstance(scores, dict):
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
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>")
uploaders = load_blob(r.Uploaders, {})
if not isinstance(uploaders, dict) or len(uploaders) == 0:
uploaders = {}
uv = list(uploaders.values())
cutoff = datetime.datetime.now() - datetime.timedelta(31)
uv = [u for u in uv if datetime.datetime.strptime(u.latest, "%Y-%m-%d %H:%M:%S") > cutoff]
uv.sort(key=lambda u: u.latest, reverse=True)
for j, u in enumerate(uv):
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)
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 = 0
backlog = float(tasks) * 60.0 / last_min
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 = "".join(out)
memcache.set(cacheKey, outstr, time=630)
global_dict[cacheKey] = outstr
elapsed = time.time() - starttime
if timing:
outstr += "<br>\n Page served in " + str(int(round(elapsed * 1000))) + "ms."
outstr += "</body></html>"
return outstr