-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
333 lines (261 loc) · 9.9 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
import pandas as pd
import streamlit as st
from pinotdb import connect
from datetime import datetime
import plotly.express as px
import time
st.set_page_config(layout="wide")
# conn = connect(host='localhost', port=9000, path='/sql', scheme='http')
conn = connect(host='broker.pinot.flrg1s.s7e.startree.cloud', port=443, path='/query/sql', scheme='https',
username=st.secrets["username"], password=st.secrets["password"])
def overview():
st.header("Overview")
query = """
select count(*) FILTER(WHERE ts > ago('PT1M')) AS events1Min,
count(*) FILTER(WHERE ts <= ago('PT1M') AND ts > ago('PT2M')) AS events1Min2Min,
distinctcount(user) FILTER(WHERE ts > ago('PT1M')) AS users1Min,
distinctcount(user) FILTER(WHERE ts <= ago('PT1M') AND ts > ago('PT2M')) AS users1Min2Min,
distinctcount(domain) FILTER(WHERE ts > ago('PT1M')) AS domains1Min,
distinctcount(domain) FILTER(WHERE ts <= ago('PT1M') AND ts > ago('PT2M')) AS domains1Min2Min
from Wiki_Events
where ts > ago('PT2M')
limit 1
"""
curs = conn.cursor()
curs.execute(query)
df_summary = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
metric1, metric2, metric3 = st.columns(3)
metric1.metric(
label="Changes",
value=float(df_summary['events1Min'].values[0]),
delta=float(df_summary['events1Min'].values[0] - df_summary['events1Min2Min'].values[0])
)
metric2.metric(
label="Users",
value=float(df_summary['users1Min'].values[0]),
delta=float(df_summary['users1Min'].values[0] - df_summary['users1Min2Min'].values[0])
)
metric3.metric(
label="Domains",
value=float(df_summary['domains1Min'].values[0]),
delta=float(df_summary['domains1Min'].values[0] - df_summary['domains1Min2Min'].values[0])
)
query = """
select ToDateTime(DATETRUNC('MINUTE', ts), 'yyyy-MM-dd hh:mm:ss') AS dateMin, count(*) AS changes,
distinctcount(user) AS users,
distinctcount(domain) AS domains
from Wiki_Events
where ts > ago('PT1H')
group by dateMin
order by dateMin desc
LIMIT 30
"""
curs.execute(query)
df_ts = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
df_ts_melt = pd.melt(df_ts, id_vars=['dateMin'], value_vars=['changes', 'users', 'domains'])
fig = px.line(df_ts_melt, x='dateMin', y="value", color='variable',
color_discrete_sequence=['blue', 'red', 'green'])
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40), title="Changes/Users/Domains per minute")
fig.update_yaxes(range=[0, df_ts["changes"].max() * 1.1])
st.plotly_chart(fig, use_container_width=True)
st.header("Recent Changes")
query = """
select ts, user, title, domain
from Wiki_Events
order by ts desc
LIMIT 20
"""
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
curs.close()
hide_table_row_index = """
<style>
tbody th {display:none}
.blank {display:none}
</style>
"""
# Inject CSS with Markdown
st.markdown(hide_table_row_index, unsafe_allow_html=True)
st.table(df)
def whos_making_changes():
st.header("Who's making changes?")
query = """
select bot, count(*) AS changes
from Wiki_Events
group by bot
"""
curs = conn.cursor()
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
fig = px.pie(df, names="bot", values="changes", color_discrete_sequence=['#0b263f', '#ccc'],
title="Bots vs Non Bots")
fig.update_xaxes(categoryorder='category descending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
query = """
select user, count(user) AS changes
from Wiki_Events
group by user
order by changes DESC
LIMIT 10
"""
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
fig = px.bar(df, x="changes", y="user", color_discrete_sequence=['#0b263f'] * len(df), title="Top Users")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
query = """
select user, count(user) AS changes
from Wiki_Events
WHERE bot = True
group by user
order by changes DESC
LIMIT 10
"""
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
fig = px.bar(df, x="changes", y="user", color_discrete_sequence=['#0b263f'] * len(df), title="Top Bots")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
query = """
select user, count(user) AS changes
from Wiki_Events
WHERE bot = False
group by user
order by changes DESC
LIMIT 10
"""
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
curs.close()
fig = px.bar(df, x="changes", y="user", color_discrete_sequence=['#0b263f'] * len(df), title="Top Non Bots")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
def what_changes():
st.header("What changes made?")
query = """
select domain, count(user) AS changes
from Wiki_Events
group by domain
order by changes DESC
LIMIT 10
"""
curs = conn.cursor()
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
fig = px.bar(df, x="changes", y="domain", color_discrete_sequence=['#0b263f'] * len(df), title="By Domain")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
query = """
select type, count(user) AS changes
from Wiki_Events
group by type
order by changes DESC
LIMIT 10
"""
curs.execute(query)
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
curs.close()
fig = px.bar(df, x="changes", y="type", color_discrete_sequence=['#0b263f'] * len(df), title="Types of changes")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
def drill_down():
st.header("Drill Down By User")
curs = conn.cursor()
curs.execute("""
select user, count(user) AS changes
from Wiki_Events
group by user
order by changes DESC
LIMIT 30
""")
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
users = list(df["user"].values)
def select_user_callback():
st.session_state['selected_user'] = st.session_state.select_user
if 'selected_user' not in st.session_state:
selected_user = st.selectbox('Select User', users,
key='select_user', on_change=select_user_callback
)
else:
selected_user = st.selectbox('Select User', users,
users.index(
st.session_state.selected_user) if st.session_state.selected_user in users else 0,
key='select_user',
on_change=select_user_callback
)
curs = conn.cursor()
curs.execute("""
select count(user) AS changes
from Wiki_Events
WHERE user = %(user)s
""", {"user": selected_user})
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
st.markdown(f"""
Changes: **{'{:,}'.format(df['changes'].values[0])}**
""")
query = """
select domain, count(user) AS changes
from Wiki_Events
WHERE user = %(user)s
group by domain
order by changes DESC
LIMIT 10
"""
curs.execute(query, {"user": selected_user})
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
fig = px.bar(df, x="changes", y="domain", color_discrete_sequence=['#0b263f'] * len(df), title="By Domain")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
query = """
select type, count(user) AS changes
from Wiki_Events
WHERE user = %(user)s
group by type
order by changes DESC
LIMIT 10
"""
curs.execute(query, {"user": selected_user})
df = pd.DataFrame(curs, columns=[item[0] for item in curs.description])
curs.close()
fig = px.bar(df, x="changes", y="type", color_discrete_sequence=['#0b263f'] * len(df), title="Types of changes")
fig.update_yaxes(categoryorder='max ascending')
fig['layout'].update(margin=dict(l=0, r=0, b=0, t=40))
st.write(fig)
PAGES = {
"Overview": overview,
"Who's making changes?": whos_making_changes,
"Where changes were made?": what_changes,
"Drill Down By User": drill_down
}
st.sidebar.title('Wikipedia Recent Changes')
now = datetime.now()
dt_string = now.strftime("%d %B %Y %H:%M:%S")
st.sidebar.write(f"Last update: {dt_string}")
if not "sleep_time" in st.session_state:
st.session_state.sleep_time = 2
if not "auto_refresh" in st.session_state:
st.session_state.auto_refresh = True
auto_refresh = st.sidebar.checkbox('Auto Refresh?', st.session_state.auto_refresh)
if auto_refresh:
number = st.sidebar.number_input('Refresh rate in seconds', value=st.session_state.sleep_time)
st.session_state.sleep_time = number
selection = st.sidebar.radio("Go to", list(PAGES.keys()))
page = PAGES[selection]
page()
st.markdown("""
<style>
section.main[tabindex='0'] div[data-testid='stVerticalBlock'] div.element-container:nth-child(1):has(> iframe) {
display: none;
}
</style>
""", unsafe_allow_html=True)
if auto_refresh:
time.sleep(number)
st.experimental_rerun()