-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_domains.py
385 lines (328 loc) · 10.8 KB
/
health_domains.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
""" Computing active and registered users on the network over time
"""
from datetime import datetime, timedelta
import altair as alt
import pandas as pd
import infra.constants
import infra.dask
import infra.pd
import infra.platform
# Contact tracing apps: PeduliLindungi, Bersatu Lawan COVID-19,
HEALTH_FQDN_TOKENS = {
"infeksi",
"kemkes",
"covid",
"corona",
"korona",
"kesehatan",
"kesegaran",
"kewarasan",
"dokter",
"doktor",
"pengobat",
"mengobati",
"merawat",
"penyakit",
"who",
"health",
"virus",
"vaccine",
"vaksin",
"PeduliLindungi",
"peduli",
"lindungi",
"medical",
"telemedicine",
"medicine",
"obat"
}
FALSE_POSITIVE_EXLUSIONS = {
"wholesale", # Ad network, not WHO : )
"antivirus", # Computer antivirus
"mobihealthplus.com.", # Antivirus
"bras-base-hpwhon5203w-grc-03-65-94-57-54.dsl.bell.ca", # Telecom
"the-who-8.core.ynet.pl", # Telecom
"adminmusic-test.mobihealthplus.com",
"whos.amung.us", # Analytics
"samsunghealth.com", # Phone personal health tracking
"samsungcloud.com", # Phone personal health tracking
"coronalabs.com", # Online gaming
"unhealthyrange.com", # Generic example code generating bad domain???
"bpjs-kesehatan.go.id", # Public social welfare program cash transfer
"wholeless-mileage.volia.net", # No response, seems not health related
"whoareyou.ff.garena.com", # Game survey
"hologfx.com", # Anime media sharing
"acrobat.com", # Not medicine in indonesian : )
"adobe.com", # Related to acrobat
"tanobato.wordpress.com", # Religious blog
"sobatdrama.net.", # Media
"sobatkerja.zendesk.com.", # Sobat != obat
"sobatmateri.com", # sobat != obat
"sobatnewss.blogspot.com.", # sobat != obat
"sobat-otomotif.blogspot.com", # sobat != obat
"comm.miui.com.", # alternative android UI
"jualobatpembesarpenisherbalklg.blogspot.com.", # Spam "male enhancement health"
"dunia-kesehatan01.blogspot.com.", # Blog removed and address seized!
"kesehatan.kontan.co.id.", # Indonesian news website health section
"health.detik.com.", # Indonesian news website health section
"health.kompas.com.", # News website health section
"healthcareitnews.com.", # News website
}
def _contains_any_token(test_string, token_list):
matches = filter(lambda x: x in test_string, token_list)
matched = False
for match in matches:
matched = True
break
return matched
def _make_primary_domain_from_fqdn(fqdn):
parts = fqdn.strip().strip(".").split(".")
# Special case for amp domains
if len(parts) == 4 and parts[1:4] == ["cdn", "ampproject", "org"]:
source_host = parts[0].split("-")
parts = source_host
# Special case for alodokter firebase links
if fqdn == "alodokter.page.link." or fqdn == "alodokterdev.page.link." or fqdn == "alodokterstaging.page.link.":
return "alodokter.com."
# Special case for onesignal.com
if fqdn == "doktersehat.onesignal.com.":
return "doktersehat.com."
# All country code TLDs are two characters long, and will have sub-tlds (i.e. .co.uk ~ .com)
is_cc_domain = bool(len(parts[-1]) == 2)
if is_cc_domain:
return ".".join(parts[max(-len(parts), -3):]) + "."
else:
return ".".join(parts[max(-len(parts), -2):]) + "."
def categorize_all_fqdns(df):
"""Categorizes an fqdn, currently as health or not health
"""
def _categorize_health(fqdn):
test_string = fqdn.lower()
return bool(
_contains_any_token(test_string, HEALTH_FQDN_TOKENS) and
not _contains_any_token(test_string, FALSE_POSITIVE_EXLUSIONS)
)
df["is_health"] = df["fqdn"].apply(_categorize_health)
return df
def make_health_domain_plots(infile):
""" Top-level plotting function to generate multiple related plots from the same annotated dataset.
"""
df = infra.pd.read_parquet(infile)
df = categorize_all_fqdns(df)
temp = df.loc[df["is_health"]]
print(temp["fqdn"].unique())
_make_volume_comparison_plot(df)
_make_relative_volume_plot(df)
_make_unique_users_plot(df)
_make_individual_domain_plot(df)
_make_individual_domain_unique_users_plot(df)
def _make_individual_domain_unique_users_plot(df):
df["GB_total"] = (df["bytes_up"] + df["bytes_down"])/(1000**3)
df["primary_domain"] = df["fqdn"].apply(_make_primary_domain_from_fqdn)
df = df.loc[df["is_health"] == True]
# Consolidate by week instead of by day
# df = df[
# ["day", "primary_domain", "user"]
# ].groupby(
# [pd.Grouper(key="day", freq="W-MON"), "primary_domain"]
# )["user"].nunique().reset_index()
df = df[
["day", "primary_domain", "user"]
].groupby(
[pd.Grouper(key="day", freq="W-MON"), "primary_domain"]
)
df = df["user"].nunique().reset_index()
week_range = pd.DataFrame({"day": pd.date_range(infra.constants.MIN_DATE, infra.constants.MAX_DATE + timedelta(days=1), freq="W-MON")})
domain_range = pd.DataFrame({"primary_domain": df["primary_domain"].unique()}, dtype=object)
dense_index = infra.pd.cartesian_product(week_range, domain_range)
df = df.merge(dense_index, on=["day", "primary_domain"], how="outer")
df = df.fillna(0)
df["timedelta"] = (df["day"] - datetime.strptime('2020-04-01 00:00:00', '%Y-%m-%d %H:%M:%S')).dt.to_pytimedelta()
df["week_number"] = df["timedelta"].apply(lambda x: int(x.days/7))
df = df.drop("timedelta", axis=1)
alt.Chart(df).mark_line(opacity=0.5, interpolate='step-after').encode(
x=alt.X(
"day:T"
),
y=alt.Y(
"user",
title="Weekly Unique Users",
axis=alt.Axis(labels=True),
# scale=alt.Scale(
# type="symlog"
# ),
),
color=alt.Color(
"primary_domain:N",
scale=alt.Scale(scheme="tableau20"),
),
).properties(
width=500,
).save(
"renders/health_domain_individual_domain_weekly.png",
scale_factor=2,
)
alt.Chart(df).mark_rect(opacity=1.0).encode(
x=alt.X(
"week_number:O",
),
y=alt.Y(
"primary_domain:N",
title="Weekly Unique Users",
# axis=alt.Axis(labels=True),
# scale=alt.Scale(
# type="symlog"
# ),
),
color=alt.Color(
"user:Q",
# scale=alt.Scale(scheme="tableau20"),
),
).properties(
width=500,
).save(
"renders/health_domain_individual_domain_weekly_heatmap.png",
scale_factor=2,
)
def _make_individual_domain_plot(df):
df["GB_total"] = (df["bytes_up"] + df["bytes_down"])/(1000**3)
df["primary_domain"] = df["fqdn"].apply(_make_primary_domain_from_fqdn)
df = df.loc[df["is_health"] == True]
# Consolidate by week instead of by day
df = df[
["day", "primary_domain", "GB_total"]
].groupby(
[pd.Grouper(key="day", freq="W-MON"), "primary_domain"]
).sum().reset_index()
alt.Chart(df).mark_line(opacity=1.0).encode(
x=alt.X(
"day:T"
),
y=alt.Y(
"GB_total:Q",
title="Weekly Total Traffic (GB)",
axis=alt.Axis(labels=True),
# scale=alt.Scale(
# type="symlog"
# ),
),
color=alt.Color(
"primary_domain:N",
scale=alt.Scale(scheme="tableau20"),
),
).properties(
width=500,
).save(
"renders/health_domain_individual_domain_weekly.png",
scale_factor=2,
)
def _make_unique_users_plot(df):
df = df.loc[df["is_health"] == True]
# Consolidate by week instead of by day
print(df)
temp_count = df.loc[df["fqdn"] == "www.alodokter.com"]
print(temp_count)
df = df[
["day", "user"]
].groupby(
[pd.Grouper(key="day", freq="W-MON")]
)["user"].nunique().reset_index()
alt.Chart(df).mark_line(opacity=1.0, interpolate='step-after').encode(
x=alt.X(
"day:T"
),
y=alt.Y(
"user:Q",
title="Weekly Unique Users",
axis=alt.Axis(labels=True),
# scale=alt.Scale(
# type="symlog"
# ),
),
).properties(
width=500,
).save(
"renders/health_domain_unique_users.png",
scale_factor=2,
)
def _make_relative_volume_plot(df):
df = df.loc[df["is_health"] == True]
# Consolidate by week instead of by day
df = df[
["day", "bytes_up", "bytes_down"]
].groupby(
[pd.Grouper(key="day", freq="W-MON")]
).sum().reset_index()
df["bytes"] = df["bytes_up"] + df["bytes_down"]
alt.Chart(df).mark_line(opacity=1.0, interpolate='step-after').encode(
x=alt.X(
"day:T"
),
y=alt.Y(
"bytes:Q",
title="Weekly Traffic(B)",
axis=alt.Axis(labels=True),
# scale=alt.Scale(
# type="symlog"
# ),
),
).properties(
width=500,
).save(
"renders/health_domain_isolated_magnitude.png",
scale_factor=2,
)
def _make_volume_comparison_plot(df):
# Consolidate by week instead of by day
df = df[
["day", "is_health", "bytes_up", "bytes_down"]
].groupby(
[pd.Grouper(key="day", freq="W-MON"), "is_health"]
).sum().reset_index()
df = df.melt(
id_vars=["is_health", "day"],
value_vars=["bytes_up", "bytes_down"],
var_name="direction",
value_name="bytes"
)
df["bytes"] = df["bytes"] / 1000**3
alt.Chart(df).mark_point(opacity=1.0).encode(
x=alt.X(
"day:T"
),
y=alt.Y(
"bytes:Q",
title="Weekly Traffic(GB)",
axis=alt.Axis(labels=True),
scale=alt.Scale(
type="symlog"
),
),
# shape="direction",
color=alt.Color(
"direction",
title="Type",
),
shape=alt.Shape(
"is_health:N"
)
).properties(
width=500,
).save(
"renders/health_domain_overall_magnitude.png",
scale_factor=2,
)
if __name__ == "__main__":
# Module specific format options
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', None)
pd.set_option('display.max_rows', 40)
platform = infra.platform.read_config()
graph_temporary_file = "data/aggregates/bytes_per_user_per_domain_per_day.parquet"
if platform.large_compute_support:
pass
if platform.altair_support:
make_health_domain_plots(graph_temporary_file)
# make_org_plot(graph_temporary_file)
# make_category_aggregate_bar_chart(graph_temporary_file)