mintlee commited on
Commit
217a617
·
1 Parent(s): 182876f

add fix auto delete

Browse files
db/__pycache__/mongodb.cpython-310.pyc CHANGED
Binary files a/db/__pycache__/mongodb.cpython-310.pyc and b/db/__pycache__/mongodb.cpython-310.pyc differ
 
db/mongodb.py CHANGED
@@ -28,8 +28,7 @@ def save_file_to_mongodb(uploaded_file, db_name="pptx", collection_name="root_fi
28
 
29
  try:
30
  # Kiểm tra kích thước dữ liệu hiện tại trong DB (bytes → MB)
31
- stats = db.command("dbstats")
32
- db_size_mb = stats.get("storageSize", 0) / (1024 * 1024)
33
 
34
  print(f"📦 Database size: {db_size_mb:.2f} MB")
35
  if db_size_mb > max_db_size_mb:
@@ -134,7 +133,7 @@ def delete_all_files_in_collection(collection_name, db_name="ppt"):
134
 
135
  for file_id in file_ids:
136
  fs.delete(file_id)
137
- print(f"✅ Đã xóa {len(file_ids)} file trong collection '{collection_name}'")
138
  except Exception as e:
139
  print(f"❌ Lỗi khi xóa file: {str(e)}")
140
  finally:
@@ -143,4 +142,18 @@ def delete_all_files_in_collection(collection_name, db_name="ppt"):
143
  def delete_all():
144
  for i in ['root_file', 'final_file']:
145
  for j in ['word', 'exce', 'pptx', 'csv']:
146
- delete_all_files_in_collection(i, db_name=j)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  try:
30
  # Kiểm tra kích thước dữ liệu hiện tại trong DB (bytes → MB)
31
+ db_size_mb = get_total_cluster_size(client)
 
32
 
33
  print(f"📦 Database size: {db_size_mb:.2f} MB")
34
  if db_size_mb > max_db_size_mb:
 
133
 
134
  for file_id in file_ids:
135
  fs.delete(file_id)
136
+ print(f"✅ Đã xóa {len(file_ids)} file trong collection '{collection_name}' của db '{db_name}'")
137
  except Exception as e:
138
  print(f"❌ Lỗi khi xóa file: {str(e)}")
139
  finally:
 
142
  def delete_all():
143
  for i in ['root_file', 'final_file']:
144
  for j in ['word', 'exce', 'pptx', 'csv']:
145
+ delete_all_files_in_collection(i, db_name=j)
146
+
147
+
148
+ def get_total_cluster_size(client):
149
+ total_size = 0
150
+ try:
151
+ for db_name in ['word', 'exce', 'pptx', 'csv']:
152
+ db = client[db_name]
153
+ stats = db.command("dbstats")
154
+ db_size = stats.get("dataSize", 0)
155
+ total_size += db_size
156
+ except Exception as e:
157
+ print(f"❌ Lỗi khi tính dung lượng cluster: {e}")
158
+ return -1
159
+ return total_size / (1024 ** 2)