@@ -87,11 +87,11 @@ def delete_cost_with_datasource(self, domain_id: str, data_source_id: str) -> No
87
87
history_vos .delete ()
88
88
89
89
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 ,
95
95
):
96
96
conditions = {"cost_id" : cost_id , "domain_id" : domain_id }
97
97
@@ -108,21 +108,26 @@ def filter_costs(self, **conditions):
108
108
109
109
def list_costs (self , query : dict , domain_id : str ):
110
110
query = self ._change_filter_project_group_id (query , domain_id )
111
+ query = self ._change_filter_v_workspace_id (query , domain_id )
111
112
return self .cost_model .query (** query )
112
113
113
114
def stat_costs (self , query : dict , domain_id : str ):
114
115
query = self ._change_filter_project_group_id (query , domain_id )
116
+ query = self ._change_filter_v_workspace_id (query , domain_id )
117
+
115
118
return self .cost_model .stat (** query )
116
119
117
120
def filter_monthly_costs (self , ** conditions ):
118
121
return self .monthly_cost_model .filter (** conditions )
119
122
120
123
def list_monthly_costs (self , query : dict , domain_id : str ):
121
124
query = self ._change_filter_project_group_id (query , domain_id )
125
+ query = self ._change_filter_v_workspace_id (query , domain_id )
122
126
return self .monthly_cost_model .query (** query )
123
127
124
128
def stat_monthly_costs (self , query : dict , domain_id : str ):
125
129
query = self ._change_filter_project_group_id (query , domain_id )
130
+ query = self ._change_filter_v_workspace_id (query , domain_id )
126
131
return self .monthly_cost_model .stat (** query )
127
132
128
133
def analyze_costs (self , query , domain_id , target = "SECONDARY_PREFERRED" ):
@@ -132,6 +137,7 @@ def analyze_costs(self, query, domain_id, target="SECONDARY_PREFERRED"):
132
137
_LOGGER .debug (f"[analyze_costs] query: { query } " )
133
138
134
139
query = self ._change_filter_project_group_id (query , domain_id )
140
+ query = self ._change_filter_v_workspace_id (query , domain_id )
135
141
return self .cost_model .analyze (** query )
136
142
137
143
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"):
141
147
_LOGGER .debug (f"[analyze_monthly_costs] query: { query } " )
142
148
143
149
query = self ._change_filter_project_group_id (query , domain_id )
150
+ query = self ._change_filter_v_workspace_id (query , domain_id )
144
151
return self .monthly_cost_model .analyze (** query )
145
152
146
153
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"):
150
157
_LOGGER .debug (f"[analyze_yearly_costs] query: { query } " )
151
158
152
159
query = self ._change_filter_project_group_id (query , domain_id )
160
+ query = self ._change_filter_v_workspace_id (query , domain_id )
153
161
return self .monthly_cost_model .analyze (** query )
154
162
155
163
@cache .cacheable (
156
164
key = "cost-analysis:stat-costs:monthly:{domain_id}:{data_source_id}:{query_hash}" ,
157
165
expire = 3600 * 24 ,
158
166
)
159
167
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
161
169
):
162
170
return self .stat_monthly_costs (query , domain_id )
163
171
@@ -166,7 +174,7 @@ def stat_monthly_costs_with_cache(
166
174
expire = 3600 * 24 ,
167
175
)
168
176
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"
170
178
):
171
179
return self .analyze_costs (query , domain_id , target )
172
180
@@ -175,7 +183,7 @@ def analyze_costs_with_cache(
175
183
expire = 3600 * 24 ,
176
184
)
177
185
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"
179
187
):
180
188
return self .analyze_monthly_costs (query , domain_id , target )
181
189
@@ -184,12 +192,12 @@ def analyze_monthly_costs_with_cache(
184
192
expire = 3600 * 24 ,
185
193
)
186
194
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"
188
196
):
189
197
return self .analyze_yearly_costs (query , domain_id , target )
190
198
191
199
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
193
201
):
194
202
self ._check_date_range (query )
195
203
granularity = query ["granularity" ]
@@ -418,3 +426,49 @@ def _change_filter_project_group_id(self, query: dict, domain_id: str) -> dict:
418
426
419
427
query ["filter" ] = change_filter
420
428
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
0 commit comments