Skip to content

Commit 00df3f7

Browse files
committed
feat: modify the formula for calculating exchange rates
Signed-off-by: ImMin5 <[email protected]>
1 parent 8895860 commit 00df3f7

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/cloudforet/cost_analysis/manager/cost_manager.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -512,32 +512,40 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
512512
saved_cost = 0
513513
currency = result.get("billingcurrency", "USD")
514514
meter_id = result.get("meterid")
515+
product_id = result.get("productid")
515516
quantity = self._convert_str_to_float_format(result.get("quantity", 0.0))
516517

517-
if self.retail_price_map.get(meter_id):
518-
unit_price = self.retail_price_map[meter_id]
519-
else:
520-
unit_price = self._get_unit_price_from_meter_id(meter_id)
521-
self.retail_price_map[meter_id] = unit_price
518+
if not self.retail_price_map.get(f"{meter_id}:{product_id}"):
519+
unit_price = self._get_unit_price_from_meter_id(meter_id, product_id)
520+
self.retail_price_map[f"{meter_id}:{product_id}"] = unit_price
521+
522+
unit_price = self.retail_price_map[f"{meter_id}:{product_id}"]
522523

523524
if currency != "USD":
524-
exchange_rate = result.get("exchangeratepricingtobilling", 1.0) or 1.0
525+
cost_in_billing_currency = self._convert_str_to_float_format(
526+
result.get("costinbillingcurrency", 0.0)
527+
)
528+
cost_in_pricing_currency = self._convert_str_to_float_format(
529+
result.get("costinpricingcurrency", 0.0)
530+
)
531+
exchange_rate = cost_in_billing_currency / cost_in_pricing_currency
525532

526533
retail_cost = exchange_rate * quantity * unit_price
527534
if retail_cost:
528535
saved_cost = retail_cost - cost
529536

530537
return saved_cost
531538

532-
def _get_unit_price_from_meter_id(self, meter_id: str) -> float:
539+
def _get_unit_price_from_meter_id(self, meter_id: str, product_id: str) -> float:
533540
unit_price = 0.0
534541
try:
535542
response = self.azure_cm_connector.get_retail_price(meter_id)
536543
items = response.get("Items", [])
537544

538545
for item in items:
539-
if item.get("meterId") == meter_id:
540-
unit_price = item.get("retailPrice", 0.0)
546+
sku_id = item.get("skuId").replace("/", "")
547+
if item.get("meterId") == meter_id and sku_id == product_id:
548+
unit_price = item.get("retailPrice", 1.0)
541549
break
542550

543551
except Exception as e:

0 commit comments

Comments
 (0)