Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set staging as the default database for S3 metadata storage #234

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cellpack/autopack/FirebaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class FirebaseHandler(object):
_initialized = False
_db = None

def __init__(self):
def __init__(self, default_db=None):
# check if firebase is already initialized
if not FirebaseHandler._initialized:
db_choice = FirebaseHandler.which_db()
db_choice = FirebaseHandler.which_db(default_db=default_db)
if db_choice == "staging":
cred = FirebaseHandler.get_staging_creds()
else:
Expand All @@ -34,14 +34,16 @@ def __init__(self):

# common utility methods
@staticmethod
def which_db():
def which_db(default_db=None):
options = {"1": "dev", "2": "staging"}
print("Choose database:")
if default_db in options.values():
print(f"Using {default_db} database -------------")
return default_db
for key, value in options.items():
print(f"[{key}] {value}")
choice = input("Enter number: ").strip()
print(f"Using {options.get(choice, 'dev')} database -------------")
return options.get(choice, "dev") # default to dev db
return options.get(choice, "dev") # default to dev db for recipe uploads

@staticmethod
def doc_to_dict(doc):
Expand Down Expand Up @@ -101,12 +103,13 @@ def get_staging_creds():
# set override=True to refresh the .env file if softwares or tokens updated
load_dotenv(dotenv_path="./.env", override=False)
FIREBASE_TOKEN = os.getenv("FIREBASE_TOKEN")
firebase_key = FIREBASE_TOKEN.replace("\\n", "\n")
FIREBASE_EMAIL = os.getenv("FIREBASE_EMAIL")
return {
"type": "service_account",
"project_id": "cell-pack-database",
"client_email": FIREBASE_EMAIL,
"private_key": FIREBASE_TOKEN,
"private_key": firebase_key,
"token_uri": "https://oauth2.googleapis.com/token",
}

Expand Down
3 changes: 2 additions & 1 deletion cellpack/autopack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ def load_file(filename, destination="", cache="geometries", force=None):
# command example: `pack -r firebase:recipes/[FIREBASE-RECIPE-ID] -c [CONFIG-FILE-PATH]`
if database_name == "firebase":
db = DATABASE_IDS.handlers().get(database_name)
db_handler = DBRecipeLoader(db)
initialize_db = db()
db_handler = DBRecipeLoader(initialize_db)
recipe_id = file_path.split("/")[-1]
db_doc, _ = db_handler.collect_docs_by_id(
collection="recipes", id=recipe_id
Expand Down
5 changes: 4 additions & 1 deletion cellpack/autopack/interface_objects/database_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def create_aws_handler(bucket_name, sub_folder_name, region_name):
region_name=region_name,
)

def create_firebase_handler(default_db=None):
return FirebaseHandler(default_db=default_db)

handlers_dict = {
cls.FIREBASE: FirebaseHandler(),
cls.FIREBASE: create_firebase_handler,
cls.AWS: create_aws_handler,
}
return handlers_dict
8 changes: 6 additions & 2 deletions cellpack/autopack/upy/simularium/simularium_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,8 +1423,12 @@ def store_result_file(file_path, storage=None):
@staticmethod
def store_metadata(file_name, url, db=None):
if db == "firebase":
db_handler = DBUploader(DATABASE_IDS.handlers().get(db))
db_handler.upload_result_metadata(file_name, url)
handler = DATABASE_IDS.handlers().get(db)
initialized_db = handler(
default_db="staging"
) # default to staging for metadata uploads
db_uploader = DBUploader(initialized_db)
db_uploader.upload_result_metadata(file_name, url)

@staticmethod
def open_in_simularium(aws_url):
Expand Down
Loading