-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
609 lines (508 loc) · 14.5 KB
/
plot.py
File metadata and controls
609 lines (508 loc) · 14.5 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
'''Plotting functions used for https://aryadee.dev/blog/rate-limiting-algorithms
'''
import sys
import json
import pandas as pd
import rich.traceback
from rich.pretty import pprint
from plotly import graph_objects as go
from plotly.subplots import make_subplots
rich.traceback.install() # prettier traceback
MARGIN = dict(
t = 40,
b = 20,
l = 35,
r = 20
)
def plot_fixed_window(data: dict, title_append=''):
"""Plot the fixed window data"""
df = pd.DataFrame(data['plot'])
fig = go.Figure()
df['time'] = df['time_ms'] / 1000 # convert to seconds
window_starts = list(df[df['counter'] == 1]['time'])
window_ends = list(map(lambda x: x + data['window_length_ms'] / 1000, window_starts))
for idx, (window_start, window_end) in enumerate(zip(window_starts, window_ends)):
fig.add_vrect(
x0 = window_start,
x1 = window_end,
fillcolor = "gray",
opacity = 0.05,
layer = "below",
line_width = 0,
)
fig.add_vline(
x = window_start,
line_width = 3,
line_color = "darkgreen",
layer = "below",
opacity = 0.5,
line_dash = "solid"
)
fig.add_vline(
x = window_end,
line_width = 3,
line_color = "darkred",
layer = "below",
opacity = 0.7,
line_dash = "solid",
)
window_df = df[(df['time'] >= window_start) & (df['time'] < window_end) ]
# df_duplicated = window_df.assign(counter=window_df['counter'] - 1)
df_duplicated = window_df.copy()
df_duplicated.loc[df_duplicated['status'] == 'OK', 'counter'] -= 1
df_result = pd.concat([window_df, df_duplicated], ignore_index=True).sort_values(['time', 'counter'], ascending=[True, True])
fig.add_trace(
go.Scatter(
x=df_result['time'],
y=df_result['counter'],
name=f"counter #{idx}",
mode = "lines",
line_color = "gainsboro",
opacity = 0.7
)
)
# the limit line
fig.add_hline(
y = data['limit'],
line_width = 1,
line_color = "deeppink",
line_dash = "dash",
opacity = 1,
)
fig.add_annotation(
x = 0,
y = data['limit'],
text = "limit",
showarrow = False,
textangle = 270,
xshift = -30,
xref = 'paper',
yref = 'y',
font = dict(color = "deeppink",)
)
# the OKs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "OK"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "OK"]['time']),
name = "OK",
mode = "markers",
marker = dict(
color = "darkturquoise",
size = 10,
),
)
)
# the DENIEDs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "DENIED"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "DENIED"]['time']),
name = "DENIED",
mode = "markers",
marker = dict(
color = "crimson",
symbol = "x",
size = 10,
),
)
)
get_num_oks(df, data['window_length_ms'], fig )
fig.update_layout(
title_text = "fixed_window() " + title_append,
xaxis_title_text = "time [s]",
template = "plotly_dark",
margin = MARGIN
)
return fig
def plot_enforced_avg(data: dict, title_append=''):
"""Plot the enforced average data"""
df = pd.DataFrame(data['plot'])
fig = go.Figure()
df['time'] = df['time_ms'] / 1000 # convert to seconds
# pprint(df)
# the OKs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "OK"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "OK"]['time']),
name = "OK",
mode = "markers",
marker = dict(
color = "darkturquoise",
size = 10,
),
)
)
# the DENIEDs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "DENIED"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "DENIED"]['time']),
name = "DENIED",
mode = "markers",
marker = dict(
color = "crimson",
symbol = "x",
size = 10,
),
)
)
# the windows
exclusion_window = 1 / data['limit_rps']
window_starts = list(df[df['status'] == 'OK']['time'])
window_ends = list(map(lambda x: x + exclusion_window, window_starts))
for window_start, window_end in zip(window_starts, window_ends):
fig.add_vrect(
x0 = window_start,
x1 = window_end,
fillcolor = "gray",
opacity = 0.05,
layer = "below",
line_width = 0,
)
fig.add_vline(
x = window_start,
line_width = 3,
line_color = "darkgreen",
layer = "below",
opacity = 0.5,
line_dash = "solid"
)
fig.add_vline(
x = window_end,
line_width = 3,
line_color = "darkred",
layer = "below",
opacity = 0.7,
line_dash = "solid",
)
fig.add_hline(
y = data['limit_rps'],
line_width = 1,
line_color = "deeppink",
line_dash = "dash",
opacity = 1,
)
fig.add_annotation(
x = 0,
y = data['limit_rps'],
text = "limit",
showarrow = False,
textangle = 270,
xshift = -30,
xref = 'paper',
yref = 'y',
font = dict(color = "deeppink",)
)
fig.update_layout(
title_text = "exclusion_window() " + title_append,
xaxis_title_text = "time [s]",
# yaxis_title_text = "",
template = "plotly_dark",
margin = MARGIN
)
get_num_oks(df, 1000, fig )
return fig
def plot_sliding_window(data: dict, title_append: str = ""):
"""Plot the sliding window data."""
df = pd.DataFrame(data['plot'])
fig = go.Figure()
df['time'] = df['time_ms'] / 1000 # convert to seconds
# pprint(df)
# the OKs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "OK"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "OK"]['time']),
name = "OK",
mode = "markers",
marker = dict(
color = "darkturquoise",
size = 10,
),
)
)
# the DENIEDs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "DENIED"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "DENIED"]['time']),
name = "DENIED",
mode = "markers",
marker = dict(
color = "crimson",
symbol = "x",
size = 10,
),
)
)
# the limit
fig.add_hline(
y = data['limit'],
line_width = 1,
line_color = "deeppink",
line_dash = "dash",
opacity = 1,
)
fig.add_annotation(
x = 0,
y = data['limit'],
text = "limit",
showarrow = False,
textangle = 270,
xshift = -30,
xref = 'paper',
yref = 'y',
font = dict(color = "deeppink",)
)
window_starts = df.loc[df['new'] == True, 'time'].tolist()
window_starts_inf = window_starts + [float('inf')] # dupe widnow_starts w/ inf at the end so we can iterate over pairs
for idx, window_start in enumerate(window_starts):
window_df = df[(df['time'] >= window_starts_inf[idx]) & (df['time'] < window_starts_inf[idx + 1])]
last_ok = window_df[window_df['status'] == 'OK'].tail(1)['time'].values[0]
window_end = last_ok + data['window_length_ms'] / 1000
fig.add_vrect(
x0 = window_start,
x1 = window_end,
fillcolor = "gray",
opacity = 0.05,
layer = "below",
line_width = 0,
)
fig.add_vline(
x = window_start,
line_width = 2,
line_color = "darkgreen",
layer = "below",
opacity = 0.5,
line_dash = "solid"
)
fig.add_vline(
x = window_end,
line_width = 2,
line_color = "darkred",
layer = "below",
opacity = 0.5,
line_dash = "solid",
)
df_filtered = window_df.loc[df['status'] == 'OK']
# create new dataframe with incremented counter
df_duplicated = df_filtered.assign(counter=df_filtered['counter'] - 1)
# concatenate original dataframe with new dataframe
df_result = pd.concat([window_df, df_duplicated], ignore_index=True).sort_values(['time', 'counter'], ascending=[True, True])
fig.add_trace(
go.Scatter(
x=df_result['time'],
y=df_result['counter'],
name=f"counter #{idx}",
mode = "lines",
line_color = "gainsboro",
opacity = 0.7
)
)
fig.update_layout(
title_text = "sliding_window() " + title_append,
xaxis_title_text = "time [s]",
# yaxis_title_text = "counter",
template = "plotly_dark",
xaxis_range = [0, data['duration'] + 1],
yaxis_range = [-1, 10],
margin = MARGIN
)
get_num_oks(df, data['window_length_ms'], fig )
return fig
def plot_leaky_bucket(data: dict, title_append=''):
"""Plot the leaky bucket data."""
df = pd.DataFrame(data['plot'])
fig = go.Figure()
df['time'] = df['time_ms'] / 1000 # convert to seconds
# the OKs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "OK"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "OK"]['time']),
name = "OK",
mode = "markers",
marker = dict(
color = "darkturquoise",
size = 10,
),
)
)
# the DENIEDs
fig.add_trace(
go.Scatter(
x = df[df['status'] == "DENIED"]['time'],
# choose y to be 0 for all OKs
y = [0] * len(df[df['status'] == "DENIED"]['time']),
name = "DENIED",
mode = "markers",
marker = dict(
color = "crimson",
symbol = "x",
size = 10,
),
)
)
# the limit
fig.add_hline(
y = data['limit'],
line_width = 1,
line_color = "deeppink",
line_dash = "dash",
opacity = 1,
)
fig.add_annotation(
x = 0,
y = data['limit'],
text = "limit",
showarrow = False,
textangle = 270,
xshift = -30,
xref = 'paper',
yref = 'y',
font = dict(color = "deeppink",)
)
window_starts = df.loc[df['new'] == True, 'time'].tolist()
window_starts_inf = window_starts + [float('inf')] # dupe widnow_starts w/ inf at the end so we can iterate over pairs
for idx, window_start in enumerate(window_starts):
window_df = df[(df['time'] >= window_starts_inf[idx]) & (df['time'] < window_starts_inf[idx + 1])]
last_ok = window_df[window_df['status'] == 'OK'].tail(1)['time'].values[0]
if data['mode'] == 'soft':
window_end = last_ok + (window_df[window_df['time'] == last_ok]['counter'].values[0] / data['limit']) * data['window_length_ms'] / 1000
else:
window_end = last_ok + (window_df[window_df['time'] == last_ok]['counter'].values[0]) * data['window_length_ms'] / 1000
fig.add_vrect(
x0 = window_start,
x1 = window_end,
fillcolor = "gray",
opacity = 0.05,
layer = "below",
line_width = 0,
)
fig.add_vline(
x = window_start,
line_width = 2,
line_color = "darkgreen",
layer = "below",
opacity = 0.5,
line_dash = "solid"
)
fig.add_vline(
x = window_end,
line_width = 2,
line_color = "darkred",
layer = "below",
opacity = 0.5,
line_dash = "solid",
)
df_filtered = window_df.loc[df['status'] == 'OK']
# create new dataframe with incremented counter
df_duplicated = df_filtered.assign(counter=df_filtered['counter'] - 1)
# concatenate original dataframe with new dataframe
df_result = pd.concat([window_df, df_duplicated], ignore_index=True).sort_values(['time', 'counter'], ascending=[True, True])
fig.add_trace(
go.Scatter(
x=df_result['time'],
y=df_result['counter'],
name=f"counter #{idx}",
mode = "lines",
line_color = "gainsboro",
opacity = 0.7
)
)
fig.update_layout(
title_text = "leaky_bucket() " + title_append,
xaxis_title_text = "time [s]",
# yaxis_title_text = "counter",
template = "plotly_dark",
xaxis_range = [0, data['duration'] + 1],
yaxis_range = [-1, 10],
margin = MARGIN
)
get_num_oks(df, data['window_length_ms'], fig)
return fig
def get_num_oks(df, window_len_ms: float, fig):
''' gets the number OKs in the last window_len_ms and adds it to the figure.
note: handles everything input in ms, but plots in s.
'''
df = df[['time_ms', 'status']]
# create new df with new rows for when the window ends
new_rows = df[df['status'] == 'OK'].copy()
new_rows['time_ms'] = new_rows['time_ms'] + window_len_ms
new_rows['status'] = ''
df = pd.concat([df, new_rows], ignore_index=True).drop_duplicates(subset=['time_ms']).sort_values(['time_ms', 'status'], ascending=[True, True])
num_oks = []
for end_time in df['time_ms']:
start_time = max(end_time - window_len_ms, 0)
mask = (df['time_ms'] > start_time) & (df['time_ms'] < end_time) | (df['time_ms'] == end_time) # all the times within the window
# get the number of OKs within the window
num_oks.append(df.loc[mask, 'status'].eq('OK').sum())
df['num_oks'] = num_oks
fig.add_trace(
go.Scatter(
x = df['time_ms'] / 1000,
y = df['num_oks'],
name = "num OKs",
line=dict(shape='hv', color='yellow'),
mode = "lines",
line_color = "yellow",
opacity = 0.7
)
)
def figs_to_subplot(figs: list[go.Figure], title: str, duration:float, **kwargs):
''' takes a list of figures and returns a subplot with them all in it.
'''
subplot = make_subplots(
rows=len(figs),
cols=1,
shared_xaxes=True,
**kwargs
)
for i, fig in enumerate(figs):
for trace in fig.data:
if 'subplot_titles' in kwargs:
trace.legendgroup = kwargs['subplot_titles'][i]
trace.legendgrouptitle.text = kwargs['subplot_titles'][i]
subplot.add_trace(trace, row=i+1, col=1)
for shape in fig.layout.shapes:
subplot.add_shape(shape, row=i+1, col=1)
# for annotation in fig.layout.annotations:
# subplot.add_annotation(annotation, row=i+1, col=1)
subplot.update_layout(
template = "plotly_dark",
xaxis_range = [0, duration + 1],
# yaxis_range = [-1, LIMIT * 2],
legend=dict(groupclick="toggleitem"),
title_text = title,
margin = MARGIN
)
subplot.update_xaxes(title_text="time [s]", row=len(figs), col=1)
return subplot
def debugger_is_active() -> bool:
"""Return true if the debugger is currently active"""
return hasattr(sys, 'gettrace') and sys.gettrace() is not None
if __name__ == "__main__":
if debugger_is_active():
file_path = "./data/fixed_window.json"
else:
if len(sys.argv) < 2:
print("Please provide the name of the JSON file as a command line argument.")
sys.exit(1)
file_path = sys.argv[1]
with open(file_path) as f:
data: dict = json.load(f)
filename = file_path.split("/")[-1].split(".")[0]
match filename:
case "fixed_window":
plot_fixed_window(data).show()
case _:
raise ValueError(f"No matching plot function for filename: {filename}")