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
@@ -454,6 +454,7 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
454454 result .get ("UsageQuantity" , 0.0 )
455455 )
456456 actual_cost = self ._convert_str_to_float_format (result .get ("Cost" , 0.0 ))
457+
457458 data = {
458459 "cost" : 0 ,
459460 "usage_quantity" : usage_quantity ,
@@ -466,6 +467,7 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
466467 },
467468 "additional_info" : additional_info ,
468469 }
470+
469471 return data
470472
471473 @staticmethod
@@ -509,14 +511,22 @@ def _get_cost_from_result_with_options(self, result: dict, options: dict) -> flo
509511 cost_pay_as_you_go = 0.0
510512
511513 if options .get ("include_reservation_cost_at_payg" , False ):
512- pricing_model = result .get ("pricingmodel" )
513- charge_type = result .get ("chargetype" )
514-
515- if (
516- pricing_model in ["Reservation" , "SavingsPlan" ]
517- and charge_type == "Purchase"
518- ):
519- cost_pay_as_you_go = result .get ("costinbillingcurrency" , 0.0 )
514+ if options .get ("cost_metric" ) == "AmortizedCost" :
515+ pricing_model = result .get ("pricingmodel" )
516+ charge_type = result .get ("chargetype" )
517+ if (
518+ pricing_model in ["Reservation" , "SavingsPlan" ]
519+ and charge_type == "Usage"
520+ ):
521+ cost_pay_as_you_go = self ._get_retail_cost (result )
522+ elif options .get ("cost_metric" ) == "ActualCost" :
523+ pricing_model = result .get ("pricingmodel" )
524+ charge_type = result .get ("chargetype" )
525+ if (
526+ pricing_model in ["Reservation" , "SavingsPlan" ]
527+ and charge_type == "Purchase"
528+ ):
529+ cost_pay_as_you_go = self ._get_retail_cost (result )
520530
521531 return cost_pay_as_you_go
522532
@@ -554,12 +564,11 @@ def _get_aggregate_data(self, result: dict, options: dict) -> dict:
554564
555565 return aggregate_data
556566
557- def _get_saved_cost (self , result : dict , cost : float ) -> float :
567+ def _get_retail_cost (self , result : dict ) -> float :
558568 exchange_rate = 1.0
559- saved_cost = 0
560- currency = result .get ("billingcurrency" , "USD" )
561569 meter_id = result .get ("meterid" )
562570 product_id = result .get ("productid" )
571+ currency = result .get ("billingcurrency" , "USD" )
563572 quantity = self ._convert_str_to_float_format (result .get ("quantity" , 0.0 ))
564573
565574 if not self .retail_price_map .get (f"{ meter_id } :{ product_id } " ):
@@ -570,6 +579,13 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
570579
571580 unit_price = self .retail_price_map [f"{ meter_id } :{ product_id } " ]
572581
582+ retail_cost = exchange_rate * quantity * unit_price
583+
584+ return retail_cost
585+
586+ def _get_saved_cost (self , result : dict , cost : float ) -> float :
587+ saved_cost = 0
588+
573589 # if currency != "USD" and quantity > 0:
574590 # cost_in_billing_currency = self._convert_str_to_float_format(
575591 # result.get("costinbillingcurrency", 0.0)
@@ -583,7 +599,7 @@ def _get_saved_cost(self, result: dict, cost: float) -> float:
583599 # else:
584600 # exchange_rate = cost_in_billing_currency / cost_in_pricing_currency
585601
586- retail_cost = exchange_rate * quantity * unit_price
602+ retail_cost = self . _get_retail_cost ( result )
587603 if retail_cost :
588604 saved_cost = retail_cost - cost
589605
0 commit comments