Update infrastructure/cost_estimator.py
Browse files
infrastructure/cost_estimator.py
CHANGED
|
@@ -56,12 +56,23 @@ class CostEstimator:
|
|
| 56 |
else:
|
| 57 |
self._pricing = self.DEFAULT_PRICING.copy()
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
def estimate_monthly_cost(self, intent: ProvisionResourceIntent) -> Optional[float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
resource_pricing = self._pricing.get(intent.resource_type)
|
| 62 |
if not resource_pricing:
|
|
|
|
| 63 |
return None
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def cost_delta_vs_baseline(
|
| 67 |
self,
|
|
|
|
| 56 |
else:
|
| 57 |
self._pricing = self.DEFAULT_PRICING.copy()
|
| 58 |
|
| 59 |
+
# Cache for estimate_monthly_cost using a hashable key
|
| 60 |
+
self._cost_cache = {}
|
| 61 |
+
|
| 62 |
def estimate_monthly_cost(self, intent: ProvisionResourceIntent) -> Optional[float]:
|
| 63 |
+
# Create a hashable key from the intent's immutable fields
|
| 64 |
+
# (resource_type, size, region, environment) – configuration is omitted as it doesn't affect cost in this demo.
|
| 65 |
+
key = (intent.resource_type, intent.size, intent.region, intent.environment)
|
| 66 |
+
if key in self._cost_cache:
|
| 67 |
+
return self._cost_cache[key]
|
| 68 |
+
|
| 69 |
resource_pricing = self._pricing.get(intent.resource_type)
|
| 70 |
if not resource_pricing:
|
| 71 |
+
self._cost_cache[key] = None
|
| 72 |
return None
|
| 73 |
+
cost = resource_pricing.get(intent.size)
|
| 74 |
+
self._cost_cache[key] = cost
|
| 75 |
+
return cost
|
| 76 |
|
| 77 |
def cost_delta_vs_baseline(
|
| 78 |
self,
|