forked from Hydro74000/Monocle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gyms.py
executable file
·92 lines (79 loc) · 2.62 KB
/
gyms.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
#!/usr/bin/env python3
from datetime import datetime, timedelta
from pkg_resources import resource_filename
import time
import argparse
from flask import Flask, render_template
from monocle import db, sanitized as conf
from monocle.names import POKEMON
from monocle.web_utils import get_args
from monocle.bounds import area
app = Flask(__name__, template_folder=resource_filename('monocle', 'templates'))
CACHE = {
'data': None,
'generated_at': None,
}
def get_stats():
cache_valid = (
CACHE['data'] and
CACHE['generated_at'] > datetime.now() - timedelta(minutes=15)
)
if cache_valid:
return CACHE['data']
with db.session_scope() as session:
forts = db.get_forts(session)
count = {t.value: 0 for t in db.Team}
guardians = {t.value: {} for t in db.Team}
top_guardians = {t.value: None for t in db.Team}
percentages = {}
last_date = 0
pokemon_names = POKEMON
for fort in forts:
if fort['last_modified'] > last_date:
last_date = fort['last_modified']
team = fort['team']
count[team] += 1
if team != 0:
# Guardians
guardian_value = guardians[team].get(pokemon_id, 0)
guardians[team][pokemon_id] = guardian_value + 1
for team in db.Team:
percentages[team.value] = (
count.get(team.value) / len(forts) * 100
)
if guardians[team.value]:
pokemon_id = sorted(
guardians[team.value],
key=guardians[team.value].__getitem__,
reverse=True
)[0]
top_guardians[team.value] = pokemon_names[pokemon_id]
CACHE['generated_at'] = datetime.now()
CACHE['data'] = {
'order': sorted(count, key=count.__getitem__, reverse=True),
'count': count,
'total_count': len(forts),
'percentages': percentages,
'last_date': last_date,
'top_guardians': top_guardians,
'generated_at': CACHE['generated_at'],
}
return CACHE['data']
@app.route('/')
def index():
stats = get_stats()
team_names = {k.value: k.name.title() for k in db.Team}
styles = {1: 'primary', 2: 'danger', 3: 'warning'}
return render_template(
'gyms.html',
area_name=conf.AREA_NAME,
area_size=area,
minutes_ago=int((datetime.now() - stats['generated_at']).seconds / 60),
last_date_minutes_ago=int((time.time() - stats['last_date']) / 60),
team_names=team_names,
styles=styles,
**stats
)
if __name__ == '__main__':
args = get_args()
app.run(debug=args.debug, host=args.host, port=args.port)