|
7 | 7 | from datetime import datetime, timedelta, timezone |
8 | 8 |
|
9 | 9 | from celery.schedules import crontab |
10 | | -from sqlalchemy import delete, select, text |
| 10 | +from sqlalchemy import delete, select, text, update |
11 | 11 |
|
12 | 12 | from divbase_api.models.project_versions import ProjectVersionDB |
13 | 13 | from divbase_api.models.projects import ProjectDB |
@@ -209,19 +209,21 @@ def update_storage_usage_metrics(): |
209 | 209 | """ |
210 | 210 | s3_file_manager = create_s3_file_manager(url=S3_ENDPOINT_URL) |
211 | 211 | with SyncSessionLocal() as db: |
212 | | - stmt = select(ProjectDB).where(ProjectDB.is_active == True) # noqa: E712 |
213 | | - projects = db.execute(stmt).scalars().all() |
| 212 | + stmt = select(ProjectDB.id, ProjectDB.bucket_name).where(ProjectDB.is_active.is_(True)) |
| 213 | + projects = db.execute(stmt).all() |
214 | 214 |
|
215 | | - for project in projects: |
216 | | - storage_used_bytes = s3_file_manager.get_bucket_usage_bytes(bucket_name=project.bucket_name) |
217 | | - project.storage_used_bytes = storage_used_bytes |
218 | | - db.add(project) |
| 215 | + project_updates = [] |
| 216 | + for project_id, bucket_name in projects: |
| 217 | + storage_used_bytes = s3_file_manager.get_bucket_usage_bytes(bucket_name=bucket_name) |
| 218 | + project_updates.append({"id": project_id, "storage_used_bytes": storage_used_bytes}) |
219 | 219 |
|
| 220 | + with SyncSessionLocal() as db: |
| 221 | + db.execute(update(ProjectDB), project_updates) |
220 | 222 | db.commit() |
221 | 223 |
|
222 | 224 | return { |
223 | 225 | "status": "completed", |
224 | | - "number_of_projects_updated": len(projects), |
| 226 | + "number_of_projects_updated": len(project_updates), |
225 | 227 | } |
226 | 228 |
|
227 | 229 |
|
|
0 commit comments