11import calendar
2- import logging
32import json
3+ import logging
44import time
5- import pandas as pd
6- from typing import Union , Tuple , Any , Generator
75from datetime import datetime , timezone
6+ from typing import Union
87
8+ import pandas as pd
99from spaceone .core .error import *
1010from spaceone .core .manager import BaseManager
1111
12+ from cloudforet .cost_analysis .conf .cost_conf import *
1213from cloudforet .cost_analysis .connector .azure_cost_mgmt_connector import (
1314 AzureCostMgmtConnector ,
1415)
15- from cloudforet .cost_analysis .conf .cost_conf import *
1616
1717_LOGGER = logging .getLogger ("spaceone" )
1818
@@ -462,6 +462,7 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
462462 result .get ("UsageQuantity" , 0.0 )
463463 )
464464 actual_cost = self ._convert_str_to_float_format (result .get ("Cost" , 0.0 ))
465+
465466 data = {
466467 "cost" : 0 ,
467468 "usage_quantity" : usage_quantity ,
@@ -474,6 +475,7 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
474475 },
475476 "additional_info" : additional_info ,
476477 }
478+
477479 return data
478480
479481 @staticmethod
@@ -517,14 +519,22 @@ def _get_cost_from_result_with_options(self, result: dict, options: dict) -> flo
517519 cost_pay_as_you_go = 0.0
518520
519521 if options .get ("include_reservation_cost_at_payg" , False ):
520- pricing_model = result .get ("pricingmodel" )
521- charge_type = result .get ("chargetype" )
522-
523- if (
524- pricing_model in ["Reservation" , "SavingsPlan" ]
525- and charge_type == "Purchase"
526- ):
527- cost_pay_as_you_go = result .get ("costinbillingcurrency" , 0.0 )
522+ if options .get ("cost_metric" ) == "AmortizedCost" :
523+ pricing_model = result .get ("pricingmodel" )
524+ charge_type = result .get ("chargetype" )
525+ if (
526+ pricing_model in ["Reservation" , "SavingsPlan" ]
527+ and charge_type == "Usage"
528+ ):
529+ cost_pay_as_you_go = self ._get_retail_cost (result )
530+ elif options .get ("cost_metric" ) == "ActualCost" :
531+ pricing_model = result .get ("pricingmodel" )
532+ charge_type = result .get ("chargetype" )
533+ if (
534+ pricing_model in ["Reservation" , "SavingsPlan" ]
535+ and charge_type == "Purchase"
536+ ):
537+ cost_pay_as_you_go = self ._get_retail_cost (result )
528538
529539 return cost_pay_as_you_go
530540
@@ -562,12 +572,11 @@ def _get_aggregate_data(self, result: dict, options: dict) -> dict:
562572
563573 return aggregate_data
564574
565- def _get_saved_cost (self , result : dict , cost : float ) -> float :
575+ def _get_retail_cost (self , result : dict ) -> float :
566576 exchange_rate = 1.0
567- saved_cost = 0
568- currency = result .get ("billingcurrency" , "USD" )
569577 meter_id = result .get ("meterid" )
570578 product_id = result .get ("productid" )
579+ currency = result .get ("billingcurrency" , "USD" )
571580 quantity = self ._convert_str_to_float_format (result .get ("quantity" , 0.0 ))
572581
573582 if not self .retail_price_map .get (f"{ meter_id } :{ product_id } " ):
@@ -578,6 +587,13 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
578587
579588 unit_price = self .retail_price_map [f"{ meter_id } :{ product_id } " ]
580589
590+ retail_cost = exchange_rate * quantity * unit_price
591+
592+ return retail_cost
593+
594+ def _get_saved_cost (self , result : dict , cost : float ) -> float :
595+ saved_cost = 0
596+
581597 # if currency != "USD" and quantity > 0:
582598 # cost_in_billing_currency = self._convert_str_to_float_format(
583599 # result.get("costinbillingcurrency", 0.0)
@@ -591,7 +607,7 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
591607 # else:
592608 # exchange_rate = cost_in_billing_currency / cost_in_pricing_currency
593609
594- retail_cost = exchange_rate * quantity * unit_price
610+ retail_cost = self . _get_retail_cost ( result )
595611 if retail_cost :
596612 saved_cost = retail_cost - cost
597613
0 commit comments