Skip to content

Modify get_data cost step #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM cloudforet/python-core:2.0.90
FROM cloudforet/python-core:2

ENV PYTHONUNBUFFERED 1
ENV SPACEONE_PORT 50051
Expand Down
16 changes: 14 additions & 2 deletions src/cloudforet/cost_analysis/conf/cost_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
]
}
BENEFIT_GROUPING = [
{"type": "Dimension", "name": "CustomerTenantId"},
{"type": "Dimension", "name": "CustomerName"},
{"type": "Dimension", "name": "PricingModel"},
{"type": "Dimension", "name": "Frequency"},
{"type": "Dimension", "name": "BenefitId"},
Expand All @@ -40,6 +38,20 @@
{"type": "Dimension", "name": "MeterCategory"},
]

BENEFIT_GROUPING_MPA = [
{"type": "Dimension", "name": "CustomerTenantId"},
{"type": "Dimension", "name": "CustomerName"},
]

BENEFIT_GROUPING_EA = [
{"type": "Dimension", "name": "DepartmentName"},
{"type": "Dimension", "name": "EnrollmentAccountName"},
]

BENEFIT_GROUPING_MCA = [
{"type": "Dimension", "name": "TenantId"},
]

GROUPING_EA_AGREEMENT_OPTION = [
{"type": "Dimension", "name": "DepartmentName"},
{"type": "Dimension", "name": "EnrollmentAccountName"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ def list_billing_accounts(self) -> list:
return billing_accounts_info

def query_usage_http(
self, secret_data: dict, start: datetime, end: datetime, options=None
self,
secret_data: dict,
start: datetime,
end: datetime,
account_agreement_type: str,
options=None,
):
try:
billing_account_id = secret_data["billing_account_id"]
Expand All @@ -183,6 +188,12 @@ def query_usage_http(
"filter": BENEFIT_FILTER,
},
}
if account_agreement_type == "MicrosoftPartnerAgreement":
parameters["dataset"]["grouping"].extend(BENEFIT_GROUPING_MPA)
elif account_agreement_type == "EnterpriseAgreement":
parameters["dataset"]["grouping"].extend(BENEFIT_GROUPING_EA)
else:
parameters["dataset"]["grouping"].extend(BENEFIT_GROUPING_MCA)

while self.next_link:
url = self.next_link
Expand Down Expand Up @@ -240,10 +251,12 @@ def get_cost_data(self, blobs: list, options: dict) -> list:

total_cost_count = 0
for blob in blobs:
cost_csv = self._download_cost_data(blob)
response = self._download_cost_data(blob)

df_chunk = pd.read_csv(
StringIO(cost_csv), low_memory=False, chunksize=_PAGE_SIZE
StringIO(response.text),
low_memory=False,
chunksize=_PAGE_SIZE,
)

for df in df_chunk:
Expand All @@ -253,7 +266,7 @@ def get_cost_data(self, blobs: list, options: dict) -> list:
total_cost_count += len(costs_data)
yield costs_data
del df_chunk
del cost_csv
del response
_LOGGER.debug(f"[get_cost_data] total_cost_count: {total_cost_count}")

def list_by_billing_account(self):
Expand Down Expand Up @@ -341,12 +354,13 @@ def _retry_request(self, response, url, headers, json, retry_count, method="post
raise e

@staticmethod
def _download_cost_data(blob: dict) -> str:
def _download_cost_data(blob: dict) -> requests.Response:
try:
response = requests.get(blob.get("blob_link"))
if response.status_code != 200:
raise ERROR_CONNECTOR_CALL_API(reason=f"{response.reason}")
return response.text
_LOGGER.debug(f"[_download_cost_data] response: {response}")
return response
except Exception as e:
_LOGGER.error(f"[_download_cost_data] download error: {e}", exc_info=True)
raise e
Expand Down
11 changes: 9 additions & 2 deletions src/cloudforet/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def get_benefit_data(
domain_id: str,
):
self.azure_cm_connector.create_session(options, secret_data, schema)
account_agreement_type = task_options.get("agreement_type")
start: datetime = self._get_first_date_of_month(task_options["start"])
end: datetime = datetime.utcnow()

Expand All @@ -74,7 +75,7 @@ def get_benefit_data(
_start = time_period["start"]
_end = time_period["end"]
response_stream = self.azure_cm_connector.query_usage_http(
secret_data, _start, _end
secret_data, _start, _end, account_agreement_type
)

for results in response_stream:
Expand Down Expand Up @@ -452,7 +453,13 @@ def _get_additional_info(self, result: dict, options: dict, tenant_id: str = Non
additional_info["Meter Name"] = result["metername"]

if result.get("term") != "" and result.get("term"):
additional_info["Term"] = result["term"]
term = result.get("term").trim().lower()
if term in ["1year"]:
term = 12
elif term in ["3year"]:
term = 36

additional_info["Term"] = term

if options.get("cost_metric") == "AmortizedCost":
if result.get("pricingmodel") in ["Reservation", "SavingsPlan"]:
Expand Down
Loading