Added logging to persistence

This commit is contained in:
Peter Yefi 2022-12-14 17:49:29 -05:00
parent bf66069e3a
commit 2d301853d6
7 changed files with 40 additions and 22 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
/data/energy_systems/heat_pumps/*.insel
.DS_Store
.env
logs

6
hub_logger/__init__.py Normal file
View File

@ -0,0 +1,6 @@
import logging as logger
from pathlib import Path
log_file = (Path(__file__).parent.parent / 'logs/hub.log').resolve()
logger.basicConfig(filename=log_file, format="%(asctime)s:%(levelname)s:{%(pathname)s:%(funcName)s:%(lineno)d} "
"- %(message)s", level=logger.DEBUG)

View File

@ -8,6 +8,7 @@ Project Coder Peter Yefi peteryefi@gmail.com
import os
from dotenv import load_dotenv
from sqlalchemy.ext.declarative import declarative_base
from hub_logger import logger
Base = declarative_base()
@ -33,7 +34,7 @@ class BaseConfiguration(object):
self._db_port = os.getenv(f'{app_env}_DB_PORT')
self.hub_token = os.getenv('HUB_TOKEN')
except KeyError as err:
print(f'Error with credentials: {err}')
logger.error(f'Error with credentials: {err}')
def conn_string(self):
"""

View File

@ -4,6 +4,7 @@ from persistence.models import HeatPumpSimulation
from persistence.models import User
from persistence.repositories import UserRepo
from persistence.models import UserRoles
from hub_logger import logger
class DBSetup:
@ -26,6 +27,11 @@ class DBSetup:
email = 'admin@hub.com'
password = 'HubAdmin#!98'
print('Creating default admin user...')
user_repo.insert('Administrator', email, password, UserRoles.Admin)
print(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin}')
print('Remember to change the admin default password and email address with the UserFactory')
user = user_repo.insert('Administrator', email, password, UserRoles.Admin)
if type(user) is dict:
print(user)
logger.info(user)
else:
print(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin}')
logger.info(f'Created Admin user with email: {email}, password: {password} and role: {UserRoles.Admin}')
print('Remember to change the admin default password and email address with the UserFactory')

View File

@ -14,13 +14,14 @@ import pickle
import requests
from urllib3.exceptions import HTTPError
from typing import Union, Dict
from hub_logger import logger
class CityRepo(BaseRepo):
_instance = None
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
super().__init__(db_name, dotenv_path, app_env)
super().__init__(db_name, dotenv_path, app_env)
def __new__(cls, db_name, dotenv_path, app_env):
"""
@ -43,6 +44,7 @@ class CityRepo(BaseRepo):
response = requests.get("https://rs-loy-gitlab.concordia.ca/api/v4/projects/2/repository/branches/master",
headers={"PRIVATE-TOKEN": self.config.hub_token})
recent_commit = response.json()["commit"]["id"]
logger.info(f'Current commit of hub is {recent_commit}')
exiting_city = self._get_by_hub_version(recent_commit, city.name)
# Do not persist the same city for the same version of Hub
@ -64,9 +66,9 @@ class CityRepo(BaseRepo):
else:
return {'message': f'Same version of {city.name} exist'}
except SQLAlchemyError as err:
print(f'Error while adding city: {err}')
logger.error(f'Error while adding city: {err}')
except HTTPError as err:
print(f'Error retrieving Hub latest release: {err}')
logger.error(f'Error retrieving Hub latest release: {err}')
def get_by_id(self, city_id: int) -> DBCity:
"""
@ -77,7 +79,7 @@ class CityRepo(BaseRepo):
try:
return self.session.execute(select(DBCity).where(DBCity.id == city_id)).first()[0]
except SQLAlchemyError as err:
print(f'Error while fetching city: {err}')
logger.error(f'Error while fetching city: {err}')
def _get_by_hub_version(self, hub_commit: str, city_name: str) -> City:
"""
@ -90,7 +92,7 @@ class CityRepo(BaseRepo):
return self.session.execute(select(DBCity)
.where(DBCity.hub_release == hub_commit, DBCity.name == city_name)).first()
except SQLAlchemyError as err:
print(f'Error while fetching city: {err}')
logger.error(f'Error while fetching city: {err}')
def update(self, city_id: int, city: City):
"""
@ -109,7 +111,7 @@ class CityRepo(BaseRepo):
self.session.commit()
except SQLAlchemyError as err:
print(f'Error while updating city: {err}')
logger.error(f'Error while updating city: {err}')
def get_by_name(self, city_name: str) -> [DBCity]:
"""
@ -121,7 +123,7 @@ class CityRepo(BaseRepo):
result_set = self.session.execute(select(DBCity).where(DBCity.name == city_name))
return [building[0] for building in result_set]
except SQLAlchemyError as err:
print(f'Error while fetching city by name: {err}')
logger.error(f'Error while fetching city by name: {err}')
def delete_city(self, city_id: int):
"""
@ -133,4 +135,4 @@ class CityRepo(BaseRepo):
self.session.query(DBCity).filter(DBCity.id == city_id).delete()
self.session.commit()
except SQLAlchemyError as err:
print(f'Error while fetching city: {err}')
logger.error(f'Error while fetching city: {err}')

View File

@ -10,6 +10,7 @@ from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import select
from persistence.models import HeatPumpSimulation
from typing import Union, Dict
from hub_logger import logger
class HeatPumpSimulationRepo(BaseRepo):
@ -66,9 +67,9 @@ class HeatPumpSimulationRepo(BaseRepo):
self.session.commit()
return hp_simulation
except SQLAlchemyError as err:
print(f'Error while saving heat pump simulation data: {err}')
logger.error(f'Error while saving heat pump simulation data: {err}')
except KeyError as err:
print(f'A required field is missing in your heat pump simulation dictionary: {err}')
logger.error(f'A required field is missing in your heat pump simulation dictionary: {err}')
def get_by_id(self, hp_simulation_id: int) -> HeatPumpSimulation:
"""
@ -80,7 +81,7 @@ class HeatPumpSimulationRepo(BaseRepo):
return self.session.execute(select(HeatPumpSimulation).where(HeatPumpSimulation.id == hp_simulation_id)).first()[
0]
except SQLAlchemyError as err:
print(f'Error while fetching city: {err}')
logger.error(f'Error while fetching city: {err}')
def get_by_city(self, city_id: int) -> [HeatPumpSimulation]:
"""
@ -92,7 +93,7 @@ class HeatPumpSimulationRepo(BaseRepo):
result_set = self.session.execute(select(HeatPumpSimulation).where(HeatPumpSimulation.city_id == city_id))
return [sim_data[0] for sim_data in result_set]
except SQLAlchemyError as err:
print(f'Error while fetching city by name: {err}')
logger.error(f'Error while fetching city by name: {err}')
def delete_hp_simulation(self, hp_simulation_id: int):
"""
@ -104,4 +105,4 @@ class HeatPumpSimulationRepo(BaseRepo):
self.session.query(HeatPumpSimulation).filter(HeatPumpSimulation.id == hp_simulation_id).delete()
self.session.commit()
except SQLAlchemyError as err:
print(f'Error while fetching city: {err}')
logger.error(f'Error while fetching city: {err}')

View File

@ -12,6 +12,7 @@ from persistence.models import User
from persistence.models import UserRoles
from helpers.auth import Auth
from typing import Union, Dict
from hub_logger import logger
class UserRepo(BaseRepo):
@ -39,7 +40,7 @@ class UserRepo(BaseRepo):
self.session.commit()
return user
except SQLAlchemyError as err:
print(f'An error occured while creating user: {err}')
logger.error(f'An error occured while creating user: {err}')
else:
return {'message': f'user with {email} email already exists'}
@ -59,7 +60,7 @@ class UserRepo(BaseRepo):
.update({'name': name, 'email': email, 'password': Auth.hash_password(password), 'role': role})
self.session.commit()
except SQLAlchemyError as err:
print(f'Error while updating user: {err}')
logger.error(f'Error while updating user: {err}')
def get_by_email(self, email: str) -> [User]:
"""
@ -70,7 +71,7 @@ class UserRepo(BaseRepo):
try:
return self.session.execute(select(User).where(User.email == email)).first()
except SQLAlchemyError as err:
print(f'Error while fetching user by email: {err}')
logger.error(f'Error while fetching user by email: {err}')
def delete_user(self, user_id: int):
"""
@ -82,7 +83,7 @@ class UserRepo(BaseRepo):
self.session.query(User).filter(User.id == user_id).delete()
self.session.commit()
except SQLAlchemyError as err:
print(f'Error while fetching user: {err}')
logger.error(f'Error while fetching user: {err}')
def get_user_by_email_and_password(self, email: str, password: str) -> [User]:
"""
@ -98,4 +99,4 @@ class UserRepo(BaseRepo):
return user
return {'message': 'user not found'}
except SQLAlchemyError as err:
print(f'Error while fetching user by email: {err}')
logger.error(f'Error while fetching user by email: {err}')