diff --git a/README.md b/README.md index 31f2c20..e81ae82 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,26 @@ Prerequisites: 1. Make sure that you have added the `.env` file in the project root 2. Run `docker-compose -f docker-compose-app.yml up` + +## Environment variables + +The following variables should be set in the .env file + +```shell +# These variable are of our mongo which will be used for logging recommendations +# Should not be confused with the RS Mongo +INTERNAL_MONGO_HOST=mongo # The host of the internal mongo deployed by compose +INTERNAL_MONGO_PORT=27017 # The port of the internal mongo deployed by compose +INTERNAL_MONGO_DATABASE=recommender # The name of the database we are using for internal storage +INTERNAL_MONGO_USERNAME="admin" # The username of a user of the internal mongo deployed by compose +INTERNAL_MONGO_PASSWORD="admin" # The password of a user of the internal mongo deployed by compose +# Currently we need the uri too, in a future version we will create it from the above variables +INTERNAL_MONGO_URI="mongodb://admin:admin@mongo:27017" + +# RS Mongo +RS_MONGO_URI="mongodb://admin:admin@rs_mongo:27017" +RS_MONGO_DB=recommender # The name of the database used in the RS mongo + +# The private sdn key for sentry which we use for error logging +SENTRY_SDN=https://12345... +``` diff --git a/api/databases/mongo.py b/api/databases/mongo.py index 64ef3d1..d090de3 100644 --- a/api/databases/mongo.py +++ b/api/databases/mongo.py @@ -19,7 +19,7 @@ def get_db(self): def connect(self): try: - logger.info('Connecting to the Mongo database...') + logger.debug('Connecting to the Mongo database...') self._conn = MongoClient(self._uri) self._db = self._conn[self._db_name] @@ -31,22 +31,21 @@ class RSMongoDB: def __init__(self): self.mongo_connector = MongoDbConnector(APP_SETTINGS["CREDENTIALS"]['RS_MONGO_URI'], APP_SETTINGS["CREDENTIALS"]['RS_MONGO_DB']) + self.mongo_connector.connect() + # TODO get only attributes? def get_services(self, attributes=None, conditions=None): if conditions is None: conditions = {} if attributes is None: attributes = [] - self.mongo_connector.connect() services = pd.DataFrame(list(self.mongo_connector.get_db()["service"].find(conditions))) - services.rename(columns={'_id': 'service_id'}, inplace=True) return services[["service_id"] + attributes] def get_scientific_domains(self): - self.mongo_connector.connect() return [domain["_id"] for domain in self.mongo_connector.get_db()["scientific_domain"].find({}, {"_id": 1})] def get_categories(self): @@ -55,6 +54,7 @@ def get_categories(self): def get_target_users(self): return [domain["_id"] for domain in self.mongo_connector.get_db()["target_user"].find({}, {"_id": 1})] + # TODO change it when i can get the info from recommender db def get_user_services(self, user_id): return [] @@ -63,5 +63,5 @@ class InternalMongoDB: def __init__(self): self.mongo_connector = MongoDbConnector(APP_SETTINGS["CREDENTIALS"]['INTERNAL_MONGO_URI'], APP_SETTINGS["CREDENTIALS"]['INTERNAL_MONGO_DATABASE']) - + self.mongo_connector.connect() # TODO: This is were functions like storing logging will be implemented diff --git a/api/main.py b/api/main.py index 7dd993f..eeb423d 100644 --- a/api/main.py +++ b/api/main.py @@ -14,6 +14,8 @@ dsn=APP_SETTINGS['CREDENTIALS']['SENTRY_SDN'], traces_sample_rate=1.0 ) +if not APP_SETTINGS['BACKEND']['PROD']: + sentry_sdk.init() # Disable sentry if we are not in a dev environment app = FastAPI() diff --git a/api/recommender/similar_services/recommendation_generation.py b/api/recommender/similar_services/recommendation_generation.py index 96df16f..1183996 100644 --- a/api/recommender/similar_services/recommendation_generation.py +++ b/api/recommender/similar_services/recommendation_generation.py @@ -28,6 +28,25 @@ def create_recommendation(viewed_resource_id, recommendations_num=5, user_id=Non candidates = ordering(candidates) + # TODO: implement re_ranking(candidates) return candidates[:recommendations_num].index.tolist() + + +# def test_recommendation(viewed_resource_id, purchases, recommendations_num=5, viewed_weight=0.5, metadata_weight=0.5): +# db = PostgresDb(APP_SETTINGS["CREDENTIALS"]) +# db.connect() +# +# candidates = get_recommendation_candidates(viewed_resource_id, purchased_resources=purchases, +# view_weight=viewed_weight, metadata_weight=metadata_weight) +# +# candidates = filtering(db, candidates, viewed_resource_id, purchases) +# +# db.close_connection() +# +# candidates = ordering(candidates) +# +# re_ranking(candidates) +# +# return candidates[:recommendations_num].index.tolist() diff --git a/api/settings.py b/api/settings.py index 776ffb0..8763ce4 100644 --- a/api/settings.py +++ b/api/settings.py @@ -14,6 +14,12 @@ def read_settings(): with open(args.config_file) as file: backend_settings = yaml.load(file, Loader=yaml.FullLoader) + # We need to know if we are running in prod or dev env + if 'prod' in args.config_file: + backend_settings['PROD'] = True + else: + backend_settings['PROD'] = False + credentials = dotenv_values(".env") return {