Skip to content

Commit

Permalink
databases initiation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rugeli committed Oct 23, 2023
1 parent 4f9bdc3 commit 40cd4fa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
13 changes: 13 additions & 0 deletions cellpack/autopack/DBRecipeHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,19 @@ def upload_recipe(self, recipe_meta_data, recipe_data):
key = self._get_recipe_id(recipe_to_save)
self.upload_data("recipes", recipe_to_save, key)

def upload_result_metadata(self, file_name, url):
"""
Upload the metadata of the result file to the database.
"""
if self.db:
username = self.db.get_username()
timestamp = self.db.create_timestamp()
self.db.update_or_create(
"results",
file_name,
{"user": username, "timestamp": timestamp, "url": url.split("?")[0]},
)


class DBRecipeLoader(object):
"""
Expand Down
5 changes: 2 additions & 3 deletions cellpack/autopack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import ssl
import json
from cellpack.autopack.DBRecipeHandler import DBRecipeLoader
from cellpack.autopack.FirebaseHandler import FirebaseHandler
from cellpack.autopack.interface_objects.database_ids import DATABASE_IDS

from cellpack.autopack.loaders.utils import read_json_file, write_json_file
Expand Down Expand Up @@ -384,9 +383,9 @@ def read_text_file(filename, destination="", cache="collisionTrees", force=None)
def load_file(filename, destination="", cache="geometries", force=None):
if is_remote_path(filename):
database_name, file_path = convert_db_shortname_to_url(filename)
# command example: `pack -r firebase:recipes/peroxisomes_surface_gradient_v-nucleus_cube -c examples/packing-configs/peroxisome_packing_config.json`
# command example: `pack -r firebase:recipes/[FIREBASE-RECIPE-ID] -c [CONFIG-FILE-PATH]`
if database_name == "firebase":
db = FirebaseHandler()
db = DATABASE_IDS.handlers().get(database_name)
db_handler = DBRecipeLoader(db)
recipe_id = file_path.split("/")[-1]
db_doc, _ = db_handler.collect_docs_by_id(
Expand Down
18 changes: 18 additions & 0 deletions cellpack/autopack/interface_objects/database_ids.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
from .meta_enum import MetaEnum
from cellpack.autopack.AWSHandler import AWSHandler
from cellpack.autopack.FirebaseHandler import FirebaseHandler


class DATABASE_IDS(MetaEnum):
FIREBASE = "firebase"
GITHUB = "github"
AWS = "aws"

@classmethod
def with_colon(cls):
return [f"{ele}:" for ele in cls.values()]

@classmethod
def handlers(cls):
def create_aws_handler(bucket_name, sub_folder_name, region_name):
return AWSHandler(
bucket_name=bucket_name,
sub_folder_name=sub_folder_name,
region_name=region_name,
)

handlers_dict = {
cls.FIREBASE: FirebaseHandler(),
cls.AWS: create_aws_handler,
}
return handlers_dict
30 changes: 20 additions & 10 deletions cellpack/autopack/upy/simularium/simularium_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from simulariumio.constants import DISPLAY_TYPE, VIZ_TYPE

from cellpack.autopack.upy import hostHelper
from cellpack.autopack.AWSHandler import AWSHandler
from cellpack.autopack.DBRecipeHandler import DBUploader
from cellpack.autopack.interface_objects.database_ids import DATABASE_IDS
import collada


Expand Down Expand Up @@ -1387,7 +1388,7 @@ def post_and_open_file(self, file_name):
simularium_file = Path(f"{file_name}.simularium")
url = None
try:
url = simulariumHelper.store_results_to_s3(simularium_file)
_, url = simulariumHelper.store_result_file(simularium_file, storage="aws")
except Exception as e:
aws_readme_url = "https://github.com/mesoscope/cellpack/blob/feature/main/README.md#aws-s3"
if isinstance(e, NoCredentialsError):
Expand All @@ -1402,14 +1403,23 @@ def post_and_open_file(self, file_name):
simulariumHelper.open_in_simularium(url)

@staticmethod
def store_results_to_s3(file_path):
handler = AWSHandler(
bucket_name="cellpack-results",
sub_folder_name="simularium/",
region_name="us-west-2",
)
url = handler.save_file(file_path)
return url
def store_result_file(file_path, storage=None):
if storage == "aws":
handler = DATABASE_IDS.handlers().get(storage)
initialized_handler = handler(
bucket_name="cellpack-results",
sub_folder_name="simularium/",
region_name="us-west-2",
)
file_name, url = initialized_handler.save_file(file_path)
simulariumHelper.store_metadata(file_name, url, db="firebase")
return file_name, url

@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)

@staticmethod
def open_in_simularium(aws_url):
Expand Down

0 comments on commit 40cd4fa

Please sign in to comment.