File size: 29,099 Bytes
92c4ae6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
AI-Powered Workflow Optimization System
Intelligent analysis and optimization recommendations for workflows
"""

import asyncio
from dataclasses import asdict, dataclass
from datetime import datetime, timedelta, timezone
from enum import Enum
import json
import logging
import re
from typing import Any, Dict, List, Optional, Tuple

logger = logging.getLogger(__name__)

class OptimizationType(Enum):
    """Types of workflow optimizations"""
    PERFORMANCE = "performance"
    COST = "cost"
    RELIABILITY = "reliability"
    EFFICIENCY = "efficiency"
    SECURITY = "security"
    SCALABILITY = "scalability"

class ImpactLevel(Enum):
    """Impact level of recommendations"""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    CRITICAL = "critical"

@dataclass
class OptimizationRecommendation:
    """AI-generated optimization recommendation"""
    id: str
    type: OptimizationType
    title: str
    description: str
    impact_level: ImpactLevel
    estimated_improvement: Dict[str, float]  # metric -> percentage_improvement
    implementation_effort: str  # "easy", "medium", "complex"
    steps: List[str]
    prerequisites: List[str]
    risks: List[str]
    workflow_section: Optional[str] = None
    confidence_score: float = 0.0  # 0-100
    supporting_data: Optional[Dict[str, Any]] = None

@dataclass
class WorkflowAnalysis:
    """Analysis of a workflow's performance and characteristics"""
    workflow_id: str
    workflow_name: str
    total_nodes: int
    total_edges: int
    integrations_used: List[str]
    complexity_score: float  # 0-100
    estimated_execution_time: float  # seconds
    failure_points: List[Dict[str, Any]]
    bottlenecks: List[Dict[str, Any]]
    optimization_opportunities: List[OptimizationRecommendation]
    analysis_timestamp: datetime

class AIWorkflowOptimizer:
    """AI-powered workflow optimization engine"""

    def __init__(self):
        self.optimization_rules = self._initialize_optimization_rules()
        self.performance_benchmarks = self._initialize_benchmarks()
        self.integration_patterns = self._initialize_integration_patterns()

    def _initialize_optimization_rules(self) -> Dict[OptimizationType, List[Dict]]:
        """Initialize AI optimization rules"""
        return {
            OptimizationType.PERFORMANCE: [
                {
                    "pattern": r"sequential.*api.*calls",
                    "condition": lambda data: self._count_sequential_api_calls(data) > 3,
                    "recommendation": self._recommend_parallel_processing,
                    "impact": ImpactLevel.HIGH,
                    "improvement": {"execution_time": 40, "throughput": 60}
                }
                # Note: batch_processing and validation_optimization rules disabled
                # due to missing implementation methods
            ],
            OptimizationType.COST: [
                {
                    "pattern": r"frequent.*ai.*calls",
                    "condition": lambda data: self._has_frequent_ai_calls(data),
                    "recommendation": self._recommend_ai_optimization,
                    "impact": ImpactLevel.HIGH,
                    "improvement": {"cost_reduction": 35}
                }
                # Note: integration_downgrade rule disabled due to missing implementation
            ],
            OptimizationType.RELIABILITY: [
                {
                    "pattern": r"single.*point.*failure",
                    "condition": lambda data: self._has_single_points_of_failure(data),
                    "recommendation": self._recommend_redundancy,
                    "impact": ImpactLevel.CRITICAL,
                    "improvement": {"reliability": 80}
                }
                # Note: error_handling rule disabled due to missing implementation
            ],
            OptimizationType.EFFICIENCY: [
                {
                    "pattern": r"manual.*approval.*required",
                    "condition": lambda data: self._has_manual_bottlenecks(data),
                    "recommendation": self._recommend_automation,
                    "impact": ImpactLevel.MEDIUM,
                    "improvement": {"cycle_time": 50, "manual_effort": 70}
                }
                # Note: streamlining rule disabled due to missing implementation
            ]
        }

    def _initialize_benchmarks(self) -> Dict[str, Dict]:
        """Initialize performance benchmarks"""
        return {
            "api_response_time": {"good": 0.5, "average": 2.0, "poor": 5.0},  # seconds
            "workflow_success_rate": {"good": 0.99, "average": 0.95, "poor": 0.90},
            "daily_executions": {"small": 100, "medium": 1000, "large": 10000},
            "complexity_threshold": {"simple": 20, "moderate": 50, "complex": 100}
        }

    def _initialize_integration_patterns(self) -> Dict[str, Dict]:
        """Initialize known integration performance patterns"""
        return {
            "salesforce": {
                "avg_response_time": 1.2,
                "rate_limit": 5000,  # per hour
                "batch_size": 200,
                "cost_per_call": 0.00025
            },
            "slack": {
                "avg_response_time": 0.3,
                "rate_limit": 10000,
                "batch_size": 1000,
                "cost_per_call": 0.00001
            },
            "openai": {
                "avg_response_time": 2.5,
                "rate_limit": 3500,
                "batch_size": 20,
                "cost_per_1k_tokens": 0.002
            },
            "gmail": {
                "avg_response_time": 0.8,
                "rate_limit": 2500,
                "batch_size": 100,
                "cost_per_call": 0.00005
            }
        }

    async def analyze_workflow(
        self,
        workflow_data: Dict[str, Any],
        performance_metrics: Optional[Dict[str, Any]] = None
    ) -> WorkflowAnalysis:
        """Perform comprehensive workflow analysis"""
        workflow_id = workflow_data.get("id", "unknown")
        workflow_name = workflow_data.get("name", "Unnamed Workflow")

        # Basic analysis
        nodes = workflow_data.get("nodes", [])
        edges = workflow_data.get("edges", [])
        integrations = self._extract_integrations(nodes)

        # Complexity assessment
        complexity_score = self._calculate_complexity_score(workflow_data)

        # Performance analysis
        execution_time = self._estimate_execution_time(workflow_data, performance_metrics)

        # Identify issues
        failure_points = self._identify_failure_points(workflow_data)
        bottlenecks = self._identify_bottlenecks(workflow_data, performance_metrics)

        # Generate optimization recommendations
        recommendations = await self._generate_recommendations(workflow_data, performance_metrics)

        return WorkflowAnalysis(
            workflow_id=workflow_id,
            workflow_name=workflow_name,
            total_nodes=len(nodes),
            total_edges=len(edges),
            integrations_used=integrations,
            complexity_score=complexity_score,
            estimated_execution_time=execution_time,
            failure_points=failure_points,
            bottlenecks=bottlenecks,
            optimization_opportunities=recommendations,
            analysis_timestamp=datetime.now(timezone.utc)
        )

    async def optimize_workflow_plan(
        self,
        workflow_data: Dict[str, Any],
        optimization_goals: List[OptimizationType],
        constraints: Optional[Dict[str, Any]] = None
    ) -> Dict[str, Any]:
        """Create an optimization plan with specific goals"""
        analysis = await self.analyze_workflow(workflow_data)

        # Filter recommendations by goals
        relevant_recommendations = [
            rec for rec in analysis.optimization_opportunities
            if rec.type in optimization_goals
        ]

        # Sort by impact and effort ratio
        recommendations_by_priority = sorted(
            relevant_recommendations,
            key=lambda x: self._calculate_priority_score(x),
            reverse=True
        )

        # Create optimization phases
        phases = self._create_implementation_phases(recommendations_by_priority, constraints)

        return {
            "workflow_analysis": asdict(analysis),
            "optimization_plan": {
                "goals": [goal.value for goal in optimization_goals],
                "phases": phases,
                "estimated_total_improvement": self._calculate_total_improvement(recommendations_by_priority),
                "implementation_timeline": self._estimate_implementation_timeline(recommendations_by_priority)
            },
            "generated_at": datetime.now(timezone.utc).isoformat()
        }

    async def monitor_workflow_performance(
        self,
        workflow_id: str,
        metrics: Dict[str, Any],
        time_window: int = 24  # hours
    ) -> Dict[str, Any]:
        """Monitor workflow performance and suggest real-time optimizations"""
        # Analyze performance trends
        trends = self._analyze_performance_trends(metrics, time_window)

        # Identify performance degradation
        issues = self._identify_performance_issues(trends)

        # Generate immediate recommendations
        urgent_recommendations = []
        for issue in issues:
            if issue["severity"] in ["high", "critical"]:
                recommendation = await self._generate_urgent_recommendation(issue, metrics)
                urgent_recommendations.append(recommendation)

        return {
            "workflow_id": workflow_id,
            "time_window_hours": time_window,
            "performance_trends": trends,
            "identified_issues": issues,
            "urgent_recommendations": urgent_recommendations,
            "health_score": self._calculate_health_score(metrics, issues),
            "analyzed_at": datetime.now(timezone.utc).isoformat()
        }

    # Helper methods for analysis

    def _extract_integrations(self, nodes: List[Dict]) -> List[str]:
        """Extract integrations used in workflow"""
        integrations = set()
        for node in nodes:
            config = node.get("config", {})
            integration = config.get("integration")
            if integration:
                integrations.add(integration)
        return list(integrations)

    def _calculate_complexity_score(self, workflow_data: Dict[str, Any]) -> float:
        """Calculate workflow complexity score"""
        nodes = workflow_data.get("nodes", [])
        edges = workflow_data.get("edges", [])

        # Base complexity from size
        size_score = min(len(nodes) / 10, 10) * 5 + min(len(edges) / 10, 10) * 5

        # Complexity from node types
        node_types = set(node.get("type", "") for node in nodes)
        type_score = len(node_types) * 3

        # Complexity from conditions and loops
        conditional_nodes = len([n for n in nodes if n.get("type") == "condition"])
        condition_score = conditional_nodes * 8

        # Integration complexity
        integrations = self._extract_integrations(nodes)
        integration_score = len(integrations) * 4

        total_score = size_score + type_score + condition_score + integration_score
        return min(total_score, 100)

    def _estimate_execution_time(
        self,
        workflow_data: Dict[str, Any],
        performance_metrics: Optional[Dict[str, Any]] = None
    ) -> float:
        """Estimate workflow execution time"""
        nodes = workflow_data.get("nodes", [])
        total_time = 0.0

        for node in nodes:
            node_type = node.get("type", "")
            config = node.get("config", {})
            integration = config.get("integration")

            # Base time by node type
            if node_type == "trigger":
                continue  # Triggers don't add to execution time
            elif node_type == "action":
                if integration in self.integration_patterns:
                    total_time += self.integration_patterns[integration]["avg_response_time"]
                else:
                    total_time += 1.0  # Default estimate
            elif node_type == "condition":
                total_time += 0.1  # Minimal time for conditions
            else:
                total_time += 0.5  # Default for other types

        # Apply performance multipliers if metrics available
        if performance_metrics:
            success_rate = performance_metrics.get("success_rate", 1.0)
            avg_time = performance_metrics.get("avg_execution_time", total_time)

            # Use historical average if available and reliable
            if success_rate > 0.8 and avg_time > 0:
                total_time = avg_time

        return total_time

    def _identify_failure_points(self, workflow_data: Dict[str, Any]) -> List[Dict[str, Any]]:
        """Identify potential failure points in workflow"""
        failure_points = []
        nodes = workflow_data.get("nodes", [])

        for i, node in enumerate(nodes):
            issues = []

            # Check for missing error handling
            if node.get("type") == "action" and not node.get("config", {}).get("error_handling"):
                issues.append("No error handling defined")

            # Check for hardcoded values
            config = node.get("config", {})
            if any(isinstance(v, str) and "test" in v.lower() for v in config.values()):
                issues.append("Contains test values in production")

            # Check for API rate limits
            integration = config.get("integration")
            if integration in self.integration_patterns:
                rate_limit = self.integration_patterns[integration]["rate_limit"]
                if rate_limit < 100:  # Low rate limit
                    issues.append(f"Low rate limit ({rate_limit}/hour) may cause throttling")

            if issues:
                failure_points.append({
                    "node_id": node.get("id", f"node_{i}"),
                    "node_type": node.get("type", "unknown"),
                    "issues": issues,
                    "risk_level": "high" if len(issues) > 1 else "medium"
                })

        return failure_points

    def _identify_bottlenecks(
        self,
        workflow_data: Dict[str, Any],
        performance_metrics: Optional[Dict[str, Any]] = None
    ) -> List[Dict[str, Any]]:
        """Identify performance bottlenecks"""
        bottlenecks = []

        # Analyze sequential dependencies
        nodes = workflow_data.get("nodes", [])
        edges = workflow_data.get("edges", [])

        # Find longest path
        longest_path = self._find_longest_path(nodes, edges)
        if len(longest_path) > 5:
            bottlenecks.append({
                "type": "sequential_depth",
                "description": f"Long sequential path with {len(longest_path)} nodes",
                "impact": "high",
                "suggestion": "Consider parallel processing where possible"
            })

        # Check for data processing bottlenecks
        for node in nodes:
            config = node.get("config", {})
            if config.get("process_large_data") or config.get("batch_size", 1) > 1000:
                bottlenecks.append({
                    "type": "data_processing",
                    "node_id": node.get("id"),
                    "description": "Large data processing without optimization",
                    "impact": "medium",
                    "suggestion": "Implement batch processing or streaming"
                })

        return bottlenecks

    async def _generate_recommendations(
        self,
        workflow_data: Dict[str, Any],
        performance_metrics: Optional[Dict[str, Any]] = None
    ) -> List[OptimizationRecommendation]:
        """Generate AI-powered optimization recommendations"""
        recommendations = []
        data_context = {
            "workflow": workflow_data,
            "metrics": performance_metrics or {}
        }

        # Apply optimization rules
        for opt_type, rules in self.optimization_rules.items():
            for rule in rules:
                try:
                    if rule["condition"](data_context):
                        recommendation = rule["recommendation"](data_context, rule)
                        recommendation.type = opt_type
                        recommendations.append(recommendation)
                except Exception as e:
                    logger.error(f"Error applying optimization rule: {e}")

        # Sort by impact and confidence
        recommendations.sort(
            key=lambda x: (x.impact_level.value, x.confidence_score),
            reverse=True
        )

        return recommendations

    # Recommendation generation methods

    def _recommend_parallel_processing(self, data: Dict, rule: Dict) -> OptimizationRecommendation:
        """Recommend parallel processing for sequential API calls"""
        return OptimizationRecommendation(
            id="parallel_processing",
            type=OptimizationType.PERFORMANCE,
            title="Implement Parallel Processing",
            description="Multiple sequential API calls can be executed in parallel to reduce execution time",
            impact_level=rule["impact"],
            estimated_improvement=rule["improvement"],
            implementation_effort="medium",
            steps=[
                "Identify independent API calls in workflow",
                "Group calls that can be executed simultaneously",
                "Implement parallel execution pattern",
                "Add error handling for parallel calls",
                "Test with different batch sizes"
            ],
            prerequisites=["API access for parallel execution", "Error handling framework"],
            risks=["Rate limiting issues", "Increased complexity"],
            confidence_score=85,
            supporting_data={"sequential_calls": self._count_sequential_api_calls(data)}
        )

    def _recommend_ai_optimization(self, data: Dict, rule: Dict) -> OptimizationRecommendation:
        """Recommend AI usage optimization"""
        return OptimizationRecommendation(
            id="ai_optimization",
            type=OptimizationType.COST,
            title="Optimize AI Usage",
            description="Reduce AI API costs through smart caching and provider selection",
            impact_level=rule["impact"],
            estimated_improvement=rule["improvement"],
            implementation_effort="medium",
            steps=[
                "Implement response caching for repeated queries",
                "Use cost-effective AI providers for simple tasks",
                "Batch multiple AI requests where possible",
                "Implement request deduplication",
                "Monitor and optimize prompt lengths"
            ],
            prerequisites=["AI provider access", "Caching infrastructure"],
            risks=["Cache staleness", "Reduced accuracy with cheaper models"],
            confidence_score=90
        )

    def _recommend_redundancy(self, data: Dict, rule: Dict) -> OptimizationRecommendation:
        """Recommend adding redundancy for reliability"""
        return OptimizationRecommendation(
            id="add_redundancy",
            type=OptimizationType.RELIABILITY,
            title="Add Redundancy and Fallbacks",
            description="Implement fallback mechanisms to prevent single points of failure",
            impact_level=rule["impact"],
            estimated_improvement=rule["improvement"],
            implementation_effort="complex",
            steps=[
                "Identify critical failure points",
                "Implement backup integration providers",
                "Add retry logic with exponential backoff",
                "Create manual override procedures",
                "Set up monitoring and alerts"
            ],
            prerequisites=["Multiple integration options", "Monitoring system"],
            risks=["Increased complexity", "Higher costs"],
            confidence_score=95
        )

    def _recommend_automation(self, data: Dict, rule: Dict) -> OptimizationRecommendation:
        """Recommend automation of manual steps"""
        return OptimizationRecommendation(
            id="automation_opportunity",
            type=OptimizationType.EFFICIENCY,
            title="Automate Manual Approvals",
            description="Replace manual approval steps with automated decision rules",
            impact_level=rule["impact"],
            estimated_improvement=rule["improvement"],
            implementation_effort="medium",
            steps=[
                "Analyze approval patterns and criteria",
                "Define automated decision rules",
                "Implement rule-based approval system",
                "Create exception handling workflows",
                "Monitor and adjust rules"
            ],
            prerequisites=["Clear approval criteria", "Stakeholder buy-in"],
            risks=["Errors in automated decisions", "Loss of human oversight"],
            confidence_score=75
        )

    # Utility methods

    def _count_sequential_api_calls(self, data: Dict) -> int:
        """Count sequential API calls in workflow"""
        # Simplified implementation
        nodes = data.get("workflow", {}).get("nodes", [])
        api_nodes = [n for n in nodes if n.get("config", {}).get("integration")]
        return len(api_nodes)

    def _has_large_data_processing(self, data: Dict) -> bool:
        """Check if workflow processes large amounts of data"""
        nodes = data.get("workflow", {}).get("nodes", [])
        for node in nodes:
            config = node.get("config", {})
            if config.get("batch_size", 1) > 1000 or config.get("process_large_files"):
                return True
        return False

    def _has_frequent_ai_calls(self, data: Dict) -> bool:
        """Check if workflow makes frequent AI calls"""
        nodes = data.get("workflow", {}).get("nodes", [])
        ai_nodes = [n for n in nodes if "openai" in str(n.get("config", {})).lower()]
        return len(ai_nodes) > 2

    def _has_single_points_of_failure(self, data: Dict) -> bool:
        """Check for single points of failure"""
        # Simplified implementation
        return len(data.get("workflow", {}).get("nodes", [])) > 5

    def _lacks_error_handling(self, data: Dict) -> bool:
        """Check if workflow lacks proper error handling"""
        nodes = data.get("workflow", {}).get("nodes", [])
        for node in nodes:
            if not node.get("config", {}).get("error_handling"):
                return True
        return False

    def _has_manual_bottlenecks(self, data: Dict) -> bool:
        """Check for manual approval bottlenecks"""
        nodes = data.get("workflow", {}).get("nodes", [])
        for node in nodes:
            if "approval" in str(node.get("label", "")).lower():
                return True
        return False

    def _has_redundant_validations(self, data: Dict) -> bool:
        """Check for redundant validations"""
        # Simplified implementation - in production would analyze validation patterns
        return False

    def _has_underutilized_premium_integrations(self, data: Dict) -> bool:
        """Check for underutilized premium integrations"""
        # Simplified implementation
        return False

    def _has_unnecessary_transformations(self, data: Dict) -> bool:
        """Check for unnecessary data transformations"""
        # Simplified implementation
        return False

    def _find_longest_path(self, nodes: List[Dict], edges: List[Dict]) -> List[str]:
        """Find longest path in workflow graph"""
        # Simplified implementation
        # In practice, this would use graph algorithms
        return [n.get("id", f"node_{i}") for i, n in enumerate(nodes)]

    def _calculate_priority_score(self, recommendation: OptimizationRecommendation) -> float:
        """Calculate priority score for recommendation"""
        impact_weights = {"critical": 4, "high": 3, "medium": 2, "low": 1}
        effort_weights = {"easy": 3, "medium": 2, "complex": 1}

        impact_score = impact_weights.get(recommendation.impact_level.value, 1)
        effort_score = effort_weights.get(recommendation.implementation_effort, 1)

        return (impact_score * recommendation.confidence_score) / (6 - effort_score)

    def _create_implementation_phases(
        self,
        recommendations: List[OptimizationRecommendation],
        constraints: Optional[Dict[str, Any]]
    ) -> List[Dict]:
        """Create phased implementation plan"""
        phases = [
            {
                "phase": 1,
                "name": "Quick Wins",
                "duration_weeks": 1,
                "recommendations": [r for r in recommendations if r.implementation_effort == "easy"][:3],
                "description": "High-impact, low-effort optimizations"
            },
            {
                "phase": 2,
                "name": "Core Optimizations",
                "duration_weeks": 2,
                "recommendations": [r for r in recommendations if r.implementation_effort == "medium"][:5],
                "description": "Significant improvements requiring moderate effort"
            },
            {
                "phase": 3,
                "name": "Advanced Enhancements",
                "duration_weeks": 4,
                "recommendations": [r for r in recommendations if r.implementation_effort == "complex"][:3],
                "description": "Complex optimizations for maximum benefit"
            }
        ]
        return [p for p in phases if p["recommendations"]]

    def _calculate_total_improvement(self, recommendations: List[OptimizationRecommendation]) -> Dict[str, float]:
        """Calculate total expected improvements"""
        improvements = {}
        for rec in recommendations:
            for metric, improvement in rec.estimated_improvement.items():
                improvements[metric] = improvements.get(metric, 0) + improvement
        return improvements

    def _estimate_implementation_timeline(self, recommendations: List[OptimizationRecommendation]) -> str:
        """Estimate total implementation timeline"""
        effort_days = {"easy": 1, "medium": 3, "complex": 7}
        total_days = sum(effort_days.get(rec.implementation_effort, 3) for rec in recommendations)
        return f"{total_days} days"

    def _analyze_performance_trends(self, metrics: Dict[str, Any], time_window: int) -> Dict:
        """Analyze performance trends over time"""
        # Simplified trend analysis
        return {
            "execution_time": "stable",
            "success_rate": "improving",
            "error_rate": "stable",
            "throughput": "increasing"
        }

    def _identify_performance_issues(self, trends: Dict) -> List[Dict]:
        """Identify performance issues from trends"""
        issues = []

        # Example issue detection logic
        if trends.get("success_rate") == "declining":
            issues.append({
                "type": "success_rate_decline",
                "severity": "high",
                "description": "Workflow success rate is declining"
            })

        return issues

    async def _generate_urgent_recommendation(self, issue: Dict, metrics: Dict) -> OptimizationRecommendation:
        """Generate urgent recommendation for performance issue"""
        return OptimizationRecommendation(
            id=f"urgent_{issue['type']}",
            type=OptimizationType.RELIABILITY,
            title="Urgent: Address Performance Issue",
            description=issue["description"],
            impact_level=ImpactLevel.CRITICAL,
            estimated_improvement={"reliability": 50},
            implementation_effort="easy",
            steps=["Investigate root cause", "Apply immediate fix", "Monitor closely"],
            prerequisites=["Access to metrics", "Debugging tools"],
            risks=["Temporary disruption"],
            confidence_score=95
        )

    def _calculate_health_score(self, metrics: Dict, issues: List[Dict]) -> float:
        """Calculate overall workflow health score"""
        base_score = 100

        # Deduct points for issues
        for issue in issues:
            if issue["severity"] == "critical":
                base_score -= 30
            elif issue["severity"] == "high":
                base_score -= 20
            elif issue["severity"] == "medium":
                base_score -= 10

        return max(0, base_score)

# Global AI workflow optimizer instance
_ai_workflow_optimizer = None

def get_ai_workflow_optimizer() -> AIWorkflowOptimizer:
    """Get the global AI workflow optimizer instance"""
    global _ai_workflow_optimizer
    if _ai_workflow_optimizer is None:
        _ai_workflow_optimizer = AIWorkflowOptimizer()
    return _ai_workflow_optimizer