File size: 28,114 Bytes
d6d843f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
ULTIMATE Gradio Dashboard for Crypto Data Sources
Advanced monitoring with force testing, auto-healing, and real-time status
"""

import gradio as gr
import httpx
import asyncio
import json
import time
from datetime import datetime
from typing import Dict, List, Tuple, Optional
import pandas as pd
from pathlib import Path
import sys
import os
import threading
from collections import defaultdict

# Add project root to path
sys.path.insert(0, os.path.dirname(__file__))


class UltimateCryptoMonitor:
    """Ultimate monitoring system with force testing and auto-healing"""

    def __init__(self):
        self.api_resources = self.load_all_resources()
        self.health_status = {}
        self.auto_heal_enabled = False
        self.monitoring_active = False
        self.fastapi_url = "http://localhost:7860"
        self.hf_engine_url = "http://localhost:8000"
        self.test_results = []
        self.force_test_results = {}

    def load_all_resources(self) -> Dict:
        """Load ALL API resources from all JSON files"""
        resources = {}

        json_files = [
            "api-resources/crypto_resources_unified_2025-11-11.json",
            "api-resources/ultimate_crypto_pipeline_2025_NZasinich.json",
            "all_apis_merged_2025.json",
            "providers_config_extended.json",
            "providers_config_ultimate.json",
        ]

        for json_file in json_files:
            try:
                path = Path(json_file)
                if path.exists():
                    with open(path) as f:
                        data = json.load(f)
                        resources[path.stem] = data
                        print(f"βœ… Loaded: {json_file}")
            except Exception as e:
                print(f"❌ Error loading {json_file}: {e}")

        return resources

    async def force_test_endpoint(
        self,
        url: str,
        method: str = "GET",
        headers: Optional[Dict] = None,
        retry_count: int = 3,
        timeout: int = 10
    ) -> Dict:
        """Force test an endpoint with retries and detailed results"""
        results = {
            "url": url,
            "method": method,
            "attempts": [],
            "success": False,
            "total_time": 0,
            "final_status": "Failed"
        }

        for attempt in range(retry_count):
            attempt_result = {
                "attempt": attempt + 1,
                "timestamp": datetime.now().isoformat(),
                "success": False
            }

            start_time = time.time()

            try:
                async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
                    if method == "GET":
                        response = await client.get(url, headers=headers or {})
                    else:
                        response = await client.post(url, headers=headers or {})

                    elapsed = (time.time() - start_time) * 1000

                    attempt_result.update({
                        "success": response.status_code < 400,
                        "status_code": response.status_code,
                        "latency_ms": elapsed,
                        "response_size": len(response.content),
                        "headers": dict(response.headers)
                    })

                    if response.status_code < 400:
                        results["success"] = True
                        results["final_status"] = "Success"
                        results["attempts"].append(attempt_result)
                        break

            except httpx.TimeoutException:
                attempt_result["error"] = "Timeout"
                attempt_result["latency_ms"] = timeout * 1000
            except httpx.ConnectError:
                attempt_result["error"] = "Connection refused"
            except Exception as e:
                attempt_result["error"] = str(e)[:200]

            results["attempts"].append(attempt_result)
            results["total_time"] = (time.time() - start_time) * 1000

            if attempt < retry_count - 1:
                await asyncio.sleep(1)  # Wait before retry

        return results

    def get_comprehensive_overview(self) -> str:
        """Get ultra-comprehensive system overview"""
        overview = f"""
# πŸš€ ULTIMATE Crypto Data Sources Monitor

**Current Time:** {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
**Monitoring Status:** {'🟒 Active' if self.monitoring_active else 'πŸ”΄ Inactive'}
**Auto-Heal:** {'βœ… Enabled' if self.auto_heal_enabled else '❌ Disabled'}

---

## πŸ–₯️ Core Systems Status

"""

        # Check FastAPI
        try:
            response = httpx.get(f"{self.fastapi_url}/health", timeout=5)
            overview += f"### βœ… FastAPI Backend - ONLINE\n"
            overview += f"- URL: `{self.fastapi_url}`\n"
            overview += f"- Status: {response.status_code}\n"
            overview += f"- Response Time: {response.elapsed.total_seconds() * 1000:.0f}ms\n\n"
        except:
            overview += f"### ❌ FastAPI Backend - OFFLINE\n"
            overview += f"- URL: `{self.fastapi_url}`\n"
            overview += f"- Status: Not accessible\n\n"

        # Check HF Data Engine
        try:
            response = httpx.get(f"{self.hf_engine_url}/api/health", timeout=5)
            data = response.json()
            overview += f"### βœ… HF Data Engine - ONLINE\n"
            overview += f"- URL: `{self.hf_engine_url}`\n"
            overview += f"- Providers: {len(data.get('providers', []))}\n"
            overview += f"- Uptime: {data.get('uptime', 0)}s\n"
            overview += f"- Cache Hit Rate: {data.get('cache', {}).get('hitRate', 0):.2%}\n\n"
        except:
            overview += f"### ❌ HF Data Engine - OFFLINE\n"
            overview += f"- URL: `{self.hf_engine_url}`\n"
            overview += f"- Status: Not accessible\n\n"

        # Resource statistics
        overview += "## πŸ“Š Loaded Resources\n\n"
        for name, data in self.api_resources.items():
            if isinstance(data, dict):
                if 'registry' in data:
                    count = sum(len(v) if isinstance(v, list) else 1 for v in data['registry'].values())
                elif 'providers' in data:
                    count = len(data['providers'])
                else:
                    count = len(data)
            elif isinstance(data, list):
                count = len(data)
            else:
                count = 1

            overview += f"- **{name}:** {count} items\n"

        return overview

    async def force_test_all_sources(self, progress=gr.Progress()) -> Tuple[str, pd.DataFrame]:
        """Force test ALL sources with retries"""
        all_results = []
        total_sources = 0

        # Count total sources
        for resource_name, resource_data in self.api_resources.items():
            if isinstance(resource_data, dict) and 'registry' in resource_data:
                for sources in resource_data['registry'].values():
                    if isinstance(sources, list):
                        total_sources += len(sources)

        progress(0, desc="Initializing force test...")
        current = 0

        # Test unified resources with force
        for resource_name, resource_data in self.api_resources.items():
            if isinstance(resource_data, dict) and 'registry' in resource_data:
                registry = resource_data['registry']

                for source_type, sources in registry.items():
                    if not isinstance(sources, list):
                        continue

                    for source in sources:
                        current += 1
                        name = source.get('name', source.get('id', 'Unknown'))
                        progress(current / max(total_sources, 1), desc=f"Force testing {name}...")

                        base_url = source.get('base_url', source.get('url', ''))
                        if not base_url:
                            continue

                        # Force test with retries
                        result = await self.force_test_endpoint(base_url, retry_count=2, timeout=8)

                        status = "βœ… ONLINE" if result["success"] else "❌ OFFLINE"
                        best_latency = min(
                            [a.get("latency_ms", 99999) for a in result["attempts"] if a.get("success")],
                            default=None
                        )

                        all_results.append({
                            "Name": name,
                            "Source": resource_name,
                            "Category": source.get('category', source.get('chain', source.get('role', 'unknown'))),
                            "Status": status,
                            "Attempts": len(result["attempts"]),
                            "Best Latency": f"{best_latency:.0f}ms" if best_latency else "-",
                            "URL": base_url[:60] + "..." if len(base_url) > 60 else base_url,
                            "Final Result": result["final_status"]
                        })

                        self.force_test_results[name] = result
                        await asyncio.sleep(0.2)  # Rate limiting

        df = pd.DataFrame(all_results) if all_results else pd.DataFrame()

        online = len([r for r in all_results if 'βœ…' in r['Status']])
        offline = len([r for r in all_results if '❌' in r['Status']])

        summary = f"""
# πŸ§ͺ FORCE TEST COMPLETE

**Total Sources Tested:** {len(all_results)}
**βœ… Online:** {online} ({online/max(len(all_results), 1)*100:.1f}%)
**❌ Offline:** {offline} ({offline/max(len(all_results), 1)*100:.1f}%)
**⏱️ Average Response Time:** {sum(float(r['Best Latency'].replace('ms', '')) for r in all_results if r['Best Latency'] != '-') / max(1, len([r for r in all_results if r['Best Latency'] != '-'])):.0f}ms
**πŸ• Completed:** {datetime.now().strftime("%H:%M:%S")}

**Success Rate:** {online/max(len(all_results), 1)*100:.1f}%
"""

        return summary, df

    def test_with_auto_heal(self, endpoints: List[str]) -> Tuple[str, List[Dict]]:
        """Test endpoints and attempt auto-healing for failures"""
        results = []

        for endpoint in endpoints:
            result = {
                "endpoint": endpoint,
                "attempts": []
            }

            # First attempt
            try:
                response = httpx.get(endpoint, timeout=10)
                result["attempts"].append({
                    "status": "success" if response.status_code < 400 else "error",
                    "code": response.status_code,
                    "time": response.elapsed.total_seconds()
                })

                if response.status_code >= 400 and self.auto_heal_enabled:
                    # Attempt auto-heal: retry with different strategies
                    for strategy in ["with_headers", "different_timeout", "follow_redirects"]:
                        time.sleep(1)

                        if strategy == "with_headers":
                            headers = {"User-Agent": "Mozilla/5.0"}
                            response = httpx.get(endpoint, headers=headers, timeout=10)
                        elif strategy == "different_timeout":
                            response = httpx.get(endpoint, timeout=30)
                        else:
                            response = httpx.get(endpoint, timeout=10, follow_redirects=True)

                        result["attempts"].append({
                            "strategy": strategy,
                            "status": "success" if response.status_code < 400 else "error",
                            "code": response.status_code,
                            "time": response.elapsed.total_seconds()
                        })

                        if response.status_code < 400:
                            break

            except Exception as e:
                result["attempts"].append({
                    "status": "failed",
                    "error": str(e)
                })

            results.append(result)

        summary = f"Tested {len(endpoints)} endpoints with auto-heal"
        return summary, results

    def get_detailed_resource_info(self, resource_name: str) -> str:
        """Get ultra-detailed resource information"""
        info = f"# πŸ“‹ Detailed Resource Analysis: {resource_name}\n\n"

        found = False
        for source_file, data in self.api_resources.items():
            if isinstance(data, dict) and 'registry' in data:
                for source_type, sources in data['registry'].items():
                    if not isinstance(sources, list):
                        continue

                    for source in sources:
                        if source.get('name') == resource_name or source.get('id') == resource_name:
                            found = True
                            info += f"## Source File: `{source_file}`\n"
                            info += f"## Source Type: `{source_type}`\n\n"
                            info += "### Configuration\n```json\n"
                            info += json.dumps(source, indent=2)
                            info += "\n```\n\n"

                            # Force test results
                            if resource_name in self.force_test_results:
                                test_result = self.force_test_results[resource_name]
                                info += "### Force Test Results\n\n"
                                info += f"- **Success:** {test_result['success']}\n"
                                info += f"- **Final Status:** {test_result['final_status']}\n"
                                info += f"- **Total Attempts:** {len(test_result['attempts'])}\n\n"

                                info += "#### Attempt Details\n"
                                for attempt in test_result['attempts']:
                                    info += f"\n**Attempt {attempt['attempt']}:**\n"
                                    info += f"- Success: {attempt.get('success', False)}\n"
                                    if 'latency_ms' in attempt:
                                        info += f"- Latency: {attempt['latency_ms']:.0f}ms\n"
                                    if 'status_code' in attempt:
                                        info += f"- Status Code: {attempt['status_code']}\n"
                                    if 'error' in attempt:
                                        info += f"- Error: {attempt['error']}\n"

                            return info

        if not found:
            info += f"❌ Resource '{resource_name}' not found in any loaded files.\n"

        return info

    def export_results_csv(self) -> str:
        """Export test results to CSV"""
        if not self.test_results:
            return "No test results to export"

        df = pd.DataFrame(self.test_results)
        csv_path = f"test_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
        df.to_csv(csv_path, index=False)

        return f"βœ… Results exported to: {csv_path}"


# Initialize monitor
monitor = UltimateCryptoMonitor()


# Build ULTIMATE Gradio Interface
with gr.Blocks(
    title="ULTIMATE Crypto Monitor",
    theme=gr.themes.Base(
        primary_hue="blue",
        secondary_hue="cyan",
    ),
    css="""
    .gradio-container {
        font-family: 'Inter', sans-serif;
    }
    .output-markdown h1 {
        color: #2563eb;
    }
    .output-markdown h2 {
        color: #3b82f6;
    }
    """
) as demo:

    gr.Markdown("""
# πŸš€ ULTIMATE Crypto Data Sources Monitor

**Advanced Real-Time Monitoring with Force Testing & Auto-Healing**

Monitor, test, and auto-heal 200+ cryptocurrency data sources, APIs, and backends.

---
    """)

    # Global settings
    with gr.Row():
        auto_heal_toggle = gr.Checkbox(label="πŸ”§ Enable Auto-Heal", value=False)
        monitoring_toggle = gr.Checkbox(label="πŸ“‘ Enable Real-Time Monitoring", value=False)

    def toggle_auto_heal(enabled):
        monitor.auto_heal_enabled = enabled
        return f"βœ… Auto-heal {'enabled' if enabled else 'disabled'}"

    def toggle_monitoring(enabled):
        monitor.monitoring_active = enabled
        return f"βœ… Monitoring {'enabled' if enabled else 'disabled'}"

    auto_heal_status = gr.Markdown()
    monitoring_status = gr.Markdown()

    auto_heal_toggle.change(fn=toggle_auto_heal, inputs=[auto_heal_toggle], outputs=[auto_heal_status])
    monitoring_toggle.change(fn=toggle_monitoring, inputs=[monitoring_toggle], outputs=[monitoring_status])

    # Main Tabs
    with gr.Tabs():
        # Tab 1: Dashboard
        with gr.Tab("🏠 Dashboard"):
            overview_md = gr.Markdown(monitor.get_comprehensive_overview())
            with gr.Row():
                refresh_btn = gr.Button("πŸ”„ Refresh", variant="primary", size="sm")
                export_btn = gr.Button("πŸ’Ύ Export Report", variant="secondary", size="sm")

            refresh_btn.click(
                fn=lambda: monitor.get_comprehensive_overview(),
                outputs=[overview_md]
            )

        # Tab 2: Force Test All
        with gr.Tab("πŸ§ͺ Force Test"):
            gr.Markdown("""
            ### πŸ’ͺ Force Test All Sources
            Test all data sources with multiple retry attempts and detailed diagnostics.
            This will test **every single API endpoint** with force retries.
            """)

            force_test_btn = gr.Button("⚑ START FORCE TEST", variant="primary", size="lg")
            force_summary = gr.Markdown()
            force_table = gr.Dataframe(wrap=True, interactive=False)

            force_test_btn.click(
                fn=monitor.force_test_all_sources,
                outputs=[force_summary, force_table]
            )

        # Tab 3: Resource Explorer
        with gr.Tab("πŸ” Resource Explorer"):
            gr.Markdown("### Explore and analyze individual resources")

            # Get all resource names
            all_names = []
            for resource_data in monitor.api_resources.values():
                if isinstance(resource_data, dict) and 'registry' in resource_data:
                    for sources in resource_data['registry'].values():
                        if isinstance(sources, list):
                            for source in sources:
                                name = source.get('name', source.get('id'))
                                if name:
                                    all_names.append(name)

            resource_search = gr.Dropdown(
                choices=sorted(set(all_names)),
                label="πŸ”Ž Search Resource",
                interactive=True,
                allow_custom_value=True
            )
            resource_detail = gr.Markdown()

            resource_search.change(
                fn=monitor.get_detailed_resource_info,
                inputs=[resource_search],
                outputs=[resource_detail]
            )

        # Tab 4: FastAPI Monitor
        with gr.Tab("⚑ FastAPI Status"):
            gr.Markdown("### Real-time FastAPI Backend Monitoring")

            fastapi_test_btn = gr.Button("πŸ§ͺ Test All Endpoints", variant="primary")

            def test_fastapi_full():
                endpoints = [
                    "/health", "/api/status", "/api/providers",
                    "/api/pools", "/api/hf/health", "/api/feature-flags",
                    "/api/data/market", "/api/data/news"
                ]

                results = []
                for endpoint in endpoints:
                    try:
                        url = f"{monitor.fastapi_url}{endpoint}"
                        response = httpx.get(url, timeout=10)
                        results.append({
                            "Endpoint": endpoint,
                            "Status": "βœ… Working" if response.status_code < 400 else "⚠️ Error",
                            "Code": response.status_code,
                            "Time": f"{response.elapsed.total_seconds() * 1000:.0f}ms",
                            "Size": f"{len(response.content)} bytes"
                        })
                    except Exception as e:
                        results.append({
                            "Endpoint": endpoint,
                            "Status": "❌ Failed",
                            "Code": "-",
                            "Time": "-",
                            "Size": str(e)[:50]
                        })

                df = pd.DataFrame(results)
                working = len([r for r in results if 'βœ…' in r['Status']])
                summary = f"**{working}/{len(results)} endpoints working**"
                return summary, df

            fastapi_summary = gr.Markdown()
            fastapi_df = gr.Dataframe()

            fastapi_test_btn.click(
                fn=test_fastapi_full,
                outputs=[fastapi_summary, fastapi_df]
            )

        # Tab 5: HF Engine Monitor
        with gr.Tab("πŸ€— HF Data Engine"):
            gr.Markdown("### HuggingFace Data Engine Status")

            hf_test_btn = gr.Button("πŸ§ͺ Test All Endpoints", variant="primary")

            def test_hf_full():
                endpoints = [
                    ("/api/health", "Health"),
                    ("/api/prices?symbols=BTC,ETH,SOL", "Prices"),
                    ("/api/ohlcv?symbol=BTC&interval=1h&limit=5", "OHLCV"),
                    ("/api/sentiment", "Sentiment"),
                    ("/api/market/overview", "Market"),
                    ("/api/cache/stats", "Cache Stats"),
                ]

                results = []
                for endpoint, name in endpoints:
                    try:
                        url = f"{monitor.hf_engine_url}{endpoint}"
                        start = time.time()
                        response = httpx.get(url, timeout=30)
                        latency = (time.time() - start) * 1000

                        results.append({
                            "Endpoint": name,
                            "URL": endpoint.split("?")[0],
                            "Status": "βœ… Working" if response.status_code < 400 else "⚠️ Error",
                            "Latency": f"{latency:.0f}ms",
                            "Size": f"{len(response.content)} bytes"
                        })
                    except Exception as e:
                        results.append({
                            "Endpoint": name,
                            "URL": endpoint.split("?")[0],
                            "Status": "❌ Failed",
                            "Latency": "-",
                            "Size": str(e)[:50]
                        })

                df = pd.DataFrame(results)
                working = len([r for r in results if 'βœ…' in r['Status']])
                summary = f"**{working}/{len(results)} endpoints working**"
                return summary, df

            hf_summary = gr.Markdown()
            hf_df = gr.Dataframe()

            hf_test_btn.click(
                fn=test_hf_full,
                outputs=[hf_summary, hf_df]
            )

        # Tab 6: Custom API Test
        with gr.Tab("🎯 Custom Test"):
            gr.Markdown("### Test Any API Endpoint")

            with gr.Row():
                with gr.Column():
                    custom_url = gr.Textbox(
                        label="URL",
                        placeholder="https://api.example.com/endpoint",
                        lines=1
                    )
                    custom_method = gr.Radio(
                        choices=["GET", "POST", "PUT", "DELETE"],
                        label="Method",
                        value="GET"
                    )
                    custom_headers = gr.Textbox(
                        label="Headers (JSON)",
                        placeholder='{"Authorization": "Bearer token"}',
                        lines=3
                    )
                    custom_retry = gr.Slider(
                        minimum=1,
                        maximum=5,
                        value=3,
                        step=1,
                        label="Retry Attempts"
                    )
                    custom_test_btn = gr.Button("πŸš€ Test", variant="primary", size="lg")

                with gr.Column():
                    custom_result = gr.JSON(label="Result")

            async def test_custom(url, method, headers_str, retries):
                try:
                    headers = json.loads(headers_str) if headers_str else None
                except:
                    headers = None

                result = await monitor.force_test_endpoint(
                    url,
                    method=method,
                    headers=headers,
                    retry_count=int(retries)
                )
                return result

            custom_test_btn.click(
                fn=test_custom,
                inputs=[custom_url, custom_method, custom_headers, custom_retry],
                outputs=[custom_result]
            )

        # Tab 7: Statistics & Analytics
        with gr.Tab("πŸ“Š Analytics"):
            gr.Markdown("### Comprehensive Analytics")

            def get_analytics():
                total_resources = 0
                by_category = defaultdict(int)
                by_source_file = {}

                for filename, data in monitor.api_resources.items():
                    file_count = 0

                    if isinstance(data, dict) and 'registry' in data:
                        for sources in data['registry'].values():
                            if isinstance(sources, list):
                                file_count += len(sources)
                                for source in sources:
                                    cat = source.get('category', source.get('chain', source.get('role', 'unknown')))
                                    by_category[cat] += 1

                    by_source_file[filename] = file_count
                    total_resources += file_count

                analytics = f"""
# πŸ“Š Analytics Dashboard

## Resource Summary

**Total Resources:** {total_resources}

### By Source File
"""
                for filename, count in sorted(by_source_file.items(), key=lambda x: x[1], reverse=True):
                    analytics += f"- **{filename}:** {count} resources\n"

                analytics += "\n### By Category\n"
                for cat, count in sorted(by_category.items(), key=lambda x: x[1], reverse=True):
                    analytics += f"- **{cat}:** {count} resources\n"

                # Create DataFrame
                df_data = [
                    {"Metric": "Total Resources", "Value": total_resources},
                    {"Metric": "Source Files", "Value": len(by_source_file)},
                    {"Metric": "Categories", "Value": len(by_category)},
                    {"Metric": "Avg per File", "Value": f"{total_resources / max(len(by_source_file), 1):.0f}"},
                ]

                return analytics, pd.DataFrame(df_data)

            analytics_md = gr.Markdown()
            analytics_df = gr.Dataframe()

            refresh_analytics_btn = gr.Button("πŸ”„ Refresh Analytics", variant="primary")
            refresh_analytics_btn.click(
                fn=get_analytics,
                outputs=[analytics_md, analytics_df]
            )

            # Auto-load on tab open
            demo.load(fn=get_analytics, outputs=[analytics_md, analytics_df])

    # Footer
    gr.Markdown("""
---
**ULTIMATE Crypto Data Sources Monitor** β€’ v2.0 β€’ Built with ❀️ using Gradio
    """)


if __name__ == "__main__":
    print("πŸš€ Starting ULTIMATE Crypto Monitor Dashboard...")
    print(f"πŸ“Š Loaded {len(monitor.api_resources)} resource files")

    demo.launch(
        server_name="0.0.0.0",
        server_port=7861,
        share=False,
        show_error=True,
        quiet=False
    )