-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
196 lines (160 loc) · 6.44 KB
/
main.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
from datetime import datetime
import pytz
import os
import json
from collections import OrderedDict
from urllib.parse import urlparse, parse_qs
from flask import Flask, render_template, request, \
redirect, send_from_directory, url_for
from google.cloud import bigquery
from cardcalc_fflogsapi import decompose_url, get_bearer_token
from cardcalc_data import CardCalcException
from cardcalc_cards import cardcalc
app = Flask(__name__)
LAST_CALC_DATE = pytz.UTC.localize(datetime.utcfromtimestamp(1663886556))
token = get_bearer_token()
client = bigquery.Client('astcardcalc-vm')
Reports = client.get_table('astcardcalc-vm.Reports.Reports')
Counts = client.get_table('astcardcalc-vm.Reports.Counts')
def get_count():
count_query = client.query(
"SELECT * FROM `astcardcalc-vm.Reports.Counts`;").result()
report_count = next(count_query).get('total_reports')
return report_count
def increment_count():
report_count = get_count() + 1
sql = """
UPDATE `astcardcalc-vm.Reports.Counts`
SET total_reports = {}
WHERE total_reports > 0;
""".format(report_count)
client.query(sql).result()
return report_count
def prune_reports():
Reports = client.get_table('astcardcalc-vm.Reports.Reports')
if Reports.num_rows > 10000:
sql_get = """SELECT computed FROM `astcardcalc-vm.Reports.Reports`
ORDER BY computed ASC
LIMIT 1 OFFSET 500"""
time_query = client.query(sql_get).result()
computed_cutoff = next(time_query).get('computed')
sql_delete = """DELETE FROM `astcardcalc-vm.Reports.Reports`
WHERE computed < {}""".format(computed_cutoff)
client.query(sql_delete).result()
@app.route('/', methods=['GET', 'POST'])
def homepage():
"""Simple form for redirecting to a report, no validation"""
if request.method == 'POST':
report_url = request.form['report_url']
try:
report_id, fight_id = decompose_url(report_url, token)
except CardCalcException as exception:
return render_template('error.html', exception=exception)
return redirect(url_for('calc',
report_id=report_id,
fight_id=fight_id))
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html', report_count=get_count())
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/png')
@app.route('/<string:report_id>/<int:fight_id>')
def calc(report_id, fight_id):
"""The actual calculated results view"""
# Very light validation, more for the db query than for the user
if (len(report_id) < 14 or len(report_id) > 24):
return redirect(url_for('homepage'))
sql_report = None
report = None
sql = """
SELECT * FROM `astcardcalc-vm.Reports.Reports`
WHERE report_id='{}' AND fight_id={}
ORDER BY computed DESC;
""".format(report_id, fight_id)
query_res = client.query(sql).result()
if (query_res.total_rows > 0):
sql_report = next(query_res)
sql_report = dict(sql_report.items())
report = {
'report_id': sql_report['report_id'],
'fight_id': sql_report['report_id'],
'results': json.loads(sql_report['results']),
'actors': json.loads(sql_report['actors']),
'enc_name': sql_report['enc_name'],
'enc_time': sql_report['enc_time'],
'enc_kill': sql_report['enc_kill'],
'computed': sql_report['computed'],
}
if sql_report is None:
# Compute
try:
results, actors, encounter_info = cardcalc(
report_id, fight_id, token)
except CardCalcException as exception:
return render_template('error.html', exception=exception)
sql_report = {
'report_id': report_id,
'fight_id': fight_id,
'results': json.dumps(results),
'actors': json.dumps(actors),
'enc_name': encounter_info['enc_name'],
'enc_time': encounter_info['enc_time'],
'enc_kill': encounter_info['enc_kill'],
'computed': datetime.now().isoformat(),
}
report = {
'report_id': report_id,
'fight_id': fight_id,
'results': results,
'actors': actors,
'enc_name': encounter_info['enc_name'],
'enc_time': encounter_info['enc_time'],
'enc_kill': encounter_info['enc_kill'],
'computed': datetime.now(),
}
# print(sql_report)
row_result = client.insert_rows_json(Reports, [sql_report])
# print(row_result)
increment_count()
else:
# print(sql_report['computed'])
# print(type(sql_report['computed']))
# print(LAST_CALC_DATE)
# print(type(LAST_CALC_DATE))
# Recompute if no computed timestamp
if sql_report['computed'] < LAST_CALC_DATE:
try:
results, actors, encounter_info = cardcalc(
report_id, fight_id, token)
except CardCalcException as exception:
return render_template('error.html', exception=exception)
sql_report = {
'report_id': report_id,
'fight_id': fight_id,
'results': json.dumps(results),
'actors': json.dumps(actors),
'enc_name': encounter_info['enc_name'],
'enc_time': encounter_info['enc_time'],
'enc_kill': encounter_info['enc_kill'],
'computed': datetime.now().isoformat(),
}
report = {
'report_id': report_id,
'fight_id': fight_id,
'results': results,
'actors': actors,
'enc_name': encounter_info['enc_name'],
'enc_time': encounter_info['enc_time'],
'enc_kill': encounter_info['enc_kill'],
'computed': datetime.now(),
}
row_result = client.insert_rows_json(Reports, [sql_report])
report['results'] = {int(k): v for k, v in report['results'].items()}
report['actors'] = {int(k): v for k, v in report['actors'].items()}
report['results'] = list(OrderedDict(
sorted(report['results'].items())).values())
actors = {int(k): v for k, v in report['actors'].items()}
return render_template('calc.html', report=report)