Skip to content

Commit d00248e

Browse files
committed
feat: add change query filter for v_workspace_id
1 parent ba0efae commit d00248e

File tree

3 files changed

+72
-25
lines changed

3 files changed

+72
-25
lines changed

src/spaceone/cost_analysis/manager/cost_manager.py

+64-10
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ def delete_cost_with_datasource(self, domain_id: str, data_source_id: str) -> No
8787
history_vos.delete()
8888

8989
def get_cost(
90-
self,
91-
cost_id: str,
92-
domain_id: str,
93-
workspace_id=None,
94-
user_projects: list = None,
90+
self,
91+
cost_id: str,
92+
domain_id: str,
93+
workspace_id=None,
94+
user_projects: list = None,
9595
):
9696
conditions = {"cost_id": cost_id, "domain_id": domain_id}
9797

@@ -108,21 +108,26 @@ def filter_costs(self, **conditions):
108108

109109
def list_costs(self, query: dict, domain_id: str):
110110
query = self._change_filter_project_group_id(query, domain_id)
111+
query = self._change_filter_v_workspace_id(query, domain_id)
111112
return self.cost_model.query(**query)
112113

113114
def stat_costs(self, query: dict, domain_id: str):
114115
query = self._change_filter_project_group_id(query, domain_id)
116+
query = self._change_filter_v_workspace_id(query, domain_id)
117+
115118
return self.cost_model.stat(**query)
116119

117120
def filter_monthly_costs(self, **conditions):
118121
return self.monthly_cost_model.filter(**conditions)
119122

120123
def list_monthly_costs(self, query: dict, domain_id: str):
121124
query = self._change_filter_project_group_id(query, domain_id)
125+
query = self._change_filter_v_workspace_id(query, domain_id)
122126
return self.monthly_cost_model.query(**query)
123127

124128
def stat_monthly_costs(self, query: dict, domain_id: str):
125129
query = self._change_filter_project_group_id(query, domain_id)
130+
query = self._change_filter_v_workspace_id(query, domain_id)
126131
return self.monthly_cost_model.stat(**query)
127132

128133
def analyze_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
@@ -132,6 +137,7 @@ def analyze_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
132137
_LOGGER.debug(f"[analyze_costs] query: {query}")
133138

134139
query = self._change_filter_project_group_id(query, domain_id)
140+
query = self._change_filter_v_workspace_id(query, domain_id)
135141
return self.cost_model.analyze(**query)
136142

137143
def analyze_monthly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
@@ -141,6 +147,7 @@ def analyze_monthly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
141147
_LOGGER.debug(f"[analyze_monthly_costs] query: {query}")
142148

143149
query = self._change_filter_project_group_id(query, domain_id)
150+
query = self._change_filter_v_workspace_id(query, domain_id)
144151
return self.monthly_cost_model.analyze(**query)
145152

146153
def analyze_yearly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
@@ -150,14 +157,15 @@ def analyze_yearly_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
150157
_LOGGER.debug(f"[analyze_yearly_costs] query: {query}")
151158

152159
query = self._change_filter_project_group_id(query, domain_id)
160+
query = self._change_filter_v_workspace_id(query, domain_id)
153161
return self.monthly_cost_model.analyze(**query)
154162

155163
@cache.cacheable(
156164
key="cost-analysis:stat-costs:monthly:{domain_id}:{data_source_id}:{query_hash}",
157165
expire=3600 * 24,
158166
)
159167
def stat_monthly_costs_with_cache(
160-
self, query, query_hash, domain_id, data_source_id
168+
self, query, query_hash, domain_id, data_source_id
161169
):
162170
return self.stat_monthly_costs(query, domain_id)
163171

@@ -166,7 +174,7 @@ def stat_monthly_costs_with_cache(
166174
expire=3600 * 24,
167175
)
168176
def analyze_costs_with_cache(
169-
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
177+
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
170178
):
171179
return self.analyze_costs(query, domain_id, target)
172180

@@ -175,7 +183,7 @@ def analyze_costs_with_cache(
175183
expire=3600 * 24,
176184
)
177185
def analyze_monthly_costs_with_cache(
178-
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
186+
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
179187
):
180188
return self.analyze_monthly_costs(query, domain_id, target)
181189

@@ -184,12 +192,12 @@ def analyze_monthly_costs_with_cache(
184192
expire=3600 * 24,
185193
)
186194
def analyze_yearly_costs_with_cache(
187-
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
195+
self, query, query_hash, domain_id, data_source_id, target="SECONDARY_PREFERRED"
188196
):
189197
return self.analyze_yearly_costs(query, domain_id, target)
190198

191199
def analyze_costs_by_granularity(
192-
self, query: dict, domain_id: dict, data_source_id: dict
200+
self, query: dict, domain_id: dict, data_source_id: dict
193201
):
194202
self._check_date_range(query)
195203
granularity = query["granularity"]
@@ -418,3 +426,49 @@ def _change_filter_project_group_id(self, query: dict, domain_id: str) -> dict:
418426

419427
query["filter"] = change_filter
420428
return query
429+
430+
def _change_filter_v_workspace_id(self, query: dict, domain_id: str) -> dict:
431+
change_filter = []
432+
workspace_ids = []
433+
data_source_id = self._get_data_source_id_from_filter(query)
434+
435+
for condition in query.get("filter", []):
436+
key = condition.get("k", condition.get("key"))
437+
value = condition.get("v", condition.get("value"))
438+
operator = condition.get("o", condition.get("operator"))
439+
440+
if key == "workspace_id":
441+
query = {
442+
"filter": [
443+
{"k": "domain_id", "v": domain_id, "o": "eq"},
444+
{"k": "data_source_id", "v": data_source_id, "o": "eq"},
445+
{"k": key, "v": value, "o": operator},
446+
]
447+
}
448+
(
449+
ds_accounts_vos,
450+
total_count,
451+
) = self.data_source_account_mgr.list_data_source_accounts(query)
452+
453+
if total_count > 0:
454+
for ds_accounts_vo in ds_accounts_vos:
455+
if ds_accounts_vo.workspace_id:
456+
workspace_ids.append(ds_accounts_vo.workspace_id)
457+
458+
change_filter.append(
459+
{"k": "workspace_id", "v": workspace_ids, "o": "in"}
460+
)
461+
462+
else:
463+
change_filter.append(condition)
464+
query["filter"] = change_filter
465+
return query
466+
467+
@staticmethod
468+
def _get_data_source_id_from_filter(query: dict) -> str:
469+
for condition in query.get("filter", []):
470+
key = condition.get("k", condition.get("key"))
471+
value = condition.get("v", condition.get("value"))
472+
473+
if key == "data_source_id":
474+
return value

src/spaceone/cost_analysis/manager/data_source_account_manager.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ def connect_cost_data(
9494

9595
use_account_routing = plugin_info_metadata.get("use_account_routing", False)
9696

97-
_LOGGER.debug(
98-
f"[connect_cost_data] data_source_id: {data_source_id}, domain_id: {domain_id}, use_account_routing: {use_account_routing}, plugin_info_metadata: {plugin_info_metadata}"
99-
)
100-
10197
ds_account_vo = None
10298
if use_account_routing:
10399
account_connect_polices: list = plugin_info_metadata.get(
@@ -112,10 +108,6 @@ def connect_cost_data(
112108
target_value = utils.get_dict_value(cost_data, source)
113109
operator = policy.get("operator")
114110

115-
_LOGGER.debug(
116-
f"[connect_cost_data] source: {source}, target_key: {target_key}, target_value: {target_value}"
117-
)
118-
119111
if target_value:
120112
ds_account_vo = self._get_data_source_account_vo(
121113
target_key,
@@ -124,11 +116,10 @@ def connect_cost_data(
124116
domain_id,
125117
operator,
126118
)
127-
_LOGGER.debug(
128-
f"[connect_cost_data] ds_account_vo: {ds_account_vo.to_dict()}"
129-
)
130-
if ds_account_vo:
131-
cost_data["account_id"] = ds_account_vo.account_id
119+
120+
if ds_account_vo:
121+
cost_data["account_id"] = ds_account_vo.account_id
122+
cost_data["workspace_id"] = ds_account_vo.v_workspace_id
132123

133124
return cost_data, ds_account_vo
134125

src/spaceone/cost_analysis/manager/data_source_rule_manager.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ def _change_cost_data_with_actions(
155155
target_key, target_value, domain_id, workspace_id
156156
)
157157
if project_info:
158-
cost_data["workspace_id"] = project_info.get("workspace_id")
159158
cost_data["project_id"] = project_info["project_id"]
159+
if not data_source_account_vo:
160+
cost_data["workspace_id"] = project_info.get("workspace_id")
160161

161162
elif action == "match_service_account" and value:
162163
source = value["source"]
@@ -170,10 +171,11 @@ def _change_cost_data_with_actions(
170171
cost_data["service_account_id"] = service_account_info[
171172
"service_account_id"
172173
]
174+
cost_data["project_id"] = service_account_info.get("project_id")
175+
if not data_source_account_vo:
173176
cost_data["workspace_id"] = service_account_info.get(
174177
"workspace_id"
175178
)
176-
cost_data["project_id"] = service_account_info.get("project_id")
177179

178180
if action == "add_additional_info" and value:
179181
cost_data["additional_info"] = cost_data.get("additional_info", {})

0 commit comments

Comments
 (0)