File size: 29,643 Bytes
d8cf478
 
8db40cd
 
 
d8cf478
8db40cd
d8cf478
 
 
 
efabdf9
6154c13
d8cf478
355fb10
 
 
 
 
d8cf478
 
 
 
52d1750
6154c13
dc11fb3
3498a52
f245408
d8cf478
0e538d2
 
 
7f9e80f
0e538d2
 
efabdf9
577dd09
 
8704528
577dd09
355fb10
 
 
 
52d1750
d8cf478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db40cd
 
 
 
a8cd875
8db40cd
 
 
 
 
a8cd875
 
8db40cd
a8cd875
 
8db40cd
 
 
a8cd875
8db40cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2628969
8db40cd
 
 
 
2628969
8db40cd
2628969
 
8db40cd
2628969
 
8db40cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8cf478
 
e376aff
 
 
 
 
 
 
8db40cd
 
d8cf478
 
 
5f0d39e
d8cf478
0e538d2
 
 
d8cf478
 
5b74576
d8cf478
 
18ab870
3035b84
efabdf9
5b74576
5f0d39e
 
1c9dfec
d8cf478
5b74576
3035b84
 
 
5b74576
1c9dfec
 
 
d8cf478
3035b84
1c9dfec
 
 
3035b84
52d1750
1c9dfec
 
 
52d1750
e376aff
 
 
 
 
 
 
8db40cd
e376aff
d8cf478
 
e376aff
 
 
 
 
 
 
8db40cd
e376aff
2628969
355fb10
1c9dfec
355fb10
1c9dfec
 
18ab870
d8cf478
 
 
0b5bfca
d8cf478
1ed82ec
0b5bfca
efabdf9
e376aff
 
 
8db40cd
 
 
e376aff
652d8f6
 
 
1ed82ec
0b5bfca
 
 
 
1ed82ec
652d8f6
3035b84
e376aff
 
 
 
 
 
7839697
e376aff
 
 
296080f
 
 
e376aff
7839697
d8cf478
5b74576
 
d8cf478
 
8704528
d8cf478
efabdf9
d8cf478
 
efabdf9
d8cf478
 
 
 
 
 
 
 
 
 
 
cd2003a
d8cf478
 
 
 
 
 
 
 
 
 
 
 
5b74576
efabdf9
63c3662
efabdf9
5b74576
efabdf9
 
 
 
 
 
 
3035b84
efabdf9
1ed82ec
efabdf9
 
cd2003a
efabdf9
 
 
 
1ed82ec
efabdf9
 
5b74576
efabdf9
5b74576
3035b84
efabdf9
e376aff
 
 
 
 
 
 
 
 
 
efabdf9
e376aff
 
 
 
 
 
 
 
efabdf9
e376aff
 
 
5b74576
efabdf9
 
e376aff
 
 
 
efabdf9
3035b84
652d8f6
 
cd2003a
652d8f6
 
 
 
 
 
3035b84
652d8f6
 
 
 
 
 
 
cd2003a
 
 
652d8f6
 
 
 
3035b84
 
 
652d8f6
 
 
 
3035b84
7f9e80f
18ab870
a0ba53d
 
 
 
 
18ab870
 
a0ba53d
18ab870
 
 
 
 
7f9e80f
18ab870
 
 
 
 
 
 
 
 
 
577dd09
18ab870
 
 
 
 
 
 
 
 
 
 
 
3035b84
18ab870
5b74576
7f9e80f
3035b84
7f9e80f
 
 
 
 
 
 
3035b84
7f9e80f
 
5b74576
18ab870
7f9e80f
 
 
 
3035b84
7f9e80f
 
5b74576
7f9e80f
3035b84
7f9e80f
3035b84
7f9e80f
3035b84
 
7f9e80f
 
5b74576
7f9e80f
3035b84
7f9e80f
 
 
 
18ab870
7f9e80f
 
3035b84
7f9e80f
 
5b74576
18ab870
7f9e80f
 
18ab870
7f9e80f
3035b84
7f9e80f
3035b84
7f9e80f
 
5b74576
18ab870
3035b84
7f9e80f
3035b84
7f9e80f
3035b84
 
7f9e80f
355fb10
 
 
3058723
 
 
3d497f3
3058723
 
 
 
 
 
355fb10
dff5e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c3662
e376aff
1c9dfec
 
 
 
3d497f3
 
 
 
 
 
 
 
 
 
63c3662
 
e376aff
 
63c3662
 
 
 
 
 
 
e376aff
 
63c3662
 
 
 
 
 
 
e376aff
 
 
 
 
 
 
 
 
 
 
63c3662
e376aff
 
63c3662
e376aff
 
63c3662
e376aff
 
 
 
 
 
63c3662
e376aff
63c3662
 
 
 
 
e376aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f245408
 
 
 
e376aff
f245408
 
 
 
e376aff
 
f245408
 
 
 
 
 
 
e376aff
 
f245408
7f9e80f
8704528
52d1750
 
dc11fb3
52d1750
 
 
12536a4
52d1750
 
12536a4
dc11fb3
330cbe3
 
 
 
dc11fb3
00d49a3
5f0d39e
00d49a3
e376aff
5f0d39e
00d49a3
5b74576
00d49a3
3498a52
577dd09
 
00d49a3
577dd09
 
3035b84
5b74576
577dd09
3498a52
577dd09
 
00d49a3
577dd09
 
3035b84
5b74576
577dd09
3035b84
3498a52
8704528
3498a52
e376aff
8704528
5b74576
8704528
 
5b74576
8704528
3035b84
5b74576
8704528
 
4a24b3a
 
 
 
 
 
5f0d39e
6154c13
 
7839697
dc11fb3
 
6154c13
 
d8cf478
7839697
5b74576
7839697
 
 
e376aff
 
7839697
 
e376aff
 
 
 
 
 
 
 
 
 
 
 
 
d8cf478
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
import gradio as gr
import pandas as pd
import gzip
import shutil
import os
import logging
from huggingface_hub import hf_hub_download


from scripts.metrics import (
    compute_weekly_metrics_by_market_creator,
    compute_daily_metrics_by_market_creator,
    compute_winning_metrics_by_trader,
)
from scripts.retention_metrics import (
    prepare_retention_dataset,
    calculate_wow_retention_by_type,
    calculate_cohort_retention,
)
from tabs.trader_plots import (
    plot_trader_metrics_by_market_creator,
    default_trader_metric,
    trader_metric_choices,
    get_metrics_text,
    plot_winning_metric_per_trader,
    get_interpretation_text,
    plot_total_bet_amount,
    plot_active_traders,
)
from tabs.daily_graphs import (
    get_current_week_data,
    plot_daily_metrics,
    trader_daily_metric_choices,
    default_daily_metric,
)
from scripts.utils import get_traders_family
from tabs.market_plots import (
    plot_kl_div_per_market,
    plot_total_bet_amount_per_trader_per_market,
)
from tabs.retention_plots import (
    plot_wow_retention_by_type,
    plot_cohort_retention_heatmap,
)


def get_logger():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    # stream handler and formatter
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
    return logger


logger = get_logger()


def load_all_data():

    # all trades profitability
    # Download the compressed file
    gz_filepath_trades = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="all_trades_profitability.parquet.gz",
        repo_type="dataset",
    )

    parquet_filepath_trades = gz_filepath_trades.replace(".gz", "")
    parquet_filepath_trades = parquet_filepath_trades.replace("all", "")

    with gzip.open(gz_filepath_trades, "rb") as f_in:
        with open(parquet_filepath_trades, "wb") as f_out:
            shutil.copyfileobj(f_in, f_out)

    # Now read the decompressed parquet file
    df1 = pd.read_parquet(parquet_filepath_trades)

    # closed_markets_div
    closed_markets_df = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="closed_markets_div.parquet",
        repo_type="dataset",
    )
    df2 = pd.read_parquet(closed_markets_df)

    # daily_info
    daily_info_df = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="daily_info.parquet",
        repo_type="dataset",
    )
    df3 = pd.read_parquet(daily_info_df)

    # unknown traders
    unknown_df = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="unknown_traders.parquet",
        repo_type="dataset",
    )
    df4 = pd.read_parquet(unknown_df)

    # retention activity
    gz_file_path_ret = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="retention_activity.parquet.gz",
        repo_type="dataset",
    )
    parquet_file_path_ret = gz_file_path_ret.replace(".gz", "")

    with gzip.open(gz_file_path_ret, "rb") as f_in:
        with open(parquet_file_path_ret, "wb") as f_out:
            shutil.copyfileobj(f_in, f_out)
    df5 = pd.read_parquet(parquet_file_path_ret)
    # os.remove(parquet_file_path_ret)

    # active_traders.parquet
    active_traders_df = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="active_traders.parquet",
        repo_type="dataset",
    )
    df6 = pd.read_parquet(active_traders_df)

    # weekly_mech_calls.parquet
    all_mech_calls_df = hf_hub_download(
        repo_id="valory/Olas-predict-dataset",
        filename="weekly_mech_calls.parquet",
        repo_type="dataset",
    )
    df7 = pd.read_parquet(all_mech_calls_df)
    return df1, df2, df3, df4, df5, df6, df7


def prepare_data():

    (
        all_trades,
        closed_markets,
        daily_info,
        unknown_traders,
        retention_df,
        active_traders,
        all_mech_calls,
    ) = load_all_data()

    all_trades["creation_date"] = all_trades["creation_timestamp"].dt.date

    # nr-trades variable
    volume_trades_per_trader_and_market = (
        all_trades.groupby(["trader_address", "title"])["roi"]
        .count()
        .reset_index(name="nr_trades_per_market")
    )

    traders_data = pd.merge(
        all_trades, volume_trades_per_trader_and_market, on=["trader_address", "title"]
    )
    daily_info["creation_date"] = daily_info["creation_timestamp"].dt.date
    unknown_traders["creation_date"] = unknown_traders["creation_timestamp"].dt.date
    # adding the trader family column
    traders_data["trader_family"] = traders_data.apply(
        lambda x: get_traders_family(x), axis=1
    )
    # print(traders_data.head())

    traders_data = traders_data.sort_values(by="creation_timestamp", ascending=True)
    unknown_traders = unknown_traders.sort_values(
        by="creation_timestamp", ascending=True
    )
    traders_data["month_year_week"] = (
        traders_data["creation_timestamp"]
        .dt.to_period("W")
        .dt.start_time.dt.strftime("%b-%d-%Y")
    )
    unknown_traders["month_year_week"] = (
        unknown_traders["creation_timestamp"]
        .dt.to_period("W")
        .dt.start_time.dt.strftime("%b-%d-%Y")
    )
    closed_markets["month_year_week"] = (
        closed_markets["opening_datetime"]
        .dt.to_period("W")
        .dt.start_time.dt.strftime("%b-%d-%Y")
    )
    return (
        traders_data,
        closed_markets,
        daily_info,
        unknown_traders,
        retention_df,
        active_traders,
        all_mech_calls,
    )


(
    traders_data,
    closed_markets,
    daily_info,
    unknown_traders,
    raw_retention_df,
    active_traders,
    all_mech_calls,
) = prepare_data()

retention_df = prepare_retention_dataset(
    retention_df=raw_retention_df, unknown_df=unknown_traders
)
print("max date of retention df")
print(max(retention_df.creation_timestamp))

demo = gr.Blocks()
# get weekly metrics by market creator: qs, pearl or all.
weekly_metrics_by_market_creator = compute_weekly_metrics_by_market_creator(
    traders_data=traders_data, all_mech_calls=all_mech_calls
)
weekly_o_metrics_by_market_creator = compute_weekly_metrics_by_market_creator(
    traders_data=traders_data, all_mech_calls=all_mech_calls, trader_filter="Olas"
)
weekly_non_olas_metrics_by_market_creator = pd.DataFrame()
if len(traders_data.loc[traders_data["staking"] == "non_Olas"]) > 0:
    weekly_non_olas_metrics_by_market_creator = (
        compute_weekly_metrics_by_market_creator(
            traders_data, all_mech_calls, trader_filter="non_Olas"
        )
    )
weekly_unknown_trader_metrics_by_market_creator = None
if len(unknown_traders) > 0:
    weekly_unknown_trader_metrics_by_market_creator = (
        compute_weekly_metrics_by_market_creator(
            traders_data=unknown_traders,
            all_mech_calls=None,
            trader_filter=None,
            unknown_trader=True,
        )
    )

# just for all traders
weekly_winning_metrics = compute_winning_metrics_by_trader(
    traders_data=traders_data, unknown_info=unknown_traders
)
weekly_winning_metrics_olas = compute_winning_metrics_by_trader(
    traders_data=traders_data, unknown_info=unknown_traders, trader_filter="Olas"
)
weekly_non_olas_winning_metrics = pd.DataFrame()
if len(traders_data.loc[traders_data["staking"] == "non_Olas"]) > 0:
    weekly_non_olas_winning_metrics = compute_winning_metrics_by_trader(
        traders_data=traders_data,
        unknown_info=unknown_traders,
        trader_filter="non_Olas",
    )

with demo:
    gr.HTML("<h1>Traders monitoring dashboard </h1>")
    gr.Markdown("This app shows the weekly performance of the traders in Olas Predict.")

    with gr.Tabs():
        with gr.TabItem("🔥 Weekly metrics"):
            with gr.Row():
                gr.Markdown("# Weekly metrics of all traders")
            with gr.Row():
                trader_details_selector = gr.Dropdown(
                    label="Select a weekly trader metric",
                    choices=trader_metric_choices,
                    value=default_trader_metric,
                )

            with gr.Row():
                with gr.Column(scale=3):
                    trader_markets_plot = plot_trader_metrics_by_market_creator(
                        metric_name=default_trader_metric,
                        traders_df=weekly_metrics_by_market_creator,
                    )
                with gr.Column(scale=1):
                    trade_details_text = get_metrics_text(trader_type=None)

            def update_trader_details(trader_detail):
                return plot_trader_metrics_by_market_creator(
                    metric_name=trader_detail,
                    traders_df=weekly_metrics_by_market_creator,
                )

            trader_details_selector.change(
                update_trader_details,
                inputs=trader_details_selector,
                outputs=trader_markets_plot,
            )

            with gr.Row():
                gr.Markdown("# Weekly metrics of 🌊 Olas traders")
            with gr.Row():
                trader_o_details_selector = gr.Dropdown(
                    label="Select a weekly trader metric",
                    choices=trader_metric_choices,
                    value=default_trader_metric,
                )

            with gr.Row():
                with gr.Column(scale=3):
                    o_trader_markets_plot = plot_trader_metrics_by_market_creator(
                        metric_name=default_trader_metric,
                        traders_df=weekly_o_metrics_by_market_creator,
                    )
                with gr.Column(scale=1):
                    trade_details_text = get_metrics_text(trader_type="Olas")

            def update_a_trader_details(trader_detail):
                return plot_trader_metrics_by_market_creator(
                    metric_name=trader_detail,
                    traders_df=weekly_o_metrics_by_market_creator,
                )

            trader_o_details_selector.change(
                update_a_trader_details,
                inputs=trader_o_details_selector,
                outputs=o_trader_markets_plot,
            )
            if len(weekly_non_olas_metrics_by_market_creator) > 0:
                # Non-Olas traders graph
                with gr.Row():
                    gr.Markdown("# Weekly metrics of Non-Olas traders")
                with gr.Row():
                    trader_no_details_selector = gr.Dropdown(
                        label="Select a weekly trader metric",
                        choices=trader_metric_choices,
                        value=default_trader_metric,
                    )

                with gr.Row():
                    with gr.Column(scale=3):
                        trader_no_markets_plot = plot_trader_metrics_by_market_creator(
                            metric_name=default_trader_metric,
                            traders_df=weekly_non_olas_metrics_by_market_creator,
                        )
                    with gr.Column(scale=1):
                        trade_details_text = get_metrics_text(trader_type="non_Olas")

                def update_no_trader_details(trader_detail):
                    return plot_trader_metrics_by_market_creator(
                        metric_name=trader_detail,
                        traders_df=weekly_non_olas_metrics_by_market_creator,
                    )

                trader_no_details_selector.change(
                    update_no_trader_details,
                    inputs=trader_no_details_selector,
                    outputs=trader_no_markets_plot,
                )
            # Unknown traders graph
            if weekly_unknown_trader_metrics_by_market_creator is not None:
                with gr.Row():
                    gr.Markdown("# Weekly metrics of Unclassified traders")
                with gr.Row():
                    trader_u_details_selector = gr.Dropdown(
                        label="Select a weekly trader metric",
                        choices=trader_metric_choices,
                        value=default_trader_metric,
                    )

                with gr.Row():
                    with gr.Column(scale=3):
                        trader_u_markets_plot = plot_trader_metrics_by_market_creator(
                            metric_name=default_trader_metric,
                            traders_df=weekly_unknown_trader_metrics_by_market_creator,
                        )
                    with gr.Column(scale=1):
                        trade_details_text = get_metrics_text(
                            trader_type="unclassified"
                        )

                def update_u_trader_details(trader_detail):
                    return plot_trader_metrics_by_market_creator(
                        metric_name=trader_detail,
                        traders_df=weekly_unknown_trader_metrics_by_market_creator,
                    )

                trader_u_details_selector.change(
                    update_u_trader_details,
                    inputs=trader_u_details_selector,
                    outputs=trader_u_markets_plot,
                )
        with gr.TabItem("📅 Daily metrics"):
            live_trades_current_week = get_current_week_data(trades_df=daily_info)
            if len(live_trades_current_week) > 0:
                live_metrics_by_market_creator = (
                    compute_daily_metrics_by_market_creator(
                        live_trades_current_week, trader_filter=None, live_metrics=True
                    )
                )
            else:
                live_metrics_by_market_creator = pd.DataFrame()
            with gr.Row():
                gr.Markdown("# Daily live metrics for all trades")
            with gr.Row():
                trade_live_details_selector = gr.Dropdown(
                    label="Select a daily live metric",
                    choices=trader_daily_metric_choices,
                    value=default_daily_metric,
                )

            with gr.Row():
                with gr.Column(scale=3):
                    trade_live_details_plot = plot_daily_metrics(
                        metric_name=default_daily_metric,
                        trades_df=live_metrics_by_market_creator,
                    )
                with gr.Column(scale=1):
                    trade_details_text = get_metrics_text(daily=True)

            def update_trade_live_details(trade_detail, trade_live_details_plot):
                new_a_plot = plot_daily_metrics(
                    metric_name=trade_detail, trades_df=live_metrics_by_market_creator
                )
                return new_a_plot

            trade_live_details_selector.change(
                update_trade_live_details,
                inputs=[trade_live_details_selector, trade_live_details_plot],
                outputs=[trade_live_details_plot],
            )
            # Olas traders
            with gr.Row():
                gr.Markdown("# Daily live metrics for 🌊 Olas traders")
            with gr.Row():
                o_trader_live_details_selector = gr.Dropdown(
                    label="Select a daily live metric",
                    choices=trader_daily_metric_choices,
                    value=default_daily_metric,
                )

            with gr.Row():
                with gr.Column(scale=3):
                    o_trader_live_details_plot = plot_daily_metrics(
                        metric_name=default_daily_metric,
                        trades_df=live_metrics_by_market_creator,
                        trader_filter="Olas",
                    )
                with gr.Column(scale=1):
                    trade_details_text = get_metrics_text(daily=True)

            def update_a_trader_live_details(trade_detail, a_trader_live_details_plot):
                o_trader_plot = plot_daily_metrics(
                    metric_name=trade_detail,
                    trades_df=live_metrics_by_market_creator,
                    trader_filter="Olas",
                )
                return o_trader_plot

            o_trader_live_details_selector.change(
                update_a_trader_live_details,
                inputs=[o_trader_live_details_selector, o_trader_live_details_plot],
                outputs=[o_trader_live_details_plot],
            )
            with gr.Row():
                gr.Markdown("# Daily live metrics for Non-Olas traders")
            with gr.Row():
                no_trader_live_details_selector = gr.Dropdown(
                    label="Select a daily live metric",
                    choices=trader_daily_metric_choices,
                    value=default_daily_metric,
                )

            with gr.Row():
                with gr.Column(scale=3):
                    no_trader_live_details_plot = plot_daily_metrics(
                        metric_name=default_daily_metric,
                        trades_df=live_metrics_by_market_creator,
                        trader_filter="non_Olas",
                    )
                with gr.Column(scale=1):
                    trade_details_text = get_metrics_text(daily=True)

            def update_na_trader_live_details(
                trade_detail, no_trader_live_details_plot
            ):
                no_trader_plot = plot_daily_metrics(
                    metric_name=trade_detail,
                    trades_df=live_metrics_by_market_creator,
                    trader_filter="non_Olas",
                )
                return no_trader_plot

            no_trader_live_details_selector.change(
                update_na_trader_live_details,
                inputs=[no_trader_live_details_selector, no_trader_live_details_plot],
                outputs=[no_trader_live_details_plot],
            )
        with gr.TabItem("🪝 Retention metrics (WIP)"):
            with gr.Row():
                gr.Markdown("# Wow retention by trader type")
            with gr.Row():
                gr.Markdown(
                    """
                    Activity based on mech interactions for Olas and non_Olas traders and based on trading acitivity for the unclassified ones.
                    - Olas trader: agent using Mech, with a service ID and the corresponding safe in the registry
                    - Non-Olas trader: agent using Mech, with no service ID
                    - Unclassified trader: agent (safe/EOAs) not using Mechs
                    """
                )

            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("## Wow retention in Pearl markets")
                    wow_retention = calculate_wow_retention_by_type(
                        retention_df, market_creator="pearl"
                    )
                    wow_retention_plot = plot_wow_retention_by_type(
                        wow_retention=wow_retention
                    )
                with gr.Column(scale=1):
                    gr.Markdown("## Wow retention in Quickstart markets")
                    wow_retention = calculate_wow_retention_by_type(
                        retention_df, market_creator="quickstart"
                    )
                    wow_retention_plot = plot_wow_retention_by_type(
                        wow_retention=wow_retention
                    )

            with gr.Row():
                gr.Markdown("# Cohort retention graphs")
            with gr.Row():
                gr.Markdown(
                    "The Cohort groups are organized by cohort weeks. A trader is part of a cohort group/week where it was detected the FIRST activity ever of that trader."
                )
            with gr.Row():
                gr.Markdown(
                    """
                    Week 0 for a cohort group is the same cohort week of the FIRST detected activity ever of that trader. 
                    Only two values are possible for this Week 0:

                    1. 100% if the cohort size is > 0, meaning all traders active that first cohort week
                    2. 0% if the cohort size = 0, meaning no totally new traders started activity that cohort week.
                    """
                )
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("## Cohort retention of pearl traders")
                    gr.Markdown("### Cohort retention of 🌊 Olas traders")
                    cohort_retention_olas_pearl = calculate_cohort_retention(
                        df=retention_df, market_creator="pearl", trader_type="Olas"
                    )
                    cohort_retention_plot1 = plot_cohort_retention_heatmap(
                        retention_matrix=cohort_retention_olas_pearl, cmap="Purples"
                    )
                with gr.Column(scale=1):
                    gr.Markdown("## Cohort retention of quickstart traders")
                    gr.Markdown("### Cohort retention of 🌊 Olas traders")
                    cohort_retention_olas_qs = calculate_cohort_retention(
                        df=retention_df, market_creator="quickstart", trader_type="Olas"
                    )
                    cohort_retention_plot4 = plot_cohort_retention_heatmap(
                        retention_matrix=cohort_retention_olas_qs,
                        cmap="Purples",
                    )
                    # # non_Olas
                    # cohort_retention_non_olas_pearl = calculate_cohort_retention(
                    #     df=retention_df, market_creator="pearl", trader_type="non_Olas"
                    # )
                    # if len(cohort_retention_non_olas_pearl) > 0:
                    #     gr.Markdown("## Cohort retention of Non-Olas traders")
                    #     cohort_retention_plot2 = plot_cohort_retention_heatmap(
                    #         retention_matrix=cohort_retention_non_olas_pearl,
                    #         cmap=sns.color_palette("light:goldenrod", as_cmap=True),
                    #     )
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("## Cohort retention of pearl traders")
                    cohort_retention_unclassified_pearl = calculate_cohort_retention(
                        df=retention_df,
                        market_creator="pearl",
                        trader_type="unclassified",
                    )
                    if len(cohort_retention_unclassified_pearl) > 0:
                        gr.Markdown("### Cohort retention of unclassified traders")
                        cohort_retention_plot3 = plot_cohort_retention_heatmap(
                            retention_matrix=cohort_retention_unclassified_pearl,
                            cmap="Greens",
                        )
                with gr.Column(scale=1):
                    gr.Markdown("## Cohort retention in quickstart traders")
                    cohort_retention_unclassified_qs = calculate_cohort_retention(
                        df=retention_df,
                        market_creator="quickstart",
                        trader_type="unclassified",
                    )
                    if len(cohort_retention_unclassified_qs) > 0:
                        gr.Markdown("### Cohort retention of unclassified traders")
                        cohort_retention_plot6 = plot_cohort_retention_heatmap(
                            retention_matrix=cohort_retention_unclassified_qs,
                            cmap="Greens",
                        )
                    # # non_Olas
                    # cohort_retention_non_olas_qs = calculate_cohort_retention(
                    #     df=retention_df,
                    #     market_creator="quickstart",
                    #     trader_type="non_Olas",
                    # )
                    # if len(cohort_retention_non_olas_qs) > 0:
                    #     gr.Markdown("## Cohort retention of Non-Olas traders")
                    #     cohort_retention_plot5 = plot_cohort_retention_heatmap(
                    #         retention_matrix=cohort_retention_non_olas_qs,
                    #         cmap=sns.color_palette("light:goldenrod", as_cmap=True),
                    #     )
        with gr.TabItem("⚙️ Active traders"):
            with gr.Row():
                gr.Markdown("# Active traders for all markets by trader categories")
            with gr.Row():
                active_traders_plot = plot_active_traders(active_traders)

            with gr.Row():
                gr.Markdown("# Active traders for Pearl markets by trader categories")
            with gr.Row():
                active_traders_plot_pearl = plot_active_traders(
                    active_traders, market_creator="pearl"
                )

            with gr.Row():
                gr.Markdown(
                    "# Active traders for Quickstart markets by trader categories"
                )
            with gr.Row():
                active_traders_plot_qs = plot_active_traders(
                    active_traders, market_creator="quickstart"
                )

        with gr.TabItem("📉 Markets Kullback–Leibler divergence"):
            with gr.Row():
                gr.Markdown(
                    "# Weekly Market Prediction Accuracy for Closed Markets (Kullback-Leibler Divergence)"
                )
            with gr.Row():
                gr.Markdown(
                    "Aka, how much off is the market prediction’s accuracy from the real outcome of the event. Values capped at 20 for market outcomes completely opposite to the real outcome."
                )
            with gr.Row():
                trade_details_text = get_metrics_text()
            with gr.Row():
                with gr.Column(scale=3):
                    kl_div_plot = plot_kl_div_per_market(closed_markets=closed_markets)
                with gr.Column(scale=1):
                    interpretation = get_interpretation_text()

        with gr.TabItem("💰 Money invested per trader type"):
            with gr.Row():
                gr.Markdown("# Weekly total bet amount per trader type for all markets")
                gr.Markdown("## Computed only for trader agents using the mech service")
            with gr.Row():
                total_bet_amount = plot_total_bet_amount(
                    traders_data, market_filter="all"
                )

            with gr.Row():
                gr.Markdown(
                    "# Weekly total bet amount per trader type for Pearl markets"
                )
            with gr.Row():
                o_trader_total_bet_amount = plot_total_bet_amount(
                    traders_data, market_filter="pearl"
                )

            with gr.Row():
                gr.Markdown(
                    "# Weekly total bet amount per trader type for Quickstart markets"
                )
            with gr.Row():
                no_trader_total_bet_amount = plot_total_bet_amount(
                    traders_data, market_filter="quickstart"
                )

        with gr.TabItem("💰 Money invested per market"):
            with gr.Row():
                gr.Markdown("# Weekly bet amounts per market for all traders")
                gr.Markdown("## Computed only for trader agents using the mech service")
            with gr.Row():
                bet_amounts = plot_total_bet_amount_per_trader_per_market(traders_data)

            with gr.Row():
                gr.Markdown("# Weekly bet amounts per market for 🌊 Olas traders")
            with gr.Row():
                o_trader_bet_amounts = plot_total_bet_amount_per_trader_per_market(
                    traders_data, trader_filter="Olas"
                )

            # with gr.Row():
            #     gr.Markdown("# Weekly bet amounts per market for Non-Olas traders")
            # with gr.Row():
            #     no_trader_bet_amounts = plot_total_bet_amount_per_trader_per_market(
            #         traders_data, trader_filter="non_Olas"
            #     )

        with gr.TabItem("🎖️Weekly winning trades % per trader"):
            with gr.Row():
                gr.Markdown("# Weekly winning trades percentage from all traders")
            with gr.Row():
                metrics_text = get_metrics_text()
            with gr.Row():
                winning_metric = plot_winning_metric_per_trader(weekly_winning_metrics)

            with gr.Row():
                gr.Markdown("# Weekly winning trades percentage from 🌊 Olas traders")
            with gr.Row():
                metrics_text = get_metrics_text()
            with gr.Row():
                winning_metric_olas = plot_winning_metric_per_trader(
                    weekly_winning_metrics_olas
                )

            # # non_Olas traders
            # if len(weekly_non_olas_winning_metrics) > 0:
            #     with gr.Row():
            #         gr.Markdown(
            #             "# Weekly winning trades percentage from Non-Olas traders"
            #         )
            #     with gr.Row():
            #         metrics_text = get_metrics_text()
            #     with gr.Row():
            #         winning_metric = plot_winning_metric_per_trader(
            #             weekly_non_olas_winning_metrics
            #         )

demo.queue(default_concurrency_limit=40).launch()