Skip to content

Commit 121f6f3

Browse files
authored
Merge pull request #37 from stat-kwon/master
Modify create_billed_date method in cost_manager
2 parents 75d3280 + b478c60 commit 121f6f3

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def _make_cost_data(self, results):
4343
if self.http_file_connector.default_vars:
4444
self._set_default_vars(result)
4545

46+
self._create_billed_date(result)
47+
4648
if not self._convert_cost_and_usage_quantity_types(result):
4749
continue
4850

@@ -61,7 +63,7 @@ def _make_cost_data(self, results):
6163
'region_code': result.get('region_code'),
6264
'product': result.get('product'),
6365
'resource': result.get('resource', ''),
64-
'billed_date': f'{result["year"]}-{result["month"]}',
66+
'billed_date': result['billed_date'],
6567
'additional_info': result.get('additional_info', {}),
6668
'tags': result.get('tags', {})
6769
}
@@ -108,17 +110,35 @@ def _change_result_by_field_mapper(self, result):
108110
del result[actual_additional_field]
109111
result[origin_field] = additional_info
110112

111-
if 'billed_date' in origin_field:
112-
if self._check_billed_date(result):
113-
result['billed_date'] = self._apply_parse_date(result[origin_field])
114-
result['year'] = result[origin_field].year
115-
result['month'] = result[origin_field].month
113+
return result
114+
115+
def _create_billed_date(self, result):
116+
if self._exist_billed_date(result):
117+
billed_date = result['billed_date']
118+
billed_date = self._apply_parse_date(billed_date)
119+
billed_date = str(billed_date.strftime("%Y-%m-%d"))
120+
121+
result['billed_date'] = billed_date
122+
123+
else:
124+
year = result['year']
125+
month = result['month']
126+
day = result.get('day', '01')
127+
128+
if len(month) == 1:
129+
month = f'0{month}'
130+
if len(day) == 1:
131+
day = f'0{day}'
132+
133+
billed_date = f'{year}-{month}-{day}'
134+
135+
result['billed_date'] = billed_date
116136

117137
return result
118138

119139
@staticmethod
120-
def _check_billed_date(result):
121-
if result['billed_date']:
140+
def _exist_billed_date(result):
141+
if result.get('billed_date'):
122142
return True
123143
elif result.get('year') and result.get('month'):
124144
return False
@@ -166,9 +186,3 @@ def _check_required_fields(result):
166186
for field in _REQUIRED_FIELDS:
167187
if field not in result:
168188
raise ERROR_REQUIRED_PARAMETER(key=field)
169-
170-
@staticmethod
171-
def _create_billed_date_format(year, month):
172-
date = f'{year}-{month}'
173-
billed_at_format = '%Y-%m'
174-
return datetime.strptime(date, billed_at_format)

0 commit comments

Comments
 (0)