File size: 31,674 Bytes
b0e562e
f670803
b0e562e
 
 
 
 
e9034e9
5998589
b0e562e
 
 
 
 
 
 
4b78e58
b0e562e
 
c0fff3a
b0e562e
 
 
 
 
9ae9289
340fbae
244b6ac
4b78e58
5998589
b0e562e
 
f51e3c7
c8cafbf
b0e562e
244b6ac
e4c408e
3972952
 
c0fff3a
e4c408e
b0e562e
244b6ac
b0e562e
 
e9034e9
 
 
 
 
5998589
 
 
 
e9034e9
 
 
 
 
5998589
e95d2b3
 
5998589
 
 
 
e9034e9
 
5998589
e9034e9
 
 
 
 
 
5998589
e95d2b3
 
ee5d1f1
5998589
 
7485ae4
5998589
 
 
 
7485ae4
 
 
5998589
7485ae4
d81f624
b0e562e
5998589
b0e562e
5998589
 
b0e562e
 
 
 
 
 
 
 
4b78e58
9ae9289
 
 
 
7485ae4
9ae9289
 
4b78e58
9ae9289
 
4b78e58
9ae9289
 
4b78e58
9ae9289
 
 
 
 
 
 
4b78e58
 
9ae9289
 
 
 
 
 
4b78e58
9ae9289
 
4b78e58
9ae9289
b0e562e
 
9ae9289
b0e562e
 
9ae9289
 
 
 
 
 
b0e562e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5998589
b0e562e
 
 
 
 
5998589
 
b0e562e
 
 
5998589
b0e562e
 
 
 
f51e3c7
b0e562e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f51e3c7
b0e562e
 
 
 
 
 
 
f51e3c7
b0e562e
 
 
5998589
4b09f5e
5998589
4b09f5e
 
5998589
 
4b09f5e
 
 
 
5998589
 
9a5f3b6
282b1ac
4b09f5e
5998589
4b09f5e
 
5998589
 
 
725070c
5998589
 
725070c
5998589
725070c
7485ae4
5998589
 
b0e562e
 
 
 
 
 
3a48c45
b0e562e
3a48c45
b0e562e
3a48c45
b0e562e
f51e3c7
ee9e618
 
3a48c45
f51e3c7
b0e562e
3a48c45
c5a8738
 
 
0c24356
c5a8738
 
53e82b5
3a48c45
 
 
 
 
c931f22
 
ada7e18
c931f22
ada7e18
c931f22
3a48c45
5998589
 
 
3a48c45
5998589
 
 
3a48c45
5998589
 
 
 
 
 
 
 
 
 
 
3a48c45
 
5998589
 
f51e3c7
3a48c45
5998589
f51e3c7
5998589
53e82b5
3a48c45
5998589
3a48c45
5998589
 
 
f51e3c7
5998589
f51e3c7
5998589
f51e3c7
5998589
b0e562e
f51e3c7
b0e562e
 
 
3a48c45
b0e562e
 
 
 
 
 
 
 
 
 
 
 
 
 
f51e3c7
5998589
 
 
 
 
 
 
f51e3c7
b0e562e
 
 
f51e3c7
 
7485ae4
f51e3c7
 
7485ae4
b0e562e
 
ada7e18
 
1c785ab
 
ada7e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e562e
3a48c45
 
b0e562e
 
ada7e18
3a48c45
b0e562e
 
 
 
 
 
 
 
27c1eed
b0e562e
 
ada7e18
3a48c45
 
 
b0e562e
f51e3c7
b0e562e
 
 
 
 
 
ada7e18
 
 
 
 
 
 
 
 
3a48c45
244b6ac
 
 
 
 
f51e3c7
244b6ac
 
 
 
 
 
 
 
 
b0e562e
 
5998589
ad69e49
b0e562e
5998589
 
4b09f5e
5998589
 
 
 
 
 
 
 
 
 
b0e562e
c8cafbf
5998589
b0e562e
 
 
 
 
5998589
 
 
395520f
c0fff3a
5998589
c0fff3a
 
5998589
be5580b
5998589
244b6ac
b0e562e
37c1612
c8cafbf
244b6ac
395520f
e4c408e
3972952
 
e4c408e
 
244b6ac
 
b0e562e
 
395520f
f51e3c7
5998589
b0e562e
 
 
 
 
244b6ac
e4c408e
3972952
e4c408e
c0fff3a
244b6ac
b0e562e
 
 
 
c0fff3a
e4c408e
 
b0e562e
c7085e8
 
 
 
9b08675
c7085e8
 
 
 
5998589
 
 
b0e562e
 
 
4b78e58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5998589
b0e562e
f51e3c7
ee9e618
b0e562e
 
 
5998589
b0e562e
f51e3c7
5998589
 
b0e562e
5998589
b0e562e
 
 
 
5998589
b0e562e
 
 
 
 
5998589
b0e562e
f51e3c7
 
 
 
b0e562e
f51e3c7
b0e562e
 
 
37c1612
5998589
b0e562e
 
b984c23
b0e562e
 
 
 
5998589
b0e562e
5998589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f51e3c7
5998589
 
 
 
 
 
b0e562e
 
 
 
 
 
f51e3c7
5998589
 
ee9e618
5998589
b0e562e
 
5998589
 
 
 
b0e562e
 
 
7485ae4
5998589
 
 
7485ae4
b0e562e
 
5984077
f51e3c7
 
7485ae4
b0e562e
7485ae4
b0e562e
5998589
f51e3c7
b0e562e
5998589
b0e562e
f51e3c7
996ac2e
b76b31c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
996ac2e
c0fff3a
b76b31c
c0fff3a
b76b31c
996ac2e
c0fff3a
aa557eb
996ac2e
b0e562e
 
5998589
 
 
 
 
 
 
 
 
df9900d
 
 
 
aa0fa76
4feb8ae
df9900d
 
aa0fa76
4feb8ae
b0e562e
c931f22
ada7e18
c931f22
 
5998589
 
53e82b5
5998589
 
b0e562e
 
244b6ac
 
3a48c45
244b6ac
 
 
 
c931f22
 
 
 
 
 
 
5998589
4b78e58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f51e3c7
 
5998589
f51e3c7
5998589
b0e562e
 
 
 
f51e3c7
b0e562e
 
f51e3c7
 
b0e562e
5998589
b0e562e
5998589
 
 
b0e562e
 
5998589
f51e3c7
b0e562e
5998589
b0e562e
f51e3c7
b0e562e
 
 
 
 
 
5998589
b0e562e
 
 
5998589
 
b0e562e
 
 
 
 
5998589
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
import gradio as gr
from gradio_leaderboard import Leaderboard, ColumnFilter
import json
import os
import time
import requests
from huggingface_hub import HfApi, hf_hub_download
from huggingface_hub.errors import HfHubHTTPError
import backoff
from dotenv import load_dotenv
import pandas as pd
import random
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from datetime import datetime, timezone

# Load environment variables
load_dotenv(override=True)

# =============================================================================
# CONFIGURATION
# =============================================================================

AGENTS_REPO = "SWE-Arena/bot_data"  # HuggingFace dataset for assistant metadata
LEADERBOARD_FILENAME = f"{os.getenv('COMPOSE_PROJECT_NAME')}.json"
LEADERBOARD_REPO = "SWE-Arena/leaderboard_data"  # HuggingFace dataset for leaderboard data
LONGSTANDING_GAP_DAYS = 30  # Minimum days for an issue to be considered long-standing
MAX_RETRIES = 5

LEADERBOARD_COLUMNS = [
    ("Assistant", "string"),
    ("Website", "string"),
    ("Total Issues", "number"),
    ("Total Discussions", "number"),
    ("Total Wanted Issues", "number"),
    ("Issue Resolved Rate (%)", "number"),
    ("Discussion Resolved Rate (%)", "number"),
    ("Wanted Issue Resolved Rate (%)", "number"),
    ("Resolved Wanted Issues", "number"),
    ("Resolved Issues", "number"),
    ("Resolved Discussions", "number"),
]

# =============================================================================
# HUGGINGFACE API WRAPPERS WITH BACKOFF
# =============================================================================

def is_rate_limit_error(e):
    """Check if exception is a HuggingFace rate limit error (429)."""
    if isinstance(e, HfHubHTTPError):
        return e.response.status_code == 429
    return False


@backoff.on_exception(
    backoff.expo,
    HfHubHTTPError,
    max_tries=MAX_RETRIES,
    base=300,
    max_value=3600,
    giveup=lambda e: not is_rate_limit_error(e),
    on_backoff=lambda details: print(
        f"Rate limited. Retrying in {details['wait']/60:.1f} minutes ({details['wait']:.0f}s) - attempt {details['tries']}/5..."
    )
)
def list_repo_files_with_backoff(api, **kwargs):
    """Wrapper for api.list_repo_files() with exponential backoff for rate limits."""
    return api.list_repo_files(**kwargs)


@backoff.on_exception(
    backoff.expo,
    HfHubHTTPError,
    max_tries=MAX_RETRIES,
    base=300,
    max_value=3600,
    giveup=lambda e: not is_rate_limit_error(e),
    on_backoff=lambda details: print(
        f"Rate limited. Retrying in {details['wait']/60:.1f} minutes ({details['wait']:.0f}s) - attempt {details['tries']}/5..."
    )
)
def hf_hub_download_with_backoff(**kwargs):
    """Wrapper for hf_hub_download() with exponential backoff for rate limits."""
    return hf_hub_download(**kwargs)


# =============================================================================
# GITHUB USERNAME VALIDATION
# =============================================================================

def validate_github_username(identifier):
    """Verify that a GitHub identifier exists."""
    try:
        response = requests.get(f'https://api.github.com/users/{identifier}', timeout=10)
        return (True, "Username is valid") if response.status_code == 200 else (False, "GitHub identifier not found" if response.status_code == 404 else f"Validation error: HTTP {response.status_code}")
    except Exception as e:
        return False, f"Validation error: {str(e)}"


# =============================================================================
# HUGGINGFACE DATASET OPERATIONS
# =============================================================================

def load_agents_from_hf():
    """Load all assistant metadata JSON files from HuggingFace dataset."""
    try:
        api = HfApi()
        assistants = []

        # List all files in the repository
        files = list_repo_files_with_backoff(api=api, repo_id=AGENTS_REPO, repo_type="dataset")

        # Filter for JSON files only
        json_files = [f for f in files if f.endswith('.json')]

        # Download and parse each JSON file
        for json_file in json_files:
            try:
                file_path = hf_hub_download_with_backoff(
                    repo_id=AGENTS_REPO,
                    filename=json_file,
                    repo_type="dataset"
                )

                with open(file_path, 'r') as f:
                    agent_data = json.load(f)

                    # Only process assistants with status == "active"
                    if agent_data.get('status') != 'active':
                        continue

                    # Extract github_identifier from filename (e.g., "assistant[bot].json" -> "assistant[bot]")
                    filename_identifier = json_file.replace('.json', '')

                    # Add or override github_identifier to match filename
                    agent_data['github_identifier'] = filename_identifier

                    assistants.append(agent_data)

            except Exception as e:
                print(f"Warning: Could not load {json_file}: {str(e)}")
                continue

        print(f"Loaded {len(assistants)} assistants from HuggingFace")
        return assistants

    except Exception as e:
        print(f"Could not load assistants from HuggingFace: {str(e)}")
        return None


def get_hf_token():
    """Get HuggingFace token from environment variables."""
    token = os.getenv('HF_TOKEN')
    if not token:
        print("Warning: HF_TOKEN not found in environment variables")
    return token


def upload_with_retry(api, path_or_fileobj, path_in_repo, repo_id, repo_type, token, max_retries=5):
    """
    Upload file to HuggingFace with exponential backoff retry logic.

    Args:
        api: HfApi instance
        path_or_fileobj: Local file path to upload
        path_in_repo: Target path in the repository
        repo_id: Repository ID
        repo_type: Type of repository (e.g., "dataset")
        token: HuggingFace token
        max_retries: Maximum number of retry attempts

    Returns:
        True if upload succeeded, raises exception if all retries failed
    """
    delay = 2.0  # Initial delay in seconds

    for attempt in range(max_retries):
        try:
            api.upload_file(
                path_or_fileobj=path_or_fileobj,
                path_in_repo=path_in_repo,
                repo_id=repo_id,
                repo_type=repo_type,
                token=token
            )
            if attempt > 0:
                print(f"   Upload succeeded on attempt {attempt + 1}/{max_retries}")
            return True

        except Exception as e:
            if attempt < max_retries - 1:
                wait_time = delay + random.uniform(0, 1.0)
                print(f"   Upload failed (attempt {attempt + 1}/{max_retries}): {str(e)}")
                print(f"   Retrying in {wait_time:.1f} seconds...")
                time.sleep(wait_time)
                delay = min(delay * 2, 60.0)  # Exponential backoff, max 60s
            else:
                print(f"   Upload failed after {max_retries} attempts: {str(e)}")
                raise


def save_agent_to_hf(data):
    """Save a new assistant to HuggingFace dataset as {identifier}.json in root."""
    try:
        api = HfApi()
        token = get_hf_token()

        if not token:
            raise Exception("No HuggingFace token found. Please set HF_TOKEN in your Space settings.")

        identifier = data['github_identifier']
        filename = f"{identifier}.json"

        # Save locally first
        with open(filename, 'w') as f:
            json.dump(data, f, indent=2)

        try:
            # Upload to HuggingFace (root directory)
            upload_with_retry(
                api=api,
                path_or_fileobj=filename,
                path_in_repo=filename,
                repo_id=AGENTS_REPO,
                repo_type="dataset",
                token=token
            )
            print(f"Saved assistant to HuggingFace: {filename}")
            return True
        finally:
            # Always clean up local file, even if upload fails
            if os.path.exists(filename):
                os.remove(filename)

    except Exception as e:
        print(f"Error saving assistant: {str(e)}")
        return False


def load_leaderboard_data_from_hf():
    """
    Load leaderboard data and monthly metrics from HuggingFace dataset.

    Returns:
        dict: Dictionary with 'leaderboard', 'monthly_metrics', and 'metadata' keys
              Returns None if file doesn't exist or error occurs
    """
    try:
        token = get_hf_token()

        # Download file
        file_path = hf_hub_download_with_backoff(
            repo_id=LEADERBOARD_REPO,
            filename=LEADERBOARD_FILENAME,
            repo_type="dataset",
            token=token
        )

        # Load JSON data
        with open(file_path, 'r') as f:
            data = json.load(f)

        last_updated = data.get('metadata', {}).get('last_updated', 'Unknown')
        print(f"Loaded leaderboard data from HuggingFace (last updated: {last_updated})")

        return data

    except Exception as e:
        print(f"Could not load leaderboard data from HuggingFace: {str(e)}")
        return None


# =============================================================================
# UI FUNCTIONS
# =============================================================================

def create_monthly_metrics_plot(type="issue", top_n=5):
    """
    Create a Plotly figure with dual y-axes showing monthly metrics:
    - Left y-axis: Resolved Rate (%) as line curves
    - Right y-axis: Total count (Issues or Discussions) as bar charts

    Each assistant gets a unique color for both their line and bars.

    Args:
        type: Type of metrics to display - "issue" or "discussion" (default: "issue")
        top_n: Number of top assistants to show (default: 5)
    """
    # Determine metrics key and field names based on type
    if type == "issue":
        metrics_key = 'issue_monthly_metrics'
        total_field = 'total_issues'
        no_data_msg = "No issue data available for visualization"
        total_label = "Total Issues"
        print_msg = "issue"
    elif type == "discussion":
        metrics_key = 'discussion_monthly_metrics'
        total_field = 'total_discussions'
        no_data_msg = "No discussion data available for visualization"
        total_label = "Total Discussions"
        print_msg = "discussion"
    elif type == "wanted":
        metrics_key = 'wanted_issue_monthly_metrics'
        total_field = 'total_wanted'
        no_data_msg = "No wanted issue data available for visualization"
        total_label = "Total Wanted Issues"
        print_msg = "wanted issue"

    # Load from saved dataset
    saved_data = load_leaderboard_data_from_hf()

    if not saved_data or metrics_key not in saved_data:
        # Return an empty figure with a message
        fig = go.Figure()
        fig.add_annotation(
            text=no_data_msg,
            xref="paper", yref="paper",
            x=0.5, y=0.5, showarrow=False,
            font=dict(size=16)
        )
        fig.update_layout(
            title=None,
            xaxis_title=None,
            height=500
        )
        return fig

    metrics = saved_data[metrics_key]
    print(f"Loaded {print_msg} monthly metrics from saved dataset")

    # Apply top_n filter if specified
    if top_n is not None and top_n > 0 and metrics.get('assistants'):
        # Calculate total count for each assistant
        agent_totals = []
        for agent_name in metrics['assistants']:
            agent_data = metrics['data'].get(agent_name, {})
            total_count = sum(agent_data.get(total_field, []))
            agent_totals.append((agent_name, total_count))

        # Sort by total count and take top N
        agent_totals.sort(key=lambda x: x[1], reverse=True)
        top_agents = [agent_name for agent_name, _ in agent_totals[:top_n]]

        # Filter metrics to only include top assistants
        metrics = {
            'assistants': top_agents,
            'months': metrics['months'],
            'data': {assistant: metrics['data'][assistant] for assistant in top_agents if assistant in metrics['data']}
        }

    if not metrics['assistants'] or not metrics['months']:
        # Return an empty figure with a message
        fig = go.Figure()
        fig.add_annotation(
            text=no_data_msg,
            xref="paper", yref="paper",
            x=0.5, y=0.5, showarrow=False,
            font=dict(size=16)
        )
        fig.update_layout(
            title=None,
            xaxis_title=None,
            height=500
        )
        return fig

    # Create figure with secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])

    # Generate unique colors for many assistants using HSL color space
    def generate_color(index, total):
        """Generate distinct colors using HSL color space for better distribution"""
        hue = (index * 360 / total) % 360
        saturation = 70 + (index % 3) * 10  # Vary saturation slightly
        lightness = 45 + (index % 2) * 10   # Vary lightness slightly
        return f'hsl({hue}, {saturation}%, {lightness}%)'

    assistants = metrics['assistants']
    months = metrics['months']
    data = metrics['data']

    # Generate colors for all assistants
    agent_colors = {assistant: generate_color(idx, len(assistants)) for idx, assistant in enumerate(assistants)}

    # Add traces for each assistant
    for idx, agent_name in enumerate(assistants):
        color = agent_colors[agent_name]
        agent_data = data[agent_name]

        # Add line trace for resolved rate (left y-axis)
        resolved_rates = agent_data['resolved_rates']
        x_resolved = months
        y_resolved = resolved_rates

        if x_resolved and y_resolved:  # Only add trace if there's data
            fig.add_trace(
                go.Scatter(
                    x=x_resolved,
                    y=y_resolved,
                    name=agent_name,
                    mode='lines+markers',
                    line=dict(color=color, width=2),
                    marker=dict(size=8),
                    legendgroup=agent_name,
                    showlegend=(top_n is not None and top_n <= 10),  # Show legend for top N assistants
                    hovertemplate='<b>Assistant: %{fullData.name}</b><br>' +
                                 'Month: %{x}<br>' +
                                 'Resolved Rate: %{y:.2f}%<br>' +
                                 '<extra></extra>'
                ),
                secondary_y=False
            )

        # Add bar trace for total count (right y-axis)
        # Only show bars for months where assistant has data
        x_bars = []
        y_bars = []
        for month, count in zip(months, agent_data[total_field]):
            if count > 0:  # Only include months with data
                x_bars.append(month)
                y_bars.append(count)

        if x_bars and y_bars:  # Only add trace if there's data
            fig.add_trace(
                go.Bar(
                    x=x_bars,
                    y=y_bars,
                    name=agent_name,
                    marker=dict(color=color, opacity=0.6),
                    legendgroup=agent_name,
                    showlegend=False,  # Hide duplicate legend entry (already shown in Scatter)
                    hovertemplate=f'<b>Assistant: %{{fullData.name}}</b><br>' +
                                 f'Month: %{{x}}<br>' +
                                 f'{total_label}: %{{y}}<br>' +
                                 '<extra></extra>',
                    offsetgroup=agent_name  # Group bars by assistant for proper spacing
                ),
                secondary_y=True
            )

    # Update axes labels
    fig.update_xaxes(title_text=None)
    fig.update_yaxes(
        title_text="<b>Resolved Rate (%)</b>",
        range=[0, 100],
        secondary_y=False,
        showticklabels=True,
        tickmode='linear',
        dtick=10,
        showgrid=True
    )
    fig.update_yaxes(title_text=f"<b>{total_label}</b>", secondary_y=True)

    # Update layout
    show_legend = (top_n is not None and top_n <= 10)
    fig.update_layout(
        title=None,
        hovermode='closest',  # Show individual assistant info on hover
        barmode='group',
        height=600,
        showlegend=show_legend,
        margin=dict(l=50, r=150 if show_legend else 50, t=50, b=50)  # More right margin when legend is shown
    )

    return fig


def get_leaderboard_dataframe():
    """
    Load leaderboard from saved dataset and convert to pandas DataFrame for display.
    Returns formatted DataFrame sorted by total issues.
    """
    # Load from saved dataset
    saved_data = load_leaderboard_data_from_hf()

    if not saved_data or 'leaderboard' not in saved_data:
        print(f"No leaderboard data available")
        # Return empty DataFrame with correct columns if no data
        column_names = [col[0] for col in LEADERBOARD_COLUMNS]
        return pd.DataFrame(columns=column_names)

    cache_dict = saved_data['leaderboard']
    last_updated = saved_data.get('metadata', {}).get('last_updated', 'Unknown')
    print(f"Loaded leaderboard from saved dataset (last updated: {last_updated})")
    print(f"Cache dict size: {len(cache_dict)}")

    if not cache_dict:
        print("WARNING: cache_dict is empty!")
        # Return empty DataFrame with correct columns if no data
        column_names = [col[0] for col in LEADERBOARD_COLUMNS]
        return pd.DataFrame(columns=column_names)

    rows = []
    filtered_count = 0
    for identifier, data in cache_dict.items():
        total_issues = data.get('total_issues', 0)
        total_discussions = data.get('total_discussions', 0)
        total_wanted_issues = data.get('total_wanted_issues', 0)

        # Filter out assistants with no activity (all four metrics are zero)
        if total_issues == 0 and total_discussions == 0 and total_wanted_issues == 0:
            filtered_count += 1
            continue

        # Only include display-relevant fields (new column order)
        rows.append([
            data.get('name', 'Unknown'),
            data.get('website', 'N/A'),
            total_issues,  # Total Issues
            total_discussions,  # Total Discussions
            total_wanted_issues,  # Total Wanted Issues
            data.get('resolved_rate', 0.0),  # Issue Resolved Rate (%)
            data.get('discussion_resolved_rate', 0.0),  # Discussion Resolved Rate (%)
            data.get('wanted_issue_resolved_rate', 0.0),  # Wanted Issue Resolved Rate (%)
            data.get('resolved_wanted_issues', 0),  # Resolved Wanted Issues
            data.get('resolved_issues', 0),  # Resolved Issues
            data.get('resolved_discussions', 0),  # Resolved Discussions
        ])

    print(f"Filtered out {filtered_count} assistants with no activity")
    print(f"Leaderboard will show {len(rows)} assistants")

    # Create DataFrame
    column_names = [col[0] for col in LEADERBOARD_COLUMNS]
    df = pd.DataFrame(rows, columns=column_names)

    # Ensure numeric types
    numeric_cols = [
        "Total Issues", "Total Discussions", "Total Wanted Issues",
        "Issue Resolved Rate (%)", "Discussion Resolved Rate (%)",
        "Wanted Issue Resolved Rate (%)", "Resolved Wanted Issues", 
        "Resolved Issues", "Resolved Discussions"
    ]
    for col in numeric_cols:
        if col in df.columns:
            df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0)

    # Sort by Total Wanted Issues descending (primary metric for this leaderboard)
    if "Issue Resolved Rate (%)" in df.columns and not df.empty:
        df = df.sort_values(by="Issue Resolved Rate (%)", ascending=False).reset_index(drop=True)

    # Workaround for gradio_leaderboard bug: single-row tables don't render properly
    # Add a placeholder row when there's only 1 record
    if len(df) == 1:
        placeholder_row = pd.DataFrame([[
            "Submit yours to join!", "—", 0, 0, 0, 0.0, 0.0, 0.0, 0, 0, 0
        ]], columns=df.columns)
        df = pd.concat([df, placeholder_row], ignore_index=True)
        print("Added placeholder row for single-record workaround")

    print(f"Final DataFrame shape: {df.shape}")
    print("="*60 + "\n")

    return df


def get_wanted_issues_dataframe():
    """Load wanted issues and convert to pandas DataFrame."""
    saved_data = load_leaderboard_data_from_hf()

    if not saved_data or 'wanted_issues' not in saved_data:
        print(f"No wanted issues data available")
        return pd.DataFrame(columns=["Title", "URL", "Age (days)", "Labels"])

    wanted_issues = saved_data['wanted_issues']
    print(f"Loaded {len(wanted_issues)} wanted issues")

    if not wanted_issues:
        return pd.DataFrame(columns=["Title", "URL", "Age (days)", "Labels"])

    rows = []
    for issue in wanted_issues:
        # Calculate age
        created_at = issue.get('created_at')
        age_days = 0
        if created_at and created_at != 'N/A':
            try:
                created = datetime.fromisoformat(created_at.replace('Z', '+00:00'))
                age_days = (datetime.now(timezone.utc) - created).days
            except:
                pass

        # Create clickable link
        url = issue.get('url', '')
        repo = issue.get('repo', '')
        issue_number = issue.get('number', '')
        url_link = f'<a href="{url}" target="_blank">{repo}#{issue_number}</a>'

        rows.append([
            issue.get('title', ''),
            url_link,
            age_days,
            ', '.join(issue.get('labels', []))
        ])

    df = pd.DataFrame(rows, columns=["Title", "URL", "Age (days)", "Labels"])

    # Sort by age descending
    if "Age (days)" in df.columns and not df.empty:
        df = df.sort_values(by="Age (days)", ascending=False).reset_index(drop=True)

    return df


def submit_agent(identifier, agent_name, organization, website):
    """
    Submit a new assistant to the leaderboard.
    Validates input and saves submission.
    """
    # Validate required fields
    if not identifier or not identifier.strip():
        return "ERROR: GitHub identifier is required", gr.update()
    if not agent_name or not agent_name.strip():
        return "ERROR: Assistant name is required", gr.update()
    if not organization or not organization.strip():
        return "ERROR: Organization name is required", gr.update()
    if not website or not website.strip():
        return "ERROR: Website URL is required", gr.update()

    # Clean inputs
    identifier = identifier.strip()
    agent_name = agent_name.strip()
    organization = organization.strip()
    website = website.strip()

    # Validate GitHub identifier
    is_valid, message = validate_github_username(identifier)
    if not is_valid:
        return f"ERROR: {message}", gr.update()

    # Check for duplicates by loading assistants from HuggingFace
    assistants = load_agents_from_hf()
    if assistants:
        existing_names = {assistant['github_identifier'] for assistant in assistants}
        if identifier in existing_names:
            return f"WARNING: Assistant with identifier '{identifier}' already exists", gr.update()

    # Create submission
    submission = {
        'name': agent_name,
        'organization': organization,
        'github_identifier': identifier,
        'website': website,
        'status': 'active'
    }

    # Save to HuggingFace
    if not save_agent_to_hf(submission):
        return "ERROR: Failed to save submission", gr.update()

    # Return success message - data will be populated by backend updates
    return f"SUCCESS: Successfully submitted {agent_name}! Issue data will be automatically populated by the backend system via the maintainers.", gr.update()


# =============================================================================
# DATA RELOAD FUNCTION
# =============================================================================

def reload_leaderboard_data():
    """
    Reload leaderboard data from HuggingFace.
    This function is called by the scheduler on a daily basis.
    """
    print(f"\n{'='*80}")
    print(f"Reloading leaderboard data from HuggingFace...")
    print(f"{'='*80}\n")

    try:
        data = load_leaderboard_data_from_hf()
        if data:
            print(f"Successfully reloaded leaderboard data")
            print(f"   Last updated: {data.get('metadata', {}).get('last_updated', 'Unknown')}")
            print(f"   Assistants: {len(data.get('leaderboard', {}))}")
        else:
            print(f"No data available")
    except Exception as e:
        print(f"Error reloading leaderboard data: {str(e)}")

    print(f"{'='*80}\n")


# =============================================================================
# GRADIO APPLICATION
# =============================================================================

print(f"\nStarting SWE Assistant Issue Leaderboard")
print(f"   Data source: {LEADERBOARD_REPO}")
print(f"   Reload frequency: Daily at 12:00 AM UTC\n")

# Start APScheduler for daily data reload at 12:00 AM UTC
scheduler = BackgroundScheduler(timezone="UTC")
scheduler.add_job(
    reload_leaderboard_data,
    trigger=CronTrigger(hour=0, minute=0),  # 12:00 AM UTC daily
    id='daily_data_reload',
    name='Daily Data Reload',
    replace_existing=True
)
scheduler.start()
print(f"\n{'='*80}")
print(f"Scheduler initialized successfully")
print(f"Reload schedule: Daily at 12:00 AM UTC")
print(f"On startup: Loads cached data from HuggingFace on demand")
print(f"{'='*80}\n")

# Create Gradio interface
with gr.Blocks(title="SWE Assistant Issue & Discussion Leaderboard", theme=gr.themes.Soft()) as app:
    gr.Markdown("# SWE Assistant Issue & Discussion Leaderboard")
    gr.Markdown(f"Track and compare GitHub issue and discussion resolution statistics for SWE assistants")

    with gr.Tabs():

        # Leaderboard Tab
        with gr.Tab("Leaderboard"):
            gr.Markdown("*Statistics are based on assistant issue resolution activity tracked by the system*")
            leaderboard_table = Leaderboard(
                value=pd.DataFrame(columns=[col[0] for col in LEADERBOARD_COLUMNS]),  # Empty initially
                datatype=LEADERBOARD_COLUMNS,
                search_columns=["Assistant", "Website"],
                filter_columns=[
                    ColumnFilter(
                        "Issue Resolved Rate (%)",
                        min=0,
                        max=100,
                        default=[0, 100],
                        type="slider",
                        label="Issue Resolved Rate (%)"
                    ),
                    ColumnFilter(
                        "Discussion Resolved Rate (%)",
                        min=0,
                        max=100,
                        default=[0, 100],
                        type="slider",
                        label="Discussion Resolved Rate (%)"
                    ),
                    ColumnFilter(
                        "Wanted Issue Resolved Rate (%)",
                        min=0,
                        max=100,
                        default=[0, 100],
                        type="slider",
                        label="Wanted Issue Resolved Rate (%)"
                    )
                ]
            )

            # Load leaderboard data when app starts
            app.load(
                fn=get_leaderboard_dataframe,
                inputs=[],
                outputs=[leaderboard_table]
            )

            # Monthly Metrics Section
            gr.Markdown("---")  # Divider
            gr.Markdown("## Monthly Performance Metrics - Top 5 Assistants")

            with gr.Row():
                with gr.Column():
                    gr.Markdown("*Issue volume and resolved rate over time*")
                    monthly_metrics_plot = gr.Plot()

                with gr.Column():
                    gr.Markdown("*Discussion volume and resolved rate over time*")
                    discussion_metrics_plot = gr.Plot()

                with gr.Column():
                    gr.Markdown("*Wanted issue volume and resolved rate over time*")
                    wanted_metrics_plot = gr.Plot()

            # Load monthly metrics when app starts
            app.load(
                fn=lambda: create_monthly_metrics_plot(type="issue"),
                inputs=[],
                outputs=[monthly_metrics_plot]
            )

            # Load discussion monthly metrics when app starts
            app.load(
                fn=lambda: create_monthly_metrics_plot(type="discussion"),
                inputs=[],
                outputs=[discussion_metrics_plot]
            )

            # Load wanted issue monthly metrics when app starts
            app.load(
                fn=lambda: create_monthly_metrics_plot(type="wanted"),
                inputs=[],
                outputs=[wanted_metrics_plot]
            )


        # Issues Wanted Tab
        with gr.Tab("Issues Wanted"):
            gr.Markdown("### Long-Standing Patch-Wanted Issues")
            gr.Markdown(f"*Issues open for {LONGSTANDING_GAP_DAYS}+ days with patch-wanted labels from tracked organizations*")

            wanted_table = gr.Dataframe(
                value=pd.DataFrame(columns=["Title", "URL", "Age (days)", "Labels"]),
                datatype=["str", "html", "number", "str"],
                interactive=False,
                wrap=True
            )

            app.load(
                fn=get_wanted_issues_dataframe,
                inputs=[],
                outputs=[wanted_table]
            )


        # Submit Assistant Tab
        with gr.Tab("Submit Your Assistant"):

            gr.Markdown("Fill in the details below to add your assistant to the leaderboard.")

            with gr.Row():
                with gr.Column():
                    github_input = gr.Textbox(
                        label="GitHub Identifier*",
                        placeholder="Your assistant username (e.g., my-assistant[bot])"
                    )
                    name_input = gr.Textbox(
                        label="Assistant Name*",
                        placeholder="Your assistant's display name"
                    )

                with gr.Column():
                    organization_input = gr.Textbox(
                        label="Organization*",
                        placeholder="Your organization or team name"
                    )
                    website_input = gr.Textbox(
                        label="Website*",
                        placeholder="https://your-assistant-website.com"
                    )

            submit_button = gr.Button(
                "Submit Assistant",
                variant="primary"
            )
            submission_status = gr.Textbox(
                label="Submission Status",
                interactive=False
            )

            # Event handler
            submit_button.click(
                fn=submit_agent,
                inputs=[github_input, name_input, organization_input, website_input],
                outputs=[submission_status, leaderboard_table]
            )


# Launch application
if __name__ == "__main__":
    app.launch()