Skip to content

Commit a1b95c3

Browse files
authored
Merge pull request #34 from stat-kwon/master
Refactor cost spec because of changing cost-analysis service
2 parents 12be347 + 6f2b0fa commit a1b95c3

8 files changed

Lines changed: 29 additions & 35 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from spaceone.core.error import *
22

33

4-
class ERROR_EMPTY_BILLED_AT(ERROR_UNKNOWN):
5-
_message = 'Must have billed_at field or year, month and day fields.: {result}'
4+
class ERROR_EMPTY_BILLED_DATE(ERROR_UNKNOWN):
5+
_message = 'Must have billed_date field or year, month fields.: {result}'

src/cloudforet/cost_analysis/info/cost_info.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@ def CostInfo(cost_data):
1313
try:
1414
info = {
1515
'cost': cost_data['cost'],
16-
'currency': cost_data['currency'],
17-
'usage_quantity': cost_data['usage_quantity'],
16+
'usage_quantity': cost_data.get('usage_quantity'),
17+
'usage_type': cost_data.get('usage_type'),
18+
'usage_unit': cost_data.get('usage_unit'),
1819
'provider': cost_data['provider'],
1920
'region_code': cost_data['region_code'],
20-
'category': cost_data.get('category'),
2121
'product': cost_data['product'],
22-
'account': cost_data['account'],
23-
'usage_type': cost_data.get('usage_type'),
24-
'resource_group': cost_data.get('resource_group'),
2522
'resource': cost_data.get('resource'),
2623
'tags': change_struct_type(cost_data['tags']) if 'tags' in cost_data else None,
2724
'additional_info': change_struct_type(
2825
cost_data['additional_info']) if 'additional_info' in cost_data else None,
29-
'billed_at': utils.datetime_to_iso8601(cost_data['billed_at'])
26+
'billed_date': cost_data['billed_date']
3027
}
3128

3229
return cost_pb2.CostInfo(**info)

src/cloudforet/cost_analysis/manager/cost_manager.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
'currency',
1313
'year',
1414
'month',
15-
'day'
1615
]
1716

1817

@@ -52,19 +51,19 @@ def _make_cost_data(self, results):
5251

5352
self._check_required_fields(result)
5453

54+
result['additional_info']['Account'] = result.get('account')
55+
5556
try:
5657
data = {
5758
'cost': result['cost'],
58-
'currency': result['currency'],
5959
'usage_quantity': result.get('usage_quantity', 0),
60+
'usage_type': result.get('usage_type'),
61+
'usage_unit': result.get('usage_unit'),
6062
'provider': result.get('provider'),
6163
'region_code': result.get('region_code'),
6264
'product': result.get('product'),
63-
'account': str(result.get('account')),
64-
'usage_type': result.get('usage_type'),
6565
'resource': result.get('resource', ''),
66-
'resource_group': result.get('resource_group', ''),
67-
'billed_at': self._create_billed_at_format(result['year'], result['month'], result['day']),
66+
'billed_date': f'{result["year"]}-{result["month"]}',
6867
'additional_info': result.get('additional_info', {}),
6968
'tags': result.get('tags', {})
7069
}
@@ -111,24 +110,23 @@ def _change_result_by_field_mapper(self, result):
111110
del result[actual_additional_field]
112111
result[origin_field] = additional_info
113112

114-
if 'billed_at' in origin_field:
115-
if self._check_billed_at(result):
116-
result['billed_at'] = self._apply_parse_date(result[origin_field])
113+
if 'billed_date' in origin_field:
114+
if self._check_billed_date(result):
115+
result['billed_date'] = self._apply_parse_date(result[origin_field])
117116
result['year'] = result[origin_field].year
118117
result['month'] = result[origin_field].month
119-
result['day'] = result[origin_field].day
120118

121119
return result
122120

123121
@staticmethod
124-
def _check_billed_at(result):
125-
if result['billed_at']:
122+
def _check_billed_date(result):
123+
if result['billed_date']:
126124
return True
127-
elif result.get('year') and result.get('month') and result.get('day'):
125+
elif result.get('year') and result.get('month'):
128126
return False
129127
else:
130128
_LOGGER.error(f'[_is_not_empty_billed_at] billed_at is empty: {result}')
131-
raise ERROR_EMPTY_BILLED_AT(result=result)
129+
raise ERROR_EMPTY_BILLED_DATE(result=result)
132130

133131
@staticmethod
134132
def _apply_parse_date(date):
@@ -172,7 +170,7 @@ def _check_required_fields(result):
172170
raise ERROR_REQUIRED_PARAMETER(key=field)
173171

174172
@staticmethod
175-
def _create_billed_at_format(year, month, day):
176-
date = f'{year}-{month}-{day}'
177-
billed_at_format = '%Y-%m-%d'
173+
def _create_billed_date_format(year, month):
174+
date = f'{year}-{month}'
175+
billed_at_format = '%Y-%m'
178176
return datetime.strptime(date, billed_at_format)

src/cloudforet/cost_analysis/manager/job_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_tasks(self, options, secret_data, schema, start, last_synchronized_at):
2727

2828
tasks.append({'task_options': task_options})
2929
changed.append({
30-
'start': '1900-01-01T00:00:00Z'
30+
'start': '1900-01-01'
3131
})
3232

3333
_LOGGER.debug(f'[get_tasks] tasks: {tasks}')

src/cloudforet/cost_analysis/model/cost_model.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66

77
class Cost(Model):
88
cost = FloatType(required=True)
9-
currency = StringType(default='USD')
109
usage_quantity = FloatType(required=True)
10+
usage_type = StringType()
11+
usage_unit = StringType(default=None)
1112
provider = StringType(required=True)
1213
region_code = StringType()
1314
product = StringType()
14-
account = StringType(required=True)
15-
usage_type = StringType()
1615
resource = StringType()
17-
resource_group = StringType()
18-
billed_at = DateTimeType(required=True)
16+
billed_date = StringType(required=True, max_length=7)
1917
additional_info = DictType(StringType, default={})
2018
tags = DictType(StringType, default={})

src/cloudforet/cost_analysis/model/data_source_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ class DataSourceRule(Model):
3535

3636
class PluginMetadata(Model):
3737
data_source_rules = ListType(ModelType(DataSourceRule), default=[])
38+
currency = StringType(required=True, default='USD')

src/cloudforet/cost_analysis/model/job_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Task(Model):
1414

1515

1616
class Changed(Model):
17-
start = DateTimeType(required=True)
18-
end = DateTimeType(default=None)
17+
start = StringType(required=True, max_length=7)
18+
end = StringType(default=None, max_length=7)
1919

2020

2121
class Tasks(Model):

src/cloudforet/cost_analysis/service/job_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, *args, **kwargs):
1717

1818
@transaction
1919
@check_required(['options', 'secret_data'])
20-
@change_timestamp_value(['start', 'last_synchronized_at'], timestamp_format='iso8601')
20+
@change_timestamp_value(['last_synchronized_at'], timestamp_format='iso8601')
2121
def get_tasks(self, params):
2222
"""Get Job Tasks
2323

0 commit comments

Comments
 (0)