-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from fenfisdi/issue/9/insert_cmodels_in_database
Issue/9/insert cmodels in database This PR was initially opened to address issue #9. However The issue was indirectly related to these other issues that will also be closed. closes #9 closes #23 closes #10 closes #21
- Loading branch information
Showing
34 changed files
with
818 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,3 +151,6 @@ cython_debug/ | |
|
||
# Experiments | ||
experiments/ | ||
|
||
# MacOS | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM python:3.8-slim-buster | ||
EXPOSE 8080 | ||
|
||
ENV PYTHONUNBUFFERED 1 | ||
ENV APP_HOME /app | ||
|
||
COPY requirements.txt . | ||
|
||
RUN pip install --no-cache-dir -U pip \ | ||
&& pip install --no-cache-dir -r requirements.txt | ||
|
||
WORKDIR ${APP_HOME} | ||
|
||
COPY src src | ||
COPY main.py . | ||
COPY docker-entrypoint.sh . | ||
COPY .env . | ||
|
||
ENTRYPOINT ["/bin/bash"] | ||
CMD ["./docker-entrypoint.sh"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: "3.5" | ||
services: | ||
user_app: | ||
build: . | ||
container_name: cdslab_models_app | ||
env_file: .env | ||
image: cdslab_user | ||
networks: | ||
- cdslab_models | ||
ports: | ||
- 5000:5000 | ||
volumes: | ||
- ./src:/app/src | ||
|
||
user_mongo: | ||
container_name: cdslab_models_mongo | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: cdsuser | ||
MONGO_INITDB_ROOT_PASSWORD: cdspass | ||
image: mongo:3-xenial | ||
networks: | ||
- cdslab_models | ||
ports: | ||
- 27017:27017 | ||
volumes: | ||
- /tmp/data/cdslab_models/:/data/db | ||
|
||
|
||
networks: | ||
cdslab_models: | ||
name: cdslab_user | ||
driver: bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
if [ -z "${PORT}" ] ; then PORT=8080; fi | ||
|
||
if [ -z "${HOST}" ] ; then HOST=0.0.0.0; fi | ||
|
||
uvicorn main:app --port=${PORT} --host=${HOST} --reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from dotenv import dotenv_values | ||
|
||
|
||
settings = dotenv_values(".env") | ||
db_config = dotenv_values(".db_config") | ||
settings = dotenv_values('.env') | ||
db_config = dotenv_values('.db_config') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,87 @@ | ||
from typing import Generator, Tuple | ||
from typing import Generator, Optional, Tuple, Union | ||
|
||
from pymongo import MongoClient | ||
from pymongo.collection import Collection | ||
from pymongo.database import Database | ||
import mongomock | ||
|
||
from src.config import db_config | ||
from src.utils.patterns import Singleton | ||
|
||
|
||
class MongoClientSingleton(metaclass=Singleton): | ||
db_uri: Optional[str] = db_config.get('MONGO_URI'), | ||
|
||
def __init__( | ||
self, | ||
db_connection: MongoClient = MongoClient(db_uri), | ||
db: Union[str, Database] = db_config.get('MONGO_DB'), | ||
coll: Union[str, Collection] = db_config.get('CMODELS_COLL') | ||
) -> None: | ||
self.db_connection = db_connection | ||
self.db = db | ||
self.coll = coll | ||
|
||
def api_get_db_connection(self) -> Generator[MongoClient, None, None]: | ||
"""Creates connection to Mongodb server. | ||
Yields | ||
------ | ||
db_connection: MongoClient | ||
Object containing the db connection. | ||
""" | ||
try: | ||
yield self.db_connection | ||
finally: | ||
self.db_connection.close() | ||
|
||
def get_db( | ||
self, | ||
db: Optional[str] = None | ||
) -> Tuple[MongoClient, Database]: | ||
"""Gets Mongodb connection and database. | ||
Returns | ||
------- | ||
db_connection : MongoClient | ||
db : pymongo.database.Database | ||
""" | ||
if db and isinstance(db, str): | ||
self.db = db | ||
return self.db_connection, self.db | ||
|
||
def get_collection( | ||
self, | ||
coll: Optional[str] = None | ||
) -> Tuple[MongoClient, Collection]: | ||
""" | ||
Returns | ||
------- | ||
db_connection: pymongo.MongoClient | ||
coll: pymongo.collection.Collection | ||
""" | ||
if coll and isinstance(coll, str): | ||
self.coll = coll | ||
return self.db_connection, self.coll | ||
|
||
@property | ||
def db(self): | ||
return self._db | ||
|
||
@db.setter | ||
def db(self, value): # noqa | ||
if isinstance(value, str): | ||
self._db = self.db_connection[value] | ||
elif isinstance(value, Database) or isinstance(value, mongomock.Database): | ||
self._db = value | ||
|
||
@property | ||
def coll(self): | ||
return self._coll | ||
|
||
def api_get_db_connection( | ||
db_uri: str = db_config['MONGO_URI'], | ||
) -> Generator[MongoClient, None, None]: | ||
"""Creates connection to Mongodb server. | ||
Yields | ||
------ | ||
db_connection: MongoClient | ||
Object containing the db connection. | ||
""" | ||
db_connection = MongoClient(db_uri) | ||
|
||
try: | ||
yield db_connection | ||
finally: | ||
db_connection.close() | ||
|
||
|
||
def get_db( | ||
db_uri: str = db_config['MONGO_URI'], | ||
db_name: str = db_config['MONGO_DB'] | ||
) -> Tuple[MongoClient, Database]: | ||
"""Gets Mongodb connection and database. | ||
Parameters | ||
---------- | ||
db_uri: str | ||
Mongo server URI. | ||
db_name: str | ||
Mongo database name. | ||
Returns | ||
------- | ||
db_connection : MongoClient | ||
db : pymongo.database.Database | ||
""" | ||
db_connection = MongoClient(db_uri) | ||
db: Database = db_connection[db_name] | ||
|
||
return db_connection, db | ||
@coll.setter | ||
def coll(self, value): # noqa | ||
if isinstance(value, str): | ||
self._coll = self.db[value] | ||
elif isinstance(value, Collection) or isinstance(value, mongomock.Collection): | ||
self._coll = value |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from datetime import datetime | ||
|
||
from src.db.mongo import MongoClientSingleton | ||
from src.models.db.cmodels import ( | ||
CompartmentalModel, | ||
CompartmentalModelEnum | ||
) | ||
from src.interfaces.crud import MongoCRUD | ||
|
||
|
||
class CModelsInterface: | ||
|
||
def __init__( | ||
self, | ||
mongo_singleton: MongoClientSingleton | ||
) -> None: | ||
self.mongo_singleton = mongo_singleton | ||
self.crud = MongoCRUD(self.mongo_singleton) | ||
|
||
def insert_one_cmodel_document(self, model: CompartmentalModel): | ||
|
||
cmodel_document = model.dict(by_alias=True) | ||
|
||
existent_model = self.crud.read(model.id) | ||
|
||
if existent_model: | ||
pruned_existent_model = CModelsInterface._prune_db_document( | ||
existent_model | ||
) | ||
pruned_current_model = CModelsInterface._prune_db_document( | ||
cmodel_document | ||
) | ||
if pruned_existent_model == pruned_current_model: | ||
# TODO: log cmodel exists f'Cmodel exists: {model.name}' | ||
return existent_model | ||
else: | ||
model.updated_at = datetime.utcnow() | ||
updated_model = self.crud.update( | ||
model.id, | ||
model.dict(by_alias=True) | ||
) | ||
# TODO log updated cmodel f'Updated cmodel: {model.name}' | ||
return updated_model | ||
else: | ||
model_inserted = self.crud.insert(cmodel_document) | ||
# TODO: log created cmodel f'Created cmodel: {model.name}' | ||
return model_inserted | ||
|
||
def insert_all_cmodel_documents(self): | ||
return [ | ||
self.insert_one_cmodel_document(model) | ||
for model in CompartmentalModelEnum.values() | ||
] | ||
|
||
@staticmethod | ||
def _prune_db_document(model_in_db: dict) -> dict: | ||
if model_in_db: | ||
model_in_db.pop('inserted_at') | ||
model_in_db.pop('updated_at') | ||
return model_in_db |
Oops, something went wrong.