-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
74 lines (64 loc) · 2.06 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
# all the imports
import sqlite3
import json
from datetime import datetime
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from sqlalchemy.sql import func
from setup import db, create_app
from models import Version, GameStart, OSInfo, Uuid, MapStart, Map
from handlers.gamestart import GamestartHandler
from handlers.mapstart import MapstartHandler
from itertools import groupby
app = create_app(__name__)
handler_mapping = {
GamestartHandler.HANDLE: GamestartHandler,
MapstartHandler.HANDLE: MapstartHandler
}
@app.route('/upload', methods=['POST'])
def upload_data():
if request.json is not None:
data = request.json
assert isinstance(data, dict)
if not 'uuid' in data:
return
uuid = Uuid.create_or_fetch(data['uuid'])
for key, data in data.iteritems():
if key in handler_mapping:
handler_mapping[key](data, uuid)
else:
abort(401)
return redirect(url_for('show_gamestarts'))
@app.route('/')
def show_gamestarts():
gamestart_list = GameStart.query.all()
osinfos = OSInfo.query.all()
osstats = []
startstats = []
for osinfo in osinfos:
osstats.append([osinfo.system, len(osinfo.gamestarts)])
entry = {'name': osinfo.system, 'data': []}
for (dateinfo, gamestarts) in GameStart.get_grouped_by_day(osinfo.gamestarts):
entry['data'].append([unix_time(datetime(*dateinfo)), len(list(gamestarts))])
startstats.append(entry)
return render_template('show_gamestarts.html', gamestarts=gamestart_list, osstats=osstats, startstats=startstats)
@app.route('/mapstarts')
def show_mapstarts():
mapstarts = MapStart.query.all()
maps = Map.query.all()
mapdata = {}
for map in maps:
mapdata[map.mapname] = len(map.mapstarts)
mapdata = []
for map in maps:
mapdata.append([map.mapname, len(map.mapstarts)])
return render_template('show_mapstarts.html', mapstarts=mapstarts, mapdata=mapdata)
def unix_time(dt):
epoch = datetime.utcfromtimestamp(0)
delta = dt - epoch
return delta.total_seconds()*1000
# Run application
if __name__ == '__main__':
db.app = app
db.init_app(app)
db.create_all()
app.run()