-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
370 lines (332 loc) · 12.4 KB
/
app.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
import flask
from flask import request, send_from_directory, send_file, abort
import yaml
import os
import datetime
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
from sqlalchemy.orm import Session
from models import Snapshot
from flask_cors import CORS
app = flask.Flask(__name__)
CORS(app)
# Load configuration from YAML file
__dir__ = os.path.dirname(__file__)
home = os.environ.get('HOME')
app.config["REPLICA_FILE"] = f"{home}/replica.my.cnf"
app.config.update(
yaml.safe_load(open(os.path.join(__dir__, 'config.yaml'))))
# pandas is throwing a numpy deprecation warning
np.warnings.filterwarnings('ignore', category=np.VisibleDeprecationWarning)
DATABASE = '{wiki}_p'
# con = create_engine(constr, pool_recycle= 60)
remove_tagged_bots = '''left join
user_groups ug
on (
u.user_id=ug.ug_user
and ug_group = "bot"
)
left join
user_former_groups ufg
on (
u.user_id=ufg.ufg_user
and ufg_group = "bot"
)
WHERE
ug_group is null and ufg_group is null'''
periodical_query = """SELECT
`u`.`user_name`,
`revs`.`total` AS `user_editcount`,
`user_properties`.`up_value`
FROM `user` u
LEFT JOIN `user_properties` ON (
`u`.`user_id` = `user_properties`.`up_user`
AND `user_properties`.`up_property` = "gender"
)
LEFT JOIN `actor` ON `u`.`user_id` = `actor`.`actor_user`
LEFT JOIN (
SELECT rev_actor, COUNT(rev_id) as `total`
FROM revision
WHERE rev_timestamp > '{period_start}' AND rev_timestamp < '{period_end}'
GROUP BY rev_actor
) AS `revs` ON `revs`.`rev_actor` = `actor`.`actor_id`
{tagged_bots}
ORDER BY `revs`.`total` DESC
LIMIT {limit};"""
historical_query = """select
user_editcount,
user_name,
up_value
from
user u
left join
user_properties up
ON (
u.user_id=up.up_user
and up_property = "gender"
)
{tagged_bots}
ORDER BY
user_editcount desc
limit {limit};"""
recent_changes_query = '''
select count(rc_id) as user_editcount, u.user_name, up.up_value
from recentchanges
left join
actor
ON (
recentchanges.rc_actor=actor.actor_id
)
left join
user u
ON (
actor.actor_user=u.user_id
)
left join
user_properties up
ON (
u.user_id=up.up_user
and up_property = "gender"
)
{tagged_bots}
AND
recentchanges.rc_type < 3
GROUP BY recentchanges.rc_actor
ORDER BY
user_editcount desc
limit {limit}
'''
def create_replicas_connection(wiki):
db = DATABASE.format(wiki=wiki)
host = f"{wiki}.analytics.db.svc.wikimedia.cloud"
constr = f'mysql+pymysql://{host}/{db}'
con = create_engine(constr, pool_recycle=60, connect_args={
'read_default_file': app.config["REPLICA_FILE"],
}, poolclass=NullPool)
return con
def get_gender_stats(df, limit):
"""Gets gender stats from query dataframe"""
limit = int(limit)
df['up_value'] = df['up_value'].fillna(b'neutral')
df['up_value'] = df['up_value'].str.decode("utf-8")
df['user_name'] = df['user_name'].str.decode("utf-8")
describe = df.groupby(df['up_value']).describe()
describe.loc[:, ('user_editcount', '%_of_editors')] = describe[
'user_editcount']['count']/limit * 100
total_edits = df.loc[:, 'user_editcount'].sum()
describe = pd.concat([describe, df.groupby('up_value').sum(
numeric_only=True)],
axis=1).reindex(describe.index)
describe.loc[:, ('%_of_edits')] = describe.loc[:, ('user_editcount'
)]/total_edits * 100
out = describe.loc[:, [('user_editcount', '%_of_editors'),
('user_editcount', 'count'), 'user_editcount',
'%_of_edits']]
out.columns = (['%_of_editors', 'count', 'editcount', '%_of_edits'])
return out
@app.route('/historical')
def historical():
wiki = request.args.get('wiki', 'ptwiki')
limit = request.args.get('limit', '100')
con = create_replicas_connection(wiki)
df = pd.read_sql(historical_query.format(tagged_bots=remove_tagged_bots,
limit=limit), con)
return get_gender_stats(df, limit).to_json()
@app.route('/period')
def period():
wiki = request.args.get('wiki', 'ptwiki')
limit = request.args.get('limit', '100')
snapshot_con = create_snapshot_data_connection()
period_start = request.args.get('period_start', '')
period_end = request.args.get('period_end', '')
periodicity = request.args.get('periodicity', '')
if period_start == '' or period_end == '':
abort(400, description="period_start and period_end must be set")
try:
period_start = datetime.datetime.fromisoformat(period_start)
period_end = datetime.datetime.fromisoformat(period_end)
period_start_str = period_start.strftime('%Y%m%d%H%M%S')
period_end_str = period_end.strftime('%Y%m%d%H%M%S')
except Exception:
abort(400, description="Could not parse period start and end, is it in ISO format?")
if periodicity == 'monthly':
if not (period_end.day == period_start.day and (
(period_end.month - period_start.month == 1) and period_end.year == period_start.year) or
period_start.month - period_end.month == 11 and period_end.year - period_start.year == 1):
abort(400, "Period should be exactly one month")
if periodicity == 'weekly':
if not (period_start.weekday == 0 and period_end.weekday == 0):
abort(400, "Weekly snapshots should start and end on Mondays")
if not (period_start - period_end == datetime.timedelta(weeks=1)):
abort(400, "Weekly snapshots period should be 1 week")
if periodicity == 'daily':
if not (period_start - period_end == datetime.timedelta(days=1)):
abort(400, "Daily snapshots period should be 1 day")
test, results = maybe_snapshot(
'periodical', wiki, snapshot_con, limit, period_start=period_start, period_end=period_end,
periodicity=periodicity)
if test:
replicas_con = create_replicas_connection(wiki)
# print(periodical_query.format(
# tagged_bots=remove_tagged_bots,
# limit=limit, period_start=period_start_str,
# period_end=period_end_str))
df = pd.read_sql(periodical_query.format(
tagged_bots=remove_tagged_bots,
limit=limit, period_start=period_start_str,
period_end=period_end_str), replicas_con)
stats = get_gender_stats(df, limit).to_dict()
session = Session(bind=snapshot_con)
snap = Snapshot(
wiki=wiki,
type='periodical',
timestamp=datetime.datetime.now(),
editors_male=stats['count'].get('male', 0),
editors_female=stats['count'].get('female', 0),
editors_neutral=stats['count'].get('neutral', 0),
edits_male=stats['editcount'].get('male', 0),
edits_female=stats['editcount'].get('female', 0),
edits_neutral=stats['editcount'].get('neutral', 0),
period_start=period_start,
period_end=period_end,
limit=limit,
periodicity=periodicity,
)
session.add(snap)
session.commit()
results = snap.to_dict()
session.close()
replicas_con.dispose()
snapshot_con.dispose()
return results
@app.route('/recent')
def recent():
wiki = request.args.get('wiki', 'ptwiki')
limit = request.args.get('limit', '100')
snapshot_con = create_snapshot_data_connection()
test, results = maybe_snapshot('recent', wiki, snapshot_con, limit)
if test:
replicas_con = create_replicas_connection(wiki)
df = pd.read_sql(recent_changes_query.format(
tagged_bots=remove_tagged_bots,
limit=limit), replicas_con)
stats = get_gender_stats(df, limit).to_dict()
session = Session(bind=snapshot_con)
snap = Snapshot(
wiki=wiki,
type='recent',
timestamp=datetime.datetime.now(),
editors_male=stats['count'].get('male', 0),
editors_female=stats['count'].get('female', 0),
editors_neutral=stats['count'].get('neutral', 0),
edits_male=stats['editcount'].get('male', 0),
edits_female=stats['editcount'].get('female', 0),
edits_neutral=stats['editcount'].get('neutral', 0),
limit=limit,
)
session.add(snap)
session.commit()
results = snap.to_dict()
session.close()
replicas_con.dispose()
snapshot_con.dispose()
return results
def create_snapshot_data_connection():
db = app.config['database']
host = "tools.db.svc.wikimedia.cloud"
constr = f'mysql+pymysql://{host}/{db}'
con = create_engine(constr, pool_recycle=60, connect_args={
'read_default_file': app.config["REPLICA_FILE"],
}, poolclass=NullPool)
return con
def maybe_snapshot(
snapshot_type, wiki, con, limit,
timedelta=datetime.timedelta(hours=11), period_start=None, period_end=None,
periodicity=None
):
session = Session(bind=con)
if snapshot_type == 'periodical':
if period_end > datetime.datetime.now():
abort(400, 'End period is greater than today, count would be incomplete')
existing_snapshot = session.query(Snapshot).filter(
Snapshot.wiki == wiki,
Snapshot.type == snapshot_type,
Snapshot.limit == limit,
Snapshot.period_start == period_start,
Snapshot.period_end == period_end,
Snapshot.periodicity == periodicity
)
else:
existing_snapshot = session.query(Snapshot).filter(
Snapshot.wiki == wiki,
Snapshot.timestamp > datetime.datetime.now() - timedelta,
Snapshot.type == snapshot_type,
Snapshot.limit == limit,
)
existing_snapshot = existing_snapshot.first()
if existing_snapshot is None:
print('No snapshot, querying replicas')
session.close()
return (True, None)
session.close()
return (False, existing_snapshot.to_dict())
@app.route('/snapshots')
def snapshots():
wiki = request.args.get('wiki', 'ptwiki')
type = request.args.get('type', 'recent')
limit = request.args.get('limit', '100')
before = request.args.get('before', '')
if before == '':
before = datetime.datetime.now()
else:
try:
before = datetime.datetime.fromisoformat(before)
except Exception:
raise TypeError("Before date cannot be parsed. Is it ISO 8601?")
after = request.args.get('after', '')
if after == '':
after = datetime.datetime.now() - datetime.timedelta(days=30)
else:
try:
after = datetime.datetime.fromisoformat(after)
except Exception:
raise TypeError("After date cannot be parsed. Is it ISO 8601?")
snapshot_con = create_snapshot_data_connection()
session = Session(bind=snapshot_con)
if type == 'periodical':
snapshots = session.query(Snapshot).filter(
Snapshot.wiki == wiki,
Snapshot.period_start > after,
Snapshot.type == type,
Snapshot.limit == limit,
).group_by(Snapshot.period_start).order_by(Snapshot.period_start.asc())
else:
snapshots = session.query(Snapshot).filter(
Snapshot.wiki == wiki,
Snapshot.timestamp >= after,
Snapshot.timestamp < before,
Snapshot.type == type,
Snapshot.limit == limit,
).order_by(Snapshot.timestamp.asc())
out = [snap.to_dict() for snap in snapshots]
session.close()
snapshot_con.dispose()
return out
@app.route('/wikis')
def wikis():
snapshot_con = create_snapshot_data_connection()
session = Session(bind=snapshot_con)
wikis = session.query(Snapshot.wiki).distinct()
out = [wiki[0] for wiki in wikis]
session.close()
snapshot_con.dispose()
return out
@app.route('/')
def index():
return send_file('static/lutz/dist/index.html')
@app.route('/<path:path>')
def static_files(path):
return send_from_directory('static/lutz/dist', path)