File size: 6,385 Bytes
a42b16d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Cost estimation for different deployment platforms."""

from __future__ import annotations

from typing import Any, Dict, List, Optional


class CostEstimator:
    """Estimates deployment costs for different platforms."""

    def estimate_platform_cost(
        self,
        platform: str,
        framework: Optional[str] = None,
        traffic: str = "low",
        storage: str = "small"
    ) -> Dict[str, Any]:
        """Estimate monthly cost for a platform."""
        
        # Base pricing (simplified estimates)
        pricing = {
            "vercel": {
                "hobby": 0,
                "pro": 20,
                "enterprise": "custom",
                "description": "Free tier: 100GB bandwidth, Pro: $20/month unlimited"
            },
            "netlify": {
                "starter": 0,
                "pro": 19,
                "business": 99,
                "description": "Free tier: 100GB bandwidth, Pro: $19/month"
            },
            "aws": {
                "lambda": 0.20,  # per million requests
                "ec2_small": 15,
                "ec2_medium": 30,
                "description": "Pay-as-you-go, ~$15-50/month for small apps"
            },
            "gcp": {
                "cloud_run": 0.40,  # per million requests
                "compute_small": 12,
                "description": "Free tier available, ~$12-40/month"
            },
            "azure": {
                "app_service": 13,
                "functions": 0.20,
                "description": "Free tier available, ~$13-50/month"
            },
            "railway": {
                "starter": 5,
                "pro": 20,
                "description": "$5/month starter, $20/month pro"
            },
            "render": {
                "free": 0,
                "starter": 7,
                "standard": 25,
                "description": "Free tier available, $7-25/month"
            },
            "fly.io": {
                "starter": 0,
                "scale": 10,
                "description": "Free tier: 3 VMs, Scale: $10+/month"
            },
            "kubernetes": {
                "managed": 73,  # GKE, EKS base
                "self_hosted": 50,
                "description": "Managed: $73+/month, Self-hosted: $50+/month"
            },
            "docker": {
                "hosting": 5,
                "description": "Varies by hosting provider, ~$5-20/month"
            }
        }
        
        platform_lower = platform.lower()
        cost_info = pricing.get(platform_lower, {
            "estimated": "Unknown",
            "description": "Cost estimation not available"
        })
        
        # Adjust based on traffic
        base_cost = cost_info.get("pro") or cost_info.get("starter") or cost_info.get("estimated", 0)
        if isinstance(base_cost, (int, float)):
            if traffic == "high":
                base_cost *= 2
            elif traffic == "medium":
                base_cost *= 1.5
        
        return {
            "platform": platform,
            "framework": framework,
            "estimated_monthly_cost": base_cost if isinstance(base_cost, (int, float)) else base_cost,
            "cost_range": self._get_cost_range(platform_lower, traffic),
            "description": cost_info.get("description", ""),
            "traffic_level": traffic,
            "recommendations": self._get_cost_recommendations(platform_lower, base_cost)
        }

    def _get_cost_range(self, platform: str, traffic: str) -> str:
        """Get cost range string."""
        ranges = {
            "vercel": "$0-20/month",
            "netlify": "$0-19/month",
            "aws": "$15-100/month",
            "gcp": "$12-80/month",
            "azure": "$13-100/month",
            "railway": "$5-20/month",
            "render": "$0-25/month",
            "fly.io": "$0-50/month",
            "kubernetes": "$50-200/month",
            "docker": "$5-50/month"
        }
        return ranges.get(platform, "Varies")

    def _get_cost_recommendations(self, platform: str, cost: Any) -> List[str]:
        """Get cost optimization recommendations."""
        recommendations = []
        
        if platform in ["vercel", "netlify", "render"]:
            recommendations.append("Start with free tier for development")
            recommendations.append("Upgrade to paid tier only when needed")
        
        if platform in ["aws", "gcp", "azure"]:
            recommendations.append("Use reserved instances for 30-50% savings")
            recommendations.append("Monitor usage to optimize costs")
        
        if isinstance(cost, (int, float)) and cost > 50:
            recommendations.append("Consider serverless options for cost savings")
            recommendations.append("Review and optimize resource allocation")
        
        return recommendations

    def compare_platforms(self, platforms: List[str], framework: Optional[str] = None) -> Dict[str, Any]:
        """Compare costs across multiple platforms."""
        comparisons = []
        for platform in platforms:
            cost_info = self.estimate_platform_cost(platform, framework)
            comparisons.append(cost_info)
        
        # Sort by cost
        sorted_platforms = sorted(
            [c for c in comparisons if isinstance(c["estimated_monthly_cost"], (int, float))],
            key=lambda x: x["estimated_monthly_cost"]
        )
        
        return {
            "platforms": comparisons,
            "cheapest": sorted_platforms[0] if sorted_platforms else None,
            "recommendation": self._get_best_platform_recommendation(sorted_platforms, framework)
        }

    def _get_best_platform_recommendation(self, sorted_platforms: List[Dict], framework: Optional[str]) -> str:
        """Get recommendation for best platform."""
        if not sorted_platforms:
            return "Compare platforms based on your specific needs"
        
        cheapest = sorted_platforms[0]
        
        if framework == "next.js":
            return f"For Next.js, consider Vercel (optimized) or {cheapest['platform']} (cost-effective)"
        elif framework in ["django", "flask", "fastapi"]:
            return f"For Python apps, {cheapest['platform']} offers good value"
        else:
            return f"{cheapest['platform']} is the most cost-effective option"