File size: 15,758 Bytes
b190b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Master Resource Orchestrator

Orchestrates ALL 86+ resources hierarchically - NO IDLE RESOURCES

مدیریت سلسله‌مراتبی همه 86+ منبع - هیچ منبعی بیکار نمی‌ماند

"""

import httpx
import logging
import asyncio
from typing import Dict, Any, List, Optional, Tuple
from datetime import datetime
from enum import Enum

from backend.services.hierarchical_fallback_config import (
    hierarchical_config,
    Priority,
    ResourceConfig
)

logger = logging.getLogger(__name__)


class ResourceStatus(Enum):
    """Status of resource attempt"""
    SUCCESS = "success"
    FAILED = "failed"
    SKIPPED = "skipped"
    TIMEOUT = "timeout"


class MasterResourceOrchestrator:
    """

    Master orchestrator for ALL resources

    تمام 86+ منبع را به صورت سلسله‌مراتبی مدیریت می‌کند

    """
    
    def __init__(self):
        self.config = hierarchical_config
        self.timeout = 10.0
        
        # Statistics tracking
        self.usage_stats = {
            "total_requests": 0,
            "successful_requests": 0,
            "failed_requests": 0,
            "resource_usage": {},  # Track usage per resource
            "priority_distribution": {  # Track which priority level succeeded
                Priority.CRITICAL: 0,
                Priority.HIGH: 0,
                Priority.MEDIUM: 0,
                Priority.LOW: 0,
                Priority.EMERGENCY: 0
            }
        }
    
    async def fetch_with_hierarchy(

        self,

        resource_list: List[ResourceConfig],

        fetch_function: callable,

        max_concurrent: int = 3

    ) -> Tuple[Any, Dict[str, Any]]:
        """

        Fetch data using hierarchical fallback

        دریافت داده با فالبک سلسله‌مراتبی

        

        Args:

            resource_list: List of resources in priority order

            fetch_function: Async function to fetch data from a resource

            max_concurrent: Max concurrent attempts within same priority

        

        Returns:

            (data, metadata) - Data and information about which resource succeeded

        """
        self.usage_stats["total_requests"] += 1
        
        # Group resources by priority
        priority_groups = self._group_by_priority(resource_list)
        
        # Try each priority level
        for priority in [Priority.CRITICAL, Priority.HIGH, Priority.MEDIUM, Priority.LOW, Priority.EMERGENCY]:
            resources_in_priority = priority_groups.get(priority, [])
            
            if not resources_in_priority:
                continue
            
            logger.info(f"🔄 Trying {len(resources_in_priority)} resources at {priority.name} priority")
            
            # Try resources in this priority level
            # If max_concurrent > 1, try multiple resources in parallel
            if max_concurrent > 1 and len(resources_in_priority) > 1:
                result = await self._try_concurrent(
                    resources_in_priority[:max_concurrent],
                    fetch_function,
                    priority
                )
            else:
                result = await self._try_sequential(
                    resources_in_priority,
                    fetch_function,
                    priority
                )
            
            if result:
                data, metadata = result
                self.usage_stats["successful_requests"] += 1
                self.usage_stats["priority_distribution"][priority] += 1
                logger.info(f"✅ SUCCESS at {priority.name} priority: {metadata['resource_name']}")
                return data, metadata
        
        # All resources failed
        self.usage_stats["failed_requests"] += 1
        logger.error(f"❌ ALL {len(resource_list)} resources failed")
        
        raise Exception(f"All {len(resource_list)} resources failed across all priority levels")
    
    def _group_by_priority(

        self,

        resources: List[ResourceConfig]

    ) -> Dict[Priority, List[ResourceConfig]]:
        """Group resources by priority level"""
        groups = {
            Priority.CRITICAL: [],
            Priority.HIGH: [],
            Priority.MEDIUM: [],
            Priority.LOW: [],
            Priority.EMERGENCY: []
        }
        
        for resource in resources:
            groups[resource.priority].append(resource)
        
        return groups
    
    async def _try_sequential(

        self,

        resources: List[ResourceConfig],

        fetch_function: callable,

        priority: Priority

    ) -> Optional[Tuple[Any, Dict[str, Any]]]:
        """Try resources sequentially"""
        for idx, resource in enumerate(resources, 1):
            try:
                logger.info(f"  📡 [{idx}/{len(resources)}] Trying {resource.name}...")
                
                # Track usage
                if resource.name not in self.usage_stats["resource_usage"]:
                    self.usage_stats["resource_usage"][resource.name] = {
                        "attempts": 0,
                        "successes": 0,
                        "failures": 0
                    }
                
                self.usage_stats["resource_usage"][resource.name]["attempts"] += 1
                
                # Attempt to fetch data
                start_time = datetime.utcnow()
                data = await fetch_function(resource)
                end_time = datetime.utcnow()
                
                if data:
                    self.usage_stats["resource_usage"][resource.name]["successes"] += 1
                    
                    metadata = {
                        "resource_name": resource.name,
                        "priority": priority.name,
                        "base_url": resource.base_url,
                        "response_time_ms": int((end_time - start_time).total_seconds() * 1000),
                        "timestamp": int(end_time.timestamp() * 1000)
                    }
                    
                    logger.info(f"  ✅ {resource.name} succeeded in {metadata['response_time_ms']}ms")
                    return data, metadata
                
                logger.warning(f"  ⚠️ {resource.name} returned no data")
                self.usage_stats["resource_usage"][resource.name]["failures"] += 1
                
            except asyncio.TimeoutError:
                logger.warning(f"  ⏱️ {resource.name} timeout")
                self.usage_stats["resource_usage"][resource.name]["failures"] += 1
                continue
                
            except Exception as e:
                logger.warning(f"  ❌ {resource.name} failed: {e}")
                self.usage_stats["resource_usage"][resource.name]["failures"] += 1
                continue
        
        return None
    
    async def _try_concurrent(

        self,

        resources: List[ResourceConfig],

        fetch_function: callable,

        priority: Priority

    ) -> Optional[Tuple[Any, Dict[str, Any]]]:
        """Try multiple resources concurrently (race condition - first success wins)"""
        logger.info(f"  🏁 Racing {len(resources)} resources in parallel...")
        
        tasks = []
        for resource in resources:
            task = self._try_single_resource(resource, fetch_function, priority)
            tasks.append(task)
        
        # Wait for first success or all failures
        for completed_task in asyncio.as_completed(tasks):
            try:
                result = await completed_task
                if result:
                    # Cancel remaining tasks
                    for task in tasks:
                        if not task.done():
                            task.cancel()
                    return result
            except Exception:
                continue
        
        return None
    
    async def _try_single_resource(

        self,

        resource: ResourceConfig,

        fetch_function: callable,

        priority: Priority

    ) -> Optional[Tuple[Any, Dict[str, Any]]]:
        """Try a single resource (used in concurrent mode)"""
        try:
            # Track usage
            if resource.name not in self.usage_stats["resource_usage"]:
                self.usage_stats["resource_usage"][resource.name] = {
                    "attempts": 0,
                    "successes": 0,
                    "failures": 0
                }
            
            self.usage_stats["resource_usage"][resource.name]["attempts"] += 1
            
            start_time = datetime.utcnow()
            data = await fetch_function(resource)
            end_time = datetime.utcnow()
            
            if data:
                self.usage_stats["resource_usage"][resource.name]["successes"] += 1
                
                metadata = {
                    "resource_name": resource.name,
                    "priority": priority.name,
                    "base_url": resource.base_url,
                    "response_time_ms": int((end_time - start_time).total_seconds() * 1000),
                    "timestamp": int(end_time.timestamp() * 1000)
                }
                
                logger.info(f"  🏆 {resource.name} won the race! ({metadata['response_time_ms']}ms)")
                return data, metadata
            
            self.usage_stats["resource_usage"][resource.name]["failures"] += 1
            return None
            
        except Exception as e:
            logger.warning(f"  ❌ {resource.name} failed: {e}")
            self.usage_stats["resource_usage"][resource.name]["failures"] += 1
            return None
    
    def get_usage_statistics(self) -> Dict[str, Any]:
        """

        Get comprehensive usage statistics

        آمار کامل استفاده از منابع

        """
        total_resources = len(self.usage_stats["resource_usage"])
        used_resources = sum(
            1 for stats in self.usage_stats["resource_usage"].values()
            if stats["attempts"] > 0
        )
        successful_resources = sum(
            1 for stats in self.usage_stats["resource_usage"].values()
            if stats["successes"] > 0
        )
        
        # Calculate success rate per priority
        priority_success_rates = {}
        total_priority_requests = sum(self.usage_stats["priority_distribution"].values())
        
        if total_priority_requests > 0:
            for priority, count in self.usage_stats["priority_distribution"].items():
                priority_success_rates[priority.name] = {
                    "count": count,
                    "percentage": round((count / total_priority_requests) * 100, 2)
                }
        
        # Find most used resources
        most_used = sorted(
            self.usage_stats["resource_usage"].items(),
            key=lambda x: x[1]["attempts"],
            reverse=True
        )[:10]
        
        # Find most successful resources
        most_successful = sorted(
            self.usage_stats["resource_usage"].items(),
            key=lambda x: x[1]["successes"],
            reverse=True
        )[:10]
        
        return {
            "overview": {
                "total_requests": self.usage_stats["total_requests"],
                "successful_requests": self.usage_stats["successful_requests"],
                "failed_requests": self.usage_stats["failed_requests"],
                "success_rate": round(
                    (self.usage_stats["successful_requests"] / self.usage_stats["total_requests"] * 100)
                    if self.usage_stats["total_requests"] > 0 else 0,
                    2
                )
            },
            "resource_utilization": {
                "total_resources_in_system": total_resources,
                "resources_used": used_resources,
                "resources_successful": successful_resources,
                "utilization_rate": round((used_resources / total_resources * 100) if total_resources > 0 else 0, 2)
            },
            "priority_distribution": priority_success_rates,
            "top_10_most_used": [
                {
                    "resource": name,
                    "attempts": stats["attempts"],
                    "successes": stats["successes"],
                    "failures": stats["failures"],
                    "success_rate": round((stats["successes"] / stats["attempts"] * 100) if stats["attempts"] > 0 else 0, 2)
                }
                for name, stats in most_used
            ],
            "top_10_most_successful": [
                {
                    "resource": name,
                    "successes": stats["successes"],
                    "attempts": stats["attempts"],
                    "success_rate": round((stats["successes"] / stats["attempts"] * 100) if stats["attempts"] > 0 else 0, 2)
                }
                for name, stats in most_successful
            ]
        }
    
    def get_resource_health_report(self) -> Dict[str, Any]:
        """

        Get health report for all resources

        گزارش سلامت همه منابع

        """
        healthy_resources = []
        degraded_resources = []
        failed_resources = []
        unused_resources = []
        
        for resource_name, stats in self.usage_stats["resource_usage"].items():
            if stats["attempts"] == 0:
                unused_resources.append(resource_name)
            elif stats["successes"] == 0:
                failed_resources.append({
                    "name": resource_name,
                    "attempts": stats["attempts"],
                    "failures": stats["failures"]
                })
            else:
                success_rate = (stats["successes"] / stats["attempts"]) * 100
                
                if success_rate >= 80:
                    healthy_resources.append({
                        "name": resource_name,
                        "success_rate": round(success_rate, 2),
                        "attempts": stats["attempts"]
                    })
                else:
                    degraded_resources.append({
                        "name": resource_name,
                        "success_rate": round(success_rate, 2),
                        "attempts": stats["attempts"],
                        "failures": stats["failures"]
                    })
        
        return {
            "healthy_resources": {
                "count": len(healthy_resources),
                "resources": healthy_resources
            },
            "degraded_resources": {
                "count": len(degraded_resources),
                "resources": degraded_resources
            },
            "failed_resources": {
                "count": len(failed_resources),
                "resources": failed_resources
            },
            "unused_resources": {
                "count": len(unused_resources),
                "resources": unused_resources
            },
            "overall_health": "Healthy" if len(healthy_resources) > len(failed_resources) else "Degraded"
        }


# Global instance
master_orchestrator = MasterResourceOrchestrator()

__all__ = ["MasterResourceOrchestrator", "master_orchestrator", "ResourceStatus"]