Skip to content

Commit 85c87da

Browse files
authored
Merge pull request #117 from lhhyung/master
Add support for Refunds and refine options handling in PAYG cost calculation
2 parents 6574315 + e47eeb3 commit 85c87da

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

src/cloudforet/cost_analysis/conf/cost_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"dimensions": {
1616
"name": "ChargeType",
1717
"operator": "In",
18-
"values": ["Purchase"],
18+
"values": ["Purchase", "Refund"],
1919
}
2020
},
2121
{

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def _make_benefit_cost_data(
416416
if not billed_at:
417417
continue
418418

419-
data = self._make_benefit_cost_info(cb_result, billed_at)
419+
data = self._make_benefit_cost_info(cb_result, options, billed_at)
420420
benefit_costs_data.append(data)
421421

422422
except Exception as e:
@@ -425,7 +425,11 @@ def _make_benefit_cost_data(
425425
_LOGGER.info(f"[get_benefit_data] total count: {total_count}")
426426
return benefit_costs_data
427427

428-
def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
428+
def _make_benefit_cost_info(
429+
self, result: dict, options: dict, billed_at: str
430+
) -> dict:
431+
cost = 0
432+
429433
additional_info = {
430434
"Pricing Model": result.get("PricingModel"),
431435
"Benefit Id": result.get("BenefitId"),
@@ -463,8 +467,16 @@ def _make_benefit_cost_info(self, result: dict, billed_at: str) -> dict:
463467
)
464468
actual_cost = self._convert_str_to_float_format(result.get("Cost", 0.0))
465469

470+
# cost_metric = "AmortizedCost" and include_reservation_cost_at_payg = "ActualCost"
471+
if options.get("include_reservation_cost_at_payg") == "ActualCost":
472+
if options.get("show_reservation_cost_as_retail"):
473+
# TODO: Add logic to show Actual Cost RI/SP as retail
474+
pass
475+
else:
476+
cost = actual_cost
477+
466478
data = {
467-
"cost": 0,
479+
"cost": cost,
468480
"usage_quantity": usage_quantity,
469481
"provider": "azure",
470482
"product": result.get("MeterCategory"),
@@ -518,23 +530,48 @@ def _get_cost_from_result_with_options(self, result: dict, options: dict) -> flo
518530
else:
519531
cost_pay_as_you_go = 0.0
520532

521-
if options.get("include_reservation_cost_at_payg", False):
522-
if options.get("cost_metric") == "AmortizedCost":
533+
if options.get("cost_metric") == "AmortizedCost":
534+
if options.get("include_reservation_cost_at_payg") == "AmortizedCost":
523535
pricing_model = result.get("pricingmodel")
524536
charge_type = result.get("chargetype")
537+
525538
if (
526539
pricing_model in ["Reservation", "SavingsPlan"]
527540
and charge_type == "Usage"
528541
):
529-
cost_pay_as_you_go = self._get_retail_cost(result)
530-
elif options.get("cost_metric") == "ActualCost":
542+
if options.get("show_reservation_cost_as_retail", False):
543+
cost_pay_as_you_go = self._get_retail_cost(result)
544+
# if cost_pay_as_you_go == 0.0:
545+
# cost_pay_as_you_go = self._convert_str_to_float_format(
546+
# result.get("costinbillingcurrency", 0.0)
547+
# )
548+
else:
549+
cost_pay_as_you_go = self._convert_str_to_float_format(
550+
result.get("costinbillingcurrency", 0.0)
551+
)
552+
elif options.get("cost_metric") == "ActualCost":
553+
if options.get("include_reservation_cost_at_payg") == "ActualCost":
531554
pricing_model = result.get("pricingmodel")
532555
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)
556+
557+
if pricing_model in ["Reservation", "SavingsPlan"] and charge_type in [
558+
"Purchase",
559+
"Refund",
560+
]:
561+
if options.get("show_reservation_cost_as_retail", False):
562+
# TODO: Add logic to show Actual Cost RI/SP as retail
563+
# pricing_model = result.get("pricingmodel")
564+
# charge_type = result.get("chargetype")
565+
# if (
566+
# pricing_model in ["Reservation", "SavingsPlan"]
567+
# and charge_type in ["Purchase", "Refund"]
568+
# ):
569+
# cost_pay_as_you_go = self._get_retail_cost(result)
570+
pass
571+
else:
572+
cost_pay_as_you_go = self._convert_str_to_float_format(
573+
result.get("costinbillingcurrency", 0.0)
574+
)
538575

539576
return cost_pay_as_you_go
540577

src/cloudforet/cost_analysis/manager/data_source_manager.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def init_response(options: dict, domain_id: str) -> dict:
9393
"use_account_routing(bool)": False,
9494
"exclude_license_cost(bool)": False,
9595
"include_credit_cost(bool)": False,
96-
"include_reservation_cost_at_payg(bool)": False,
96+
"include_reservation_cost_at_payg(str)": "ActualCost",
97+
"show_reservation_cost_as_retail(bool): False,
9798
"cost_info(dict)": {
9899
"name" :"PayAsYouGo",
99100
"unit" :"KRW"
@@ -122,7 +123,7 @@ def init_response(options: dict, domain_id: str) -> dict:
122123
"use_account_routing": False,
123124
"exclude_license_cost": False,
124125
"include_credit_cost": False,
125-
"include_reservation_cost_at_payg": False,
126+
"include_reservation_cost_at_payg": None,
126127
"cost_info": {},
127128
"data_info": {},
128129
"additional_info": copy.deepcopy(_DEFAULT_METADATA_ADDITIONAL_INFO),
@@ -167,8 +168,13 @@ def init_response(options: dict, domain_id: str) -> dict:
167168
if options.get("include_credit_cost"):
168169
plugin_metadata["include_credit_cost"] = True
169170

170-
if options.get("include_reservation_cost_at_payg"):
171-
plugin_metadata["include_reservation_cost_at_payg"] = True
171+
if options.get("include_reservation_cost_at_payg") == "ActualCost":
172+
plugin_metadata["include_reservation_cost_at_payg"] = "ActualCost"
173+
else:
174+
plugin_metadata["include_reservation_cost_at_payg"] = "AmortizedCost"
175+
176+
if options.get("show_reservation_cost_as_retail"):
177+
plugin_metadata["show_reservation_cost_as_retail"] = True
172178

173179
return {"metadata": plugin_metadata}
174180

0 commit comments

Comments
 (0)