Skip to content

Commit

Permalink
feat: add usage type details for network traffic
Browse files Browse the repository at this point in the history
Signed-off-by: ImMin5 <[email protected]>
  • Loading branch information
ImMin5 committed May 3, 2024
1 parent 70b96ca commit c516637
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/cloudforet/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, *args, **kwargs):
)

def get_linked_accounts(
self, options: dict, secret_data: dict, domain_id: str, schema
self, options: dict, secret_data: dict, domain_id: str, schema
) -> list:
self.azure_cm_connector.create_session(options, secret_data, schema)
billing_account_info = self.azure_cm_connector.get_billing_account()
Expand All @@ -50,7 +50,7 @@ def get_linked_accounts(
return accounts_info

def get_data(
self, options: dict, secret_data: dict, schema, task_options: dict
self, options: dict, secret_data: dict, schema, task_options: dict
) -> list:
self.azure_cm_connector.create_session(options, secret_data, schema)
self._check_task_options(task_options)
Expand Down Expand Up @@ -96,7 +96,7 @@ def get_data(
yield []

def _make_cost_data(
self, results: list, end: datetime, options: dict, tenant_id: str = None
self, results: list, end: datetime, options: dict, tenant_id: str = None
) -> list:
"""Source Data Model"""

Expand All @@ -122,7 +122,7 @@ def _make_cost_data(
return costs_data

def _make_data_info(
self, result: dict, billed_date: str, options: dict, tenant_id: str = None
self, result: dict, billed_date: str, options: dict, tenant_id: str = None
):
additional_info = self._get_additional_info(result, options, tenant_id)
cost = self._get_cost_from_result_with_options(result, options)
Expand All @@ -134,6 +134,10 @@ def _make_data_info(
tags = self._convert_tags_str_to_dict(result.get("tags"))

aggregate_data = self._get_aggregate_data(result, options)
# Set Network Traffic Cost at Additional Info
additional_info = self._set_network_traffic_cost(
additional_info, product, usage_type
)

data = {
"cost": cost,
Expand Down Expand Up @@ -183,24 +187,26 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
additional_info["Benefit Name"] = benefit_name

if (
result.get("pricingmodel") == "Reservation"
and result["metercategory"] == ""
result.get("pricingmodel") == "Reservation"
and result["metercategory"] == ""
):
result["metercategory"] = self._set_product_from_benefit_name(
benefit_name
)
if result.get("benefitid") != "" and result.get("benefitid"):
additional_info["Benefit Id"] = result["benefitid"]

if result.get("metersubcategory") != "" and result.get("metersubcategory"):
additional_info["Meter SubCategory"] = result.get("metersubcategory")
if (
result.get("pricingmodel") == "OnDemand"
and result.get("metercategory") == ""
result.get("pricingmodel") == "OnDemand"
and result.get("metercategory") == ""
):
result["metercategory"] = result.get("metercategory")

if result.get("customername") is None:
if result.get("invoicesectionname") != "" and result.get(
"invoicesectionname"
"invoicesectionname"
):
additional_info["Department Name"] = result.get("invoicesectionname")
elif result.get("departmentname") != "" and result.get("departmentname"):
Expand All @@ -209,15 +215,15 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
if result.get("accountname") != "" and result.get("accountname"):
additional_info["Enrollment Account Name"] = result["accountname"]
elif result.get("enrollmentaccountname") != "" and result.get(
"enrollmentaccountname"
"enrollmentaccountname"
):
additional_info["Enrollment Account Name"] = result["enrollmentaccountname"]

collect_resource_id = options.get("collect_resource_id", False)
if (
collect_resource_id
and result.get("resourceid") != ""
and result.get("resourceid")
collect_resource_id
and result.get("resourceid") != ""
and result.get("resourceid")
):
additional_info["Resource Id"] = result["resourceid"]
additional_info["Resource Name"] = result["resourceid"].split("/")[-1]
Expand Down Expand Up @@ -301,10 +307,10 @@ def _get_tenant_ids(task_options: dict, collect_scope: str) -> list:

@staticmethod
def _make_scope(
secret_data: dict,
task_options: dict,
collect_scope: str,
customer_tenant_id: str = None,
secret_data: dict,
task_options: dict,
collect_scope: str,
customer_tenant_id: str = None,
):
if collect_scope == "subscription_id":
subscription_id = task_options["subscription_id"]
Expand Down Expand Up @@ -405,7 +411,7 @@ def _convert_date_format_to_utc(date_format: str) -> datetime:
return datetime.strptime(date_format, "%Y-%m-%d").replace(tzinfo=timezone.utc)

def _make_monthly_time_period(
self, start_date: datetime, end_date: datetime
self, start_date: datetime, end_date: datetime
) -> list:
monthly_time_period = []
current_date = end_date
Expand Down Expand Up @@ -435,7 +441,7 @@ def _make_monthly_time_period(

@staticmethod
def _get_linked_customer_tenants(
secret_data: dict, billing_accounts_info: list
secret_data: dict, billing_accounts_info: list
) -> list:
customer_tenants = secret_data.get("customer_tenants", [])
if not customer_tenants:
Expand All @@ -448,7 +454,7 @@ def _get_linked_customer_tenants(

@staticmethod
def _make_accounts_info_from_customer_tenants(
billing_accounts_info: list, customer_tenants: list
billing_accounts_info: list, customer_tenants: list
) -> list:
accounts_info = []
for billing_account_info in billing_accounts_info:
Expand Down Expand Up @@ -479,3 +485,12 @@ def _skip_cost_data_rule(result: dict) -> bool:
if result.get("customertenentname") and not result.get("customertenantid"):
return True
return False

@staticmethod
def _set_network_traffic_cost(
additional_info: dict, product: str, usage_type: str
) -> dict:
if product in ["Bandwidth", "Content Delivery Network"]:
additional_info["Usage Type Details"] = usage_type

return additional_info

0 comments on commit c516637

Please sign in to comment.