-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes_per_day_of_week.py
163 lines (132 loc) · 5.28 KB
/
bytes_per_day_of_week.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
import altair as alt
import numpy as np
import pandas as pd
import infra.dask
import infra.pd
import infra.platform
def create_all_flows(dask_client):
typical_flows = infra.dask.read_parquet(
"data/clean/flows/typical_fqdn_category_local_TM_DIV_none_INDEX_start")[["end", "protocol", "bytes_up", "bytes_down"]]
p2p_flows = infra.dask.read_parquet("data/clean/flows/p2p_TM_DIV_none_INDEX_start")[["end", "protocol", "bytes_a_to_b", "bytes_b_to_a"]]
nouser_flows = infra.dask.read_parquet("data/clean/flows/nouser_TM_DIV_none_INDEX_start")[["end", "protocol", "bytes_a_to_b", "bytes_b_to_a"]]
typical_flows["bytes_total"] = typical_flows["bytes_up"] + typical_flows["bytes_down"]
p2p_flows["bytes_total"] = p2p_flows["bytes_a_to_b"] + p2p_flows["bytes_b_to_a"]
nouser_flows["bytes_total"] = nouser_flows["bytes_a_to_b"] + nouser_flows["bytes_b_to_a"]
typical_flows = typical_flows.reset_index()[["start", "end", "bytes_total", "protocol"]]
p2p_flows = p2p_flows.reset_index()[["start", "end", "bytes_total", "protocol"]]
nouser_flows = nouser_flows.reset_index()[["start", "end", "bytes_total", "protocol"]]
all_flows = typical_flows.append(p2p_flows).append(nouser_flows)
all_flows = all_flows.set_index("start").repartition(partition_size="128M", force=True)
infra.dask.clean_write_parquet(all_flows, "data/clean/flows/all_TM_DIV_none_INDEX_start")
def reduce_to_pandas(outfile, dask_client):
flows = infra.dask.read_parquet(
"data/clean/flows/all_TM_DIV_none_INDEX_start")[["bytes_total"]]
# Compress to days
flows = flows.reset_index()
flows["start_bin"] = flows["start"].dt.floor("d")
flows["day"] = flows["start"].dt.day_name()
flows = flows.set_index("start_bin")
# Do the grouping
flows = flows.groupby(["start_bin", "day"]).sum()
flows = flows.compute()
infra.pd.clean_write_parquet(flows, outfile)
def make_plot(infile):
grouped_flows = infra.pd.read_parquet(infile)
grouped_flows = grouped_flows.reset_index()
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday']
grouped_flows["GB"] = grouped_flows["bytes_total"] / (1000**3)
working_times = grouped_flows.loc[(grouped_flows["start_bin"] < "2019-07-30") | (grouped_flows["start_bin"] > "2019-08-31")]
grouped_flows["outage"] = "Outage"
grouped_flows.loc[(grouped_flows["start_bin"] < "2019-07-30") | (grouped_flows["start_bin"] > "2019-08-31"), "outage"] = "Normal"
print(grouped_flows)
outages = grouped_flows.loc[grouped_flows["GB"] < 0.9]
print(outages)
alt.Chart(working_times).mark_boxplot().encode(
x=alt.X('day:N',
sort=days,
title="Day of Week"
),
y=alt.Y('GB:Q',
title="GB Per Day"
),
).save(
"renders/bytes_per_day_of_week_boxplot_exclude_outage.png",
scale_factor=2,
)
bars = alt.Chart(working_times).mark_bar().encode(
x=alt.X('day:N',
sort=days,
title=""
),
y=alt.Y('GB:Q',
title="Mean GB Per Normal Day",
aggregate="mean"
),
)
ci = alt.Chart(working_times).mark_errorbar(extent="ci").encode(
x=alt.X('day:N',
sort=days,
title="",
),
y=alt.Y('GB:Q',
title="(95% Bootstrap CI)",
),
)
mean_and_ci = bars + ci
mean_and_ci.save(
"renders/bytes_per_day_of_week_exclude_outage_mean_ci.png",
scale_factor=2,
)
overplot = alt.Chart(grouped_flows).mark_point(opacity=0.3).encode(
x=alt.X('day:N',
sort=days,
title=""
),
y=alt.Y('GB:Q',
title="GB Per Day"
),
color=alt.Color(
"outage",
title="Condition",
)
)
overplot.save(
"renders/bytes_per_day_of_week_overplot.png",
scale_factor=2,
)
(mean_and_ci | overplot).resolve_scale(y="shared").save(
"renders/bytes_per_day_of_week_compound.png",
scale_factor=2,
)
# plot = alt.Chart(grouped_flows).mark_area().encode(
# x=alt.X("start_bin:T",
# title="Time",
# axis=alt.Axis(labels=True),
# ),
# y=alt.Y("sum(GB):Q",
# title="Fraction of Traffic Per Week(GB)",
# stack="normalize",
# ),
# # shape="direction",
# color="name",
# detail="name",
# ).properties(
# # title="Local Service Use",
# width=500,
# ).save("renders/throughput_per_protocol_trends_normalized.png",
# scale_factor=2
# )
if __name__ == "__main__":
platform = infra.platform.read_config()
graph_temporary_file = "scratch/graphs/bytes_per_day_of_week"
if platform.large_compute_support:
print("Performing compute operations")
client = infra.dask.setup_dask_client()
# create_all_flows(client)
reduce_to_pandas(outfile=graph_temporary_file, dask_client=client)
client.close()
if platform.altair_support:
print("Performing vis operations")
make_plot(graph_temporary_file)
print("Done!")