Skip to content

Commit

Permalink
check and delete expired results
Browse files Browse the repository at this point in the history
  • Loading branch information
rugeli committed Mar 5, 2024
1 parent c3c87c1 commit 82a7587
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cellpack/autopack/DBRecipeHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from enum import Enum

from deepdiff import DeepDiff
import requests

from cellpack.autopack.utils import deep_merge

Expand Down Expand Up @@ -376,6 +377,30 @@ def should_write(self, db, grad_name):
return None, None


class ResultDoc(DataDoc):
def __init__(self, db):
self.db = db

def check_expired_results(self):
"""
Check if the results in the database are expired and delete them if the linked object expired.
"""
current_utc = self.db.create_timestamp()
results = self.db.collection("results").stream()
for result in results:
result_data = self.db.doc_to_dict(result)
result_age = current_utc - result_data["timestamp"]
if result_age.days > 180 and not self.check_url_expires(result_data["url"]):
self.db.delete_doc("results", self.db.doc_id(result))

def check_url_exists(self, url):
"""
Check if the S3 object at the URL exists.
"""
response = requests.head(url)
return response.status_code == 200


class DBUploader(object):
"""
Handles the uploading of data to the database.
Expand Down Expand Up @@ -710,3 +735,19 @@ def compile_db_recipe_data(db_recipe_data, obj_dict, grad_dict, comp_dict):
if grad_dict:
recipe_data["gradients"] = [{**v} for v in grad_dict.values()]
return recipe_data


class DBMaintenance(object):
"""
Handles the maintenance of the database.
"""

def __init__(self, db_handler):
self.db = db_handler
self.result_doc = ResultDoc(self.db)

def cleanup_results(self):
"""
Check if the results in the database are expired and delete them if the linked object expired.
"""
self.result_doc.check_expired_results()
7 changes: 7 additions & 0 deletions cellpack/autopack/FirebaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ def update_or_create(self, collection, id, data):
except NotFound:
self.set_doc(collection, id, data)

# Delete methods
def delete_doc(self, collection, id):
doc_ref = self.db.collection(collection).document(id)
doc_ref.delete()
print(f"successfully deleted path: {doc_ref.path}")
return doc_ref.id

# other utils
@staticmethod
def write_creds_path():
Expand Down

0 comments on commit 82a7587

Please sign in to comment.