File size: 32,939 Bytes
a27816a
 
 
30b1610
4f32597
a27816a
de3b744
fc6c268
de3b744
 
 
 
 
 
 
 
30b1610
8190051
 
 
 
 
 
 
 
1046fcc
 
 
 
 
 
de3b744
 
6517c54
1a7090c
de3b744
 
1046fcc
 
6f98bd6
 
6517c54
 
1a7090c
 
 
 
 
 
 
 
 
 
de3b744
 
1046fcc
6f98bd6
 
de3b744
 
6517c54
6f98bd6
 
6517c54
6f98bd6
 
1a7090c
 
 
 
 
0985e79
 
 
 
1a7090c
6f98bd6
0985e79
 
 
 
 
6f98bd6
 
1046fcc
6f98bd6
1a7090c
6f98bd6
 
307f223
 
 
1a7090c
307f223
 
 
 
 
 
 
1a7090c
de3b744
 
 
 
 
 
 
1046fcc
 
 
 
 
 
de3b744
1a7090c
de3b744
1a7090c
de3b744
 
1046fcc
de3b744
1046fcc
6f98bd6
 
1a7090c
6f98bd6
 
1046fcc
 
 
 
1a7090c
de3b744
1046fcc
 
 
 
 
 
 
 
 
 
254fe03
1046fcc
 
 
 
 
 
 
 
 
 
 
1a7090c
de3b744
 
0985e79
 
de3b744
1a7090c
de3b744
1a7090c
1046fcc
6f98bd6
 
1a7090c
6f98bd6
307f223
 
 
 
1a7090c
307f223
 
 
 
 
 
1a7090c
1046fcc
de3b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6517c54
 
de3b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7090c
 
0985e79
 
 
 
 
 
6f98bd6
 
 
1a7090c
de3b744
0985e79
 
 
307f223
de3b744
1046fcc
 
 
 
1a7090c
1046fcc
307f223
 
 
 
 
1046fcc
 
1a7090c
1046fcc
307f223
 
 
 
 
1046fcc
1a7090c
 
de3b744
 
 
 
 
 
 
1046fcc
 
de3b744
 
 
 
 
 
 
 
 
 
1a7090c
 
de3b744
 
 
 
 
 
 
 
 
 
 
 
 
0985e79
 
 
 
 
 
de3b744
 
1a7090c
 
0985e79
 
1a7090c
0985e79
1046fcc
0985e79
 
 
 
 
 
 
 
 
 
1046fcc
 
 
0985e79
 
1046fcc
 
 
 
 
 
 
0985e79
254fe03
6f98bd6
1046fcc
254fe03
1a7090c
de3b744
6f98bd6
1046fcc
1a7090c
 
0985e79
1a7090c
 
0985e79
 
 
 
 
 
 
1046fcc
de3b744
 
 
1046fcc
de3b744
 
 
 
 
 
1046fcc
 
 
 
 
de3b744
1046fcc
 
 
 
de3b744
 
 
1046fcc
0985e79
 
de3b744
1046fcc
6f98bd6
1046fcc
 
0985e79
 
 
 
1a7090c
 
0985e79
1a7090c
 
0985e79
1046fcc
 
 
 
 
 
 
 
de3b744
1046fcc
 
de3b744
1046fcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0985e79
1046fcc
 
 
 
 
de3b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0985e79
1a7090c
 
0985e79
 
 
 
 
 
 
1a7090c
0985e79
 
1a7090c
0985e79
 
1a7090c
0985e79
 
 
1a7090c
 
de3b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7090c
6517c54
 
1a7090c
 
 
 
 
 
 
6517c54
 
 
0985e79
 
 
 
 
 
 
 
 
 
 
 
 
1a7090c
 
 
0985e79
1a7090c
 
6517c54
0985e79
 
 
 
 
 
 
de3b744
 
 
1a7090c
de3b744
 
 
1a7090c
de3b744
 
 
1a7090c
de3b744
 
 
1a7090c
de3b744
 
 
 
1a7090c
6517c54
 
0985e79
1a7090c
de3b744
 
 
 
1a7090c
de3b744
 
 
1a7090c
 
 
de3b744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7090c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de3b744
8190051
0985e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8190051
 
edf1ecb
6df6e43
8190051
1a7090c
 
edf1ecb
8c0f360
 
6df6e43
8c0f360
1a7090c
a3558a8
6df6e43
d4652ff
 
 
307f223
 
 
 
 
 
d4652ff
6df6e43
a3558a8
de3b744
a3558a8
1a7090c
 
1bac4cd
6df6e43
a3558a8
edf1ecb
1a7090c
700777f
1a7090c
6df6e43
700777f
307f223
 
0985e79
307f223
 
700777f
a3558a8
 
1a7090c
890be77
0985e79
 
a3558a8
6df6e43
1a7090c
 
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
import gradio as gr
import json
import importlib
import os
import sys
from pathlib import Path
import concurrent.futures
import multiprocessing
import time
import threading
import queue
import uuid
import numpy as np
from datetime import datetime
from tqdm.auto import tqdm
from src.containerized_eval import eval_string_script

# Add current directory and src directory to module search path
current_dir = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(current_dir, "src")
if current_dir not in sys.path:
    sys.path.append(current_dir)
if src_dir not in sys.path:
    sys.path.append(src_dir)

# Create message queue
task_queue = queue.Queue()
# Dictionary to store task status
task_status = {}
# List to store task history, max 200 tasks
task_history = []
# Lock for shared resources
lock = threading.Lock()
# Number of worker threads - set to 1 to process one task at a time
worker_threads = 1
# Flag for running background threads
running = True
# Mapping from task type to processing time
task_type_times = {}
# Currently processing tasks counter
processing_count = 0
# Available CPU cores for task processing
available_cores = multiprocessing.cpu_count()
# Task ID counter for debugging
task_counter = 0

# Enable logging
DEBUG_MODE = True

def debug_log(message):
    """Log debug messages if debug mode is enabled"""
    if DEBUG_MODE:
        print(f"[DEBUG] {datetime.now().strftime('%H:%M:%S')} - {message}")

def queue_processor():
    """Process tasks in the queue"""
    global processing_count
    
    while running:
        try:
            # Only process if we're not already processing a task
            with lock:
                if processing_count >= worker_threads:
                    # Already processing a task, wait and try again
                    time.sleep(0.5)
                    continue
            
            # Check queue size before attempting to get a task
            queue_size = task_queue.qsize()
            if queue_size > 0:
                debug_log(f"Queue processor found {queue_size} tasks waiting")
            else:
                # No tasks waiting, sleep briefly to avoid CPU spinning
                time.sleep(0.1)
                continue
                
            # Get a task from the queue with small timeout to prevent blocking
            try:
                task_id, input_data, request_time = task_queue.get(timeout=0.1)
                debug_log(f"Processing task {task_id}")
            except queue.Empty:
                continue
            
            # Increment processing count to track active tasks
            with lock:
                processing_count += 1
                debug_log(f"Incremented processing count to {processing_count}")
                
                # Update task status
                if task_id in task_status:
                    task_status[task_id]['status'] = 'processing'
                    task_status[task_id]['start_time'] = time.time()
                    debug_log(f"Updated existing task {task_id} to processing state")
                else:
                    # Create task status entry if it doesn't exist
                    task_status[task_id] = {
                        'status': 'processing',
                        'queued_time': request_time,
                        'start_time': time.time()
                    }
                    debug_log(f"Created new task status entry for {task_id}")
            
            if isinstance(input_data, list) and len(input_data) > 0:
                sample_task = input_data[0]
                language = sample_task.get('language', 'unknown') if isinstance(sample_task, dict) else 'unknown'
                task_size = len(input_data)
                task_complexity = _estimate_task_complexity(input_data)
                
                with lock:
                    task_status[task_id]['estimated_factors'] = {
                        'language': language,
                        'size': task_size,
                        'complexity': task_complexity
                    }
            
            debug_log(f"Starting evaluation for task {task_id}")
            result = evaluate(input_data)
            debug_log(f"Finished evaluation for task {task_id}")
            
            end_time = time.time()
            process_time = end_time - task_status[task_id]['start_time']
            
            with lock:
                # Decrease processing count now that we're done
                processing_count -= 1
                debug_log(f"Decremented processing count to {processing_count}")
                
                # Update task status
                task_status[task_id]['status'] = 'completed'
                task_status[task_id]['result'] = result
                task_status[task_id]['end_time'] = end_time
                task_status[task_id]['process_time'] = process_time
                debug_log(f"Updated task {task_id} to completed state")
                
                if 'estimated_factors' in task_status[task_id]:
                    factors = task_status[task_id]['estimated_factors']
                    key = f"{factors['language']}_{factors['complexity']}"
                    
                    if key not in task_type_times:
                        task_type_times[key] = []
                    
                    task_type_times[key].append(process_time / factors['size'])
                    if len(task_type_times[key]) > 10:
                        task_type_times[key] = task_type_times[key][-10:]
                
                task_history.append({
                    'task_id': task_id,
                    'request_time': request_time,
                    'process_time': process_time,
                    'status': 'completed',
                    'factors': task_status[task_id].get('estimated_factors', {})
                })
                while len(task_history) > 200:
                    task_history.pop(0)
                    
            task_queue.task_done()
            debug_log(f"Task {task_id} completed and marked as done")
            
        except queue.Empty:
            # Use a small timeout to avoid CPU spinning
            time.sleep(0.1)
        except Exception as e:
            debug_log(f"Error in queue processor: {str(e)}")
            if 'task_id' in locals():
                debug_log(f"Error occurred while processing task {task_id}")
                with lock:
                    # Decrease processing count on error
                    processing_count -= 1
                    debug_log(f"Decremented processing count to {processing_count} due to error")
                    
                    if task_id in task_status:
                        task_status[task_id]['status'] = 'error'
                        task_status[task_id]['error'] = str(e)
                        task_status[task_id]['end_time'] = time.time()
                        debug_log(f"Updated task {task_id} to error state")
                    else:
                        task_status[task_id] = {
                            'status': 'error',
                            'error': str(e),
                            'end_time': time.time()
                        }
                        debug_log(f"Created new error entry for task {task_id}")
            task_queue.task_done()

def _estimate_task_complexity(tasks):
    """Estimate task complexity
    
    Returns: 'simple', 'medium', or 'complex'
    """
    total_code_length = 0
    count = 0
    
    for task in tasks:
        if isinstance(task, dict):
            prompt = task.get('prompt', '')
            tests = task.get('tests', '')
            completions = task.get('processed_completions', [])
            
            code_length = len(prompt) + len(tests)
            if completions:
                code_length += sum(len(comp) for comp in completions)
            
            total_code_length += code_length
            count += 1
    
    if count == 0:
        return 'medium'
    
    avg_length = total_code_length / count
    
    if avg_length < 1000:
        return 'simple'
    elif avg_length < 5000:
        return 'medium'
    else:
        return 'complex'

def evaluate(input_data):
    """Main function for code evaluation"""
    try:
        if not isinstance(input_data, list):
            return {"status": "Exception", "error": "Input must be a list"}
            
        results = []
        
        # Use all available cores for this single task but with a reasonable cap
        max_workers = max(1, min(available_cores // 2, 8))
        
        with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
            future_to_item = {executor.submit(evaluate_single_case, item): item for item in input_data}
            for future in concurrent.futures.as_completed(future_to_item):
                item = future_to_item[future]
                try:
                    result = future.result()
                    item.update(result)
                    results.append(item)
                except Exception as e:
                    item.update({"status": "Exception", "error": str(e)})
                    results.append(item)
        return results
            
    except Exception as e:
        return {"status": "Exception", "error": str(e)}

def evaluate_single_case(input_data):
    """Evaluate a single code case"""
    try:
        if not isinstance(input_data, dict):
            return {"status": "Exception", "error": "Input item must be a dictionary"}
            
        language = input_data.get('language')
        completions = input_data.get('processed_completions', [])

        if not completions:
            return {"status": "Exception", "error": "No code provided"}

        # Use a retry mechanism for all languages for better reliability
        max_retries = 2  # One retry for all languages
        
        results = []
        for comp in completions:
            code = input_data.get('prompt') + comp + '\n' + input_data.get('tests')
            
            # Try up to max_retries + 1 times for all test cases
            for attempt in range(max_retries + 1):
                result = evaluate_code(code, language)
                
                # If success or last attempt, return/record the result
                if result["status"] == "OK" or attempt == max_retries:
                    if result["status"] == "OK":
                        return result
                    results.append(result)
                    break
                    
                # For retries, briefly wait to allow resources to stabilize
                time.sleep(0.3)
            
        return results[0]
                
    except Exception as e:
        return {"status": "Exception", "error": str(e)}

def evaluate_code(code, language):
    """Evaluate code in a specific language"""
    try:
        result = eval_string_script(language, code)
        return result

    except Exception as e:
        return {"status": "Exception", "error": str(e)}

def synchronous_evaluate(input_data):
    """Synchronously evaluate code, compatible with original interface"""
    debug_log(f"Received synchronous evaluation request")
    
    # Add metadata to identify sync requests
    if isinstance(input_data, list) and len(input_data) > 0 and isinstance(input_data[0], dict):
        if 'metadata' not in input_data[0]:
            input_data[0]['metadata'] = {}
        input_data[0]['metadata']['source'] = 'sync_api'
    
    # Create a task and queue it
    task_info = enqueue_task(input_data)
    task_id = task_info['task_id']
    debug_log(f"Created task {task_id} for synchronous evaluation")
    
    # Ensure the task appears in the queue UI, add artificial delay if needed
    time.sleep(0.1)  # Small delay to make sure the task is visible in queue
    
    # Wait for task to complete
    while True:
        with lock:
            if task_id in task_status:
                status = task_status[task_id]['status']
                if status == 'completed':
                    debug_log(f"Task {task_id} completed, returning result")
                    result = task_status[task_id]['result']
                    # Keep the result in status for a short time to ensure it shows in history
                    if 'end_time' not in task_status[task_id]:
                        task_status[task_id]['end_time'] = time.time()
                    elif time.time() - task_status[task_id]['end_time'] > 5:
                        task_status.pop(task_id, None)
                    return result
                elif status == 'error':
                    debug_log(f"Task {task_id} failed with error")
                    error = task_status[task_id].get('error', 'Unknown error')
                    # Keep the error in status for a short time to ensure it shows in history
                    if 'end_time' not in task_status[task_id]:
                        task_status[task_id]['end_time'] = time.time()
                    elif time.time() - task_status[task_id]['end_time'] > 5:
                        task_status.pop(task_id, None)
                    return {"status": "Exception", "error": error}
                else:
                    debug_log(f"Task {task_id} still in status: {status}")
        
        time.sleep(0.1)

def _get_estimated_time_for_task(language, complexity):
    """Get estimated processing time for a specific task type"""
    key = f"{language}_{complexity}"
    
    if key in task_type_times and len(task_type_times[key]) > 0:
        return np.median(task_type_times[key])
    
    if complexity == 'simple':
        return 1.0
    elif complexity == 'medium':
        return 3.0
    else:  # complex
        return 8.0

def enqueue_task(input_data):
    """Add task to queue"""
    global task_counter
    
    if isinstance(input_data, list) and len(input_data) > 0:
        sample_task = input_data[0]
        language = sample_task.get('language', 'unknown') if isinstance(sample_task, dict) else 'unknown'
        task_size = len(input_data)
        task_complexity = _estimate_task_complexity(input_data)
    else:
        language = 'unknown'
        task_size = 1
        task_complexity = 'medium'
    
    estimated_time_per_task = _get_estimated_time_for_task(language, task_complexity)
    estimated_total_time = estimated_time_per_task * task_size
    
    # Generate task ID in a thread-safe way
    with lock:
        task_counter += 1
        local_counter = task_counter
    
    task_id = f"task_{local_counter}_{str(uuid.uuid4())[:8]}"
    request_time = time.time()
    
    debug_log(f"Creating new task: {task_id}")
    
    # Track if this is a synchronous or asynchronous submission
    is_async = 'async_submission' in str(threading.current_thread().name).lower() or 'async' in input_data[0].get('metadata', {}).get('source', '') if isinstance(input_data, list) and input_data and isinstance(input_data[0], dict) and 'metadata' in input_data[0] else False
    
    # Get current queue status before adding to task_status
    with lock:
        # Count actual queue status - both in queue AND waiting in task_status
        current_queue_size = task_queue.qsize()
        actual_waiting = sum(1 for t in task_status.values() if t['status'] == 'queued')
        total_waiting = actual_waiting  # Use the actual count from task_status
        
        debug_log(f"Current queue metrics: queue_size={current_queue_size}, task_status_waiting={actual_waiting}, total={total_waiting}")
        
        queue_position = total_waiting + 1
        
        # Add to task_status with 'queued' status first
        task_status[task_id] = {
            'status': 'queued',
            'queued_time': request_time,
            'queue_position': queue_position,
            'is_async': is_async,
            'estimated_factors': {
                'language': language,
                'size': task_size,
                'complexity': task_complexity
            },
            'estimated_time': estimated_total_time
        }
        debug_log(f"Added task {task_id} to task_status with queue position {queue_position}")
    
    # Get queue info for wait time estimation
    queue_info = get_queue_status()
    est_wait = queue_info['estimated_wait']
    debug_log(f"Estimated wait time for task {task_id}: {est_wait} seconds")
    
    # Add to the task queue - this must be done AFTER adding to task_status
    task_queue.put((task_id, input_data, request_time))
    debug_log(f"Added task {task_id} to task_queue")
    
    # Count queued tasks in task_status after adding
    with lock:
        queued_count = sum(1 for t in task_status.values() if t['status'] == 'queued')
        processing_tasks = sum(1 for t in task_status.values() if t['status'] == 'processing')
        debug_log(f"Queue status after adding: {task_queue.qsize()} in queue, {queued_count} with 'queued' status, {processing_tasks} processing")
        
        # Display all task IDs currently in queue
        task_ids = [(k, v['status']) for k, v in task_status.items() if v['status'] in ('queued', 'processing')]
        if task_ids:
            debug_log(f"Current tasks: {task_ids}")
    
    return {
        'task_id': task_id,
        'status': 'queued',
        'queue_position': task_status[task_id]['queue_position'],
        'estimated_wait': est_wait,
        'estimated_processing': estimated_total_time
    }

def check_status(task_id):
    """Check task status"""
    with lock:
        if task_id not in task_status:
            return {'status': 'not_found'}
        
        status_info = task_status[task_id].copy()
        
        if status_info['status'] in ['completed', 'error'] and time.time() - status_info.get('end_time', 0) > 3600:
            task_status.pop(task_id, None)
            
        return status_info

def get_queue_status():
    """Get queue status"""
    with lock:
        queued_tasks = [v for k, v in task_status.items() if v['status'] == 'queued']
        processing_tasks = [v for k, v in task_status.items() if v['status'] == 'processing']
        
        queue_size = task_queue.qsize()
        active_tasks = processing_count
        waiting_tasks = len(queued_tasks)
        
        debug_log(f"Queue status check: size={queue_size}, active={active_tasks}, waiting={waiting_tasks}")
        if waiting_tasks != queue_size and abs(waiting_tasks - queue_size) > 1:
            debug_log(f"WARNING: Queue size mismatch - task_queue has {queue_size} items but task_status has {waiting_tasks} queued items")
            
        debug_log(f"Queue status details: {len(queued_tasks)} queued tasks found in task_status")
        if queued_tasks:
            task_ids = [k for k, v in task_status.items() if v['status'] == 'queued']
            debug_log(f"Queued task IDs: {task_ids}")
        
        # Calculate remaining processing time for active tasks
        remaining_processing_time = 0
        for task in processing_tasks:
            if 'start_time' in task and 'estimated_time' in task:
                elapsed = time.time() - task['start_time']
                remaining = max(0, task['estimated_time'] - elapsed)
                remaining_processing_time += remaining
            else:
                remaining_processing_time += 2
        
        if active_tasks > 0:
            remaining_processing_time = remaining_processing_time / min(active_tasks, worker_threads)
        
        queued_processing_time = 0
        for task in queued_tasks:
            if 'estimated_time' in task:
                queued_processing_time += task['estimated_time']
            else:
                queued_processing_time += 5
        
        if worker_threads > 0 and queued_processing_time > 0:
            queued_processing_time = queued_processing_time / worker_threads
        
        estimated_wait = remaining_processing_time + queued_processing_time
        
        if task_history:
            prediction_ratios = []
            for task in task_history:
                if 'factors' in task and 'estimated_time' in task:
                    prediction_ratios.append(task['process_time'] / task['estimated_time'])
            
            if prediction_ratios:
                correction_factor = np.median(prediction_ratios)
                correction_factor = max(0.5, min(2.0, correction_factor))
                estimated_wait *= correction_factor
        
        estimated_wait = max(0.1, estimated_wait)
        if waiting_tasks == 0 and active_tasks == 0:
            estimated_wait = 0
            
        recent_tasks = task_history[-5:] if task_history else []
            
        return {
            'queue_size': queue_size,
            'active_tasks': active_tasks, 
            'waiting_tasks': waiting_tasks,
            'worker_threads': worker_threads,
            'estimated_wait': estimated_wait,
            'recent_tasks': recent_tasks
        }

def format_time(seconds):
    """Format time into readable format"""
    if seconds < 60:
        return f"{seconds:.1f} seconds"
    elif seconds < 3600:
        minutes = int(seconds / 60)
        seconds = seconds % 60
        return f"{minutes}m {seconds:.1f}s"
    else:
        hours = int(seconds / 3600)
        minutes = int((seconds % 3600) / 60)
        return f"{hours}h {minutes}m"

def ui_get_queue_info():
    """Get queue info for UI"""
    queue_info = get_queue_status()
    
    # List queued tasks with details - make sure to use task_id as key
    queued_tasks_html = ""
    with lock:
        queued_tasks = []
        for task_id, task in task_status.items():
            if task['status'] == 'queued':
                task_with_id = task.copy()
                task_with_id['task_id'] = task_id
                queued_tasks.append(task_with_id)
        
        if queued_tasks:
            # Sort by queue position
            queued_tasks.sort(key=lambda x: x.get('queue_position', 999999))
            queued_tasks_html = "<div class='queued-tasks'><h4>Tasks in Queue:</h4><ul>"
            for idx, task in enumerate(queued_tasks):
                task_id = task['task_id']
                queued_time = datetime.fromtimestamp(task.get('queued_time', 0)).strftime('%H:%M:%S')
                source = "async" if task.get('is_async', False) else "sync"
                time_in_queue = time.time() - task.get('queued_time', time.time())
                queued_tasks_html += f"<li>Task {task_id[:8]}... - Queued at {queued_time} ({time_in_queue:.1f}s ago) - Position {idx+1} ({source})</li>"
            queued_tasks_html += "</ul></div>"
    
    tasks_html = ""
    for task in reversed(queue_info['recent_tasks']):
        tasks_html += f"""
        <tr>
            <td>{task['task_id'][:8]}...</td>
            <td>{datetime.fromtimestamp(task['request_time']).strftime('%H:%M:%S')}</td>
            <td>{format_time(task['process_time'])}</td>
        </tr>
        """
    
    if not tasks_html:
        tasks_html = """
        <tr>
            <td colspan="3" style="text-align: center; padding: 20px;">No historical tasks</td>
        </tr>
        """
    
    # Add more detailed queue information
    queue_details = ""
    if queue_info['waiting_tasks'] > 0:
        queue_details = f"""
        <div class="alert alert-info">
            <p><strong>Currently {queue_info['waiting_tasks']} tasks in queue</strong></p>
            <p>Estimated wait time: {format_time(queue_info['estimated_wait'])}</p>
            {queued_tasks_html}
        </div>
        """
    
    processing_details = ""
    if queue_info['active_tasks'] > 0:
        # Display which tasks are being processed
        processing_tasks_html = ""
        with lock:
            processing_task_ids = [k for k, v in task_status.items() if v['status'] == 'processing']
            if processing_task_ids:
                processing_tasks_html = "<ul>"
                for task_id in processing_task_ids:
                    task = task_status[task_id]
                    start_time = datetime.fromtimestamp(task.get('start_time', 0)).strftime('%H:%M:%S')
                    time_processing = time.time() - task.get('start_time', time.time())
                    processing_tasks_html += f"<li>Task {task_id[:8]}... - Started at {start_time} ({time_processing:.1f}s ago)</li>"
                processing_tasks_html += "</ul>"
        
        processing_details = f"""
        <div class="alert alert-warning">
            <p><strong>Currently {queue_info['active_tasks']} tasks being processed</strong></p>
            {processing_tasks_html}
        </div>
        """
    
    # Add debug info
    debug_details = f"""
    <div class="debug-info">
        <p><small>Queue: {queue_info['queue_size']} in queue, {queue_info['waiting_tasks']} waiting, {queue_info['active_tasks']} processing</small></p>
    </div>
    """
    
    return f"""
    <div class="dashboard">
        <div class="queue-info-card main-card">
            <h3 class="card-title">Queue Status Monitor</h3>
            <div class="queue-stats">
                <div class="stat-item">
                    <div class="stat-value">{queue_info['waiting_tasks']}</div>
                    <div class="stat-label">Waiting</div>
                </div>
                <div class="stat-item">
                    <div class="stat-value">{queue_info['active_tasks']}</div>
                    <div class="stat-label">Processing</div>
                </div>
                <div class="stat-item">
                    <div class="stat-value">{queue_info['worker_threads']}</div>
                    <div class="stat-label">Worker Threads</div>
                </div>
            </div>
            
            <div class="wait-time">
                <p><b>Estimated Wait Time:</b> {format_time(queue_info['estimated_wait'])}</p>
                {queue_details}
                {processing_details}
                {debug_details}
                <p class="last-update"><small>Last update: {datetime.now().strftime('%H:%M:%S')}</small></p>
            </div>
        </div>
            
        <div class="queue-info-card history-card">
            <h3 class="card-title">Recently Processed Tasks</h3>
            <table class="recent-tasks">
                <thead>
                    <tr>
                        <th>Task ID</th>
                        <th>Request Time</th>
                        <th>Processing Time</th>
                    </tr>
                </thead>
                <tbody>
                    {tasks_html}
                </tbody>
            </table>
        </div>
    </div>
    """

def launch_workers():
    """Launch worker threads"""
    global running
    running = True
    
    for _ in range(worker_threads):
        worker = threading.Thread(target=queue_processor)
        worker.daemon = True
        worker.start()

# Custom CSS
custom_css = """
.container {
    max-width: 1200px;
    margin: 0 auto;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.dashboard {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.card-title {
    color: #333;
    border-bottom: 2px solid #ddd;
    padding-bottom: 10px;
    margin-top: 0;
}

.status-card, .queue-info-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    margin: 10px 0;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
}

.main-card {
    border-top: 5px solid #4285f4;
}

.history-card {
    border-top: 5px solid #34a853;
}

.status-card.success {
    background: #e7f5e7;
    border-left: 5px solid #28a745;
}

.status-card.error {
    background: #f8d7da;
    border-left: 5px solid #dc3545;
}

.error-message {
    color: #dc3545;
    font-weight: bold;
    padding: 10px;
    background: #f8d7da;
    border-radius: 5px;
}

.notice {
    color: #0c5460;
    background-color: #d1ecf1;
    padding: 10px;
    border-radius: 5px;
}

.queue-stats {
    display: flex;
    justify-content: space-around;
    margin: 20px 0;
}

.stat-item {
    text-align: center;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 10px;
    min-width: 120px;
    transition: transform 0.3s ease;
}

.stat-item:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.stat-value {
    font-size: 32px;
    font-weight: bold;
    color: #4285f4;
    margin-bottom: 5px;
}

.stat-label {
    color: #5f6368;
    font-size: 16px;
}

.wait-time {
    text-align: center;
    margin: 20px 0;
    padding: 15px;
    background: #f1f3f4;
    border-radius: 8px;
    font-size: 18px;
}

.last-update {
    color: #80868b;
    margin-top: 10px;
    margin-bottom: 0;
}

.recent-tasks {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
    background: white;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.recent-tasks th, .recent-tasks td {
    border: 1px solid #e0e0e0;
    padding: 12px 15px;
    text-align: center;
}

.recent-tasks th {
    background-color: #f1f3f4;
    color: #202124;
    font-weight: 500;
}

.recent-tasks tbody tr:hover {
    background-color: #f8f9fa;
}

.tabs {
    margin-top: 20px;
}

button.primary {
    background-color: #4285f4;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
    font-weight: 500;
    transition: background-color 0.3s;
}

button.primary:hover {
    background-color: #3367d6;
}

.alert {
    padding: 12px;
    margin: 10px 0;
    border-radius: 6px;
}

.alert-info {
    background-color: #d1ecf1;
    color: #0c5460;
    border: 1px solid #bee5eb;
}

.alert-warning {
    background-color: #fff3cd;
    color: #856404;
    border: 1px solid #ffeeba;
}

.queued-tasks {
    text-align: left;
    margin: 10px 0;
    padding: 8px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 4px;
}

.queued-tasks ul {
    margin: 5px 0;
    padding-left: 20px;
}

.queued-tasks li {
    margin-bottom: 3px;
}
"""

def async_enqueue(input_data):
    """Async version of enqueue_task - specifically for async API calls"""
    # Add metadata to identify async requests
    if isinstance(input_data, list) and len(input_data) > 0 and isinstance(input_data[0], dict):
        if 'metadata' not in input_data[0]:
            input_data[0]['metadata'] = {}
        input_data[0]['metadata']['source'] = 'async_api'
    
    # Just call enqueue_task but set thread name to identify as async
    current_thread = threading.current_thread()
    original_name = current_thread.name
    current_thread.name = f"async_submission_{original_name}"
    
    result = enqueue_task(input_data)
    
    # Reset thread name
    current_thread.name = original_name
    
    return result

# Initialize and launch worker threads
launch_workers()

# Create Gradio interface
with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("# Code Evaluation Service")
    gr.Markdown("Code evaluation service supporting multiple programming languages, using queue mechanism to process requests")
    
    with gr.Row():
        with gr.Column(scale=3):
            # Queue status info card
            queue_info_html = gr.HTML()
            refresh_queue_btn = gr.Button("Refresh Queue Status", variant="primary")
    
    # Hidden API interface components
    with gr.Row(visible=False):
        api_input = gr.JSON()
        api_output = gr.JSON()
        
        async_api_input = gr.JSON()
        async_api_output = gr.JSON()
        
        status_check_input = gr.Textbox()
        status_check_output = gr.JSON()
    
    # Define update function
    def update_queue_info():
        return ui_get_queue_info()
    
    # Update queue info more frequently
    demo.load(update_queue_info, None, queue_info_html, every=0.5)
    
    # Refresh button event
    refresh_queue_btn.click(update_queue_info, None, queue_info_html)
    
    # Force sync when handling API requests to prevent gradio's queue from interfering
    # Use the correct queue configuration method for current Gradio version
    
    # Add evaluation endpoint compatible with original interface
    evaluate_endpoint = demo.load(fn=synchronous_evaluate, inputs=api_input, outputs=api_output, api_name="evaluate", concurrency_limit=1)
    
    # Add async evaluation endpoint
    enqueue_endpoint = demo.load(fn=async_enqueue, inputs=async_api_input, outputs=async_api_output, api_name="enqueue", concurrency_limit=1)
    
    # Add status check endpoint
    status_endpoint = demo.load(fn=check_status, inputs=status_check_input, outputs=status_check_output, api_name="status", concurrency_limit=1)

if __name__ == "__main__":
    debug_log("Starting application")
    try:
        # Set max_threads for overall concurrency
        demo.launch(max_threads=100)
    finally:
        # Stop worker threads
        running = False
        debug_log("Shutting down application")