Skip to content

Commit 577bee5

Browse files
committed
refactor: optimize memory when synchronizing costs
1 parent 56669c3 commit 577bee5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/spaceone/cost_analysis/service/job_service.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -869,16 +869,20 @@ def _aggregate_monthly_cost_data(
869869
{"k": "job_task_id", "v": job_task_id, "o": "eq"},
870870
],
871871
"allow_disk_use": True, # Allow disk use for large data
872+
"return_type": "cursor", # Return type is cursor
872873
}
873874

874875
for data_key in data_keys:
875876
query["fields"].update(
876877
{f"data_{data_key}": {"key": f"data.{data_key}", "operator": "sum"}}
877878
)
878879

879-
response = self.cost_mgr.analyze_costs(query, domain_id, target="PRIMARY")
880-
results = response.get("results", [])
881-
for aggregated_cost_data in results:
880+
# response = self.cost_mgr.analyze_costs(query, domain_id, target="PRIMARY")
881+
# results = response.get("results", [])
882+
cursor = self.cost_mgr.analyze_costs(query, domain_id, target="PRIMARY")
883+
884+
row_count = 0
885+
for aggregated_cost_data in cursor:
882886
aggregated_cost_data["data_source_id"] = data_source_id
883887
aggregated_cost_data["billed_month"] = billed_month
884888
aggregated_cost_data["job_id"] = job_id
@@ -891,9 +895,10 @@ def _aggregate_monthly_cost_data(
891895
f"data_{data_key}", None
892896
)
893897
self.cost_mgr.create_monthly_cost(aggregated_cost_data)
898+
row_count += 1
894899

895900
_LOGGER.debug(
896-
f"[_aggregate_monthly_cost_data] create monthly costs ({billed_month}): {job_id} (count = {len(results)})"
901+
f"[_aggregate_monthly_cost_data] create monthly costs ({billed_month}): {job_id} (count = {row_count})"
897902
)
898903

899904
def _get_all_data_sources(self):

0 commit comments

Comments
 (0)