Skip to content

Commit

Permalink
Merge pull request #37 from stat-kwon/master
Browse files Browse the repository at this point in the history
Modify create_billed_date method in cost_manager
  • Loading branch information
stat-kwon authored Sep 21, 2023
2 parents 75d3280 + b478c60 commit 121f6f3
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/cloudforet/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def _make_cost_data(self, results):
if self.http_file_connector.default_vars:
self._set_default_vars(result)

self._create_billed_date(result)

if not self._convert_cost_and_usage_quantity_types(result):
continue

Expand All @@ -61,7 +63,7 @@ def _make_cost_data(self, results):
'region_code': result.get('region_code'),
'product': result.get('product'),
'resource': result.get('resource', ''),
'billed_date': f'{result["year"]}-{result["month"]}',
'billed_date': result['billed_date'],
'additional_info': result.get('additional_info', {}),
'tags': result.get('tags', {})
}
Expand Down Expand Up @@ -108,17 +110,35 @@ def _change_result_by_field_mapper(self, result):
del result[actual_additional_field]
result[origin_field] = additional_info

if 'billed_date' in origin_field:
if self._check_billed_date(result):
result['billed_date'] = self._apply_parse_date(result[origin_field])
result['year'] = result[origin_field].year
result['month'] = result[origin_field].month
return result

def _create_billed_date(self, result):
if self._exist_billed_date(result):
billed_date = result['billed_date']
billed_date = self._apply_parse_date(billed_date)
billed_date = str(billed_date.strftime("%Y-%m-%d"))

result['billed_date'] = billed_date

else:
year = result['year']
month = result['month']
day = result.get('day', '01')

if len(month) == 1:
month = f'0{month}'
if len(day) == 1:
day = f'0{day}'

billed_date = f'{year}-{month}-{day}'

result['billed_date'] = billed_date

return result

@staticmethod
def _check_billed_date(result):
if result['billed_date']:
def _exist_billed_date(result):
if result.get('billed_date'):
return True
elif result.get('year') and result.get('month'):
return False
Expand Down Expand Up @@ -166,9 +186,3 @@ def _check_required_fields(result):
for field in _REQUIRED_FIELDS:
if field not in result:
raise ERROR_REQUIRED_PARAMETER(key=field)

@staticmethod
def _create_billed_date_format(year, month):
date = f'{year}-{month}'
billed_at_format = '%Y-%m'
return datetime.strptime(date, billed_at_format)

0 comments on commit 121f6f3

Please sign in to comment.