Skip to content

Commit

Permalink
feat: modify the formula for calculating exchange rates
Browse files Browse the repository at this point in the history
Signed-off-by: ImMin5 <[email protected]>
  • Loading branch information
ImMin5 committed Aug 4, 2024
1 parent 8895860 commit 00df3f7
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/cloudforet/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,32 +512,40 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
saved_cost = 0
currency = result.get("billingcurrency", "USD")
meter_id = result.get("meterid")
product_id = result.get("productid")
quantity = self._convert_str_to_float_format(result.get("quantity", 0.0))

if self.retail_price_map.get(meter_id):
unit_price = self.retail_price_map[meter_id]
else:
unit_price = self._get_unit_price_from_meter_id(meter_id)
self.retail_price_map[meter_id] = unit_price
if not self.retail_price_map.get(f"{meter_id}:{product_id}"):
unit_price = self._get_unit_price_from_meter_id(meter_id, product_id)
self.retail_price_map[f"{meter_id}:{product_id}"] = unit_price

unit_price = self.retail_price_map[f"{meter_id}:{product_id}"]

if currency != "USD":
exchange_rate = result.get("exchangeratepricingtobilling", 1.0) or 1.0
cost_in_billing_currency = self._convert_str_to_float_format(
result.get("costinbillingcurrency", 0.0)
)
cost_in_pricing_currency = self._convert_str_to_float_format(
result.get("costinpricingcurrency", 0.0)
)
exchange_rate = cost_in_billing_currency / cost_in_pricing_currency

retail_cost = exchange_rate * quantity * unit_price
if retail_cost:
saved_cost = retail_cost - cost

return saved_cost

def _get_unit_price_from_meter_id(self, meter_id: str) -> float:
def _get_unit_price_from_meter_id(self, meter_id: str, product_id: str) -> float:
unit_price = 0.0
try:
response = self.azure_cm_connector.get_retail_price(meter_id)
items = response.get("Items", [])

for item in items:
if item.get("meterId") == meter_id:
unit_price = item.get("retailPrice", 0.0)
sku_id = item.get("skuId").replace("/", "")
if item.get("meterId") == meter_id and sku_id == product_id:
unit_price = item.get("retailPrice", 1.0)
break

except Exception as e:
Expand Down

0 comments on commit 00df3f7

Please sign in to comment.