| Current Path : /home/deltalab/PMS/recommendations/user_profiling/_library/ |
| Current File : //home/deltalab/PMS/recommendations/user_profiling/_library/io_toolkit.py |
from os import path
from datetime import datetime
import numpy as np
import json
from dotenv import dotenv_values, find_dotenv
from sys import exit
from collections import defaultdict
def read_settings(file_name = 'settings.json'):
folder_path = ""
file_path = path.join(folder_path, file_name)
with open(file_path) as setting_file:
settings = json.load(setting_file)
return settings
def read_indaco_credentials(db_type):
# Read the credentials stored in the ENV file
# STRUCTURE --> KEY = VALUE
# LOCATION: Local machine (not uploaded on the repository)
file_path = find_dotenv()
# Read the file
if file_path != '':
config_file = dotenv_values(file_path)
k_hostname ="INDACO_HOSTNAME"
k_port = "INDACO_PORT"
k_user = "INDACO_USERNAME"
k_password = "INDACO_PASSWORD"
k_dbname = "INDACO_DB_NAME"
if(db_type == "etl"):
k_hostname ="ETL_HOSTNAME"
k_port = "ETL_PORT"
k_user = "ETL_USERNAME"
k_password = "ETL_PASSWORD"
k_dbname = "ETL_DB_NAME"
# PARM 1: Hostname
if k_hostname in config_file.keys():
indaco_hostname = config_file[k_hostname]
else:
indaco_hostname = None
print('ERROR [PARAM 1]: the HOSTNAME parameter has not been found!')
# PARAM 2: PORT
if k_port in config_file.keys():
indaco_port = int(config_file[k_port])
else:
indaco_port = None
print('ERROR [PARAM 2]: the PORT parameter has not been found!')
if k_user in config_file.keys():
indaco_username = config_file[k_user]
else:
indaco_username = None
print('ERROR [PARAM 3]: the DATABASE USERNAME parameter has not been found!')
if k_password in config_file.keys():
indaco_password = config_file[k_password]
else:
indaco_password = None
print('ERROR [PARAM 4]: the DATABASE PASSWORD parameter has not been found!')
if k_dbname in config_file.keys():
db_name = config_file[k_dbname]
else:
db_name = None
print('ERROR [PARAM 5]: the DATABASE NAME parameter has not been found!')
else:
print(f'ERROR: The file ".env" has not been found! It must be provided!')
indaco_hostname, indaco_port, indaco_username, indaco_password, db_name = None, None, None, None, None
exit(1)
return indaco_hostname, indaco_port, indaco_username, indaco_password, db_name
def load_collectionTypes(filepath, verbose = True):
rawCollectionTypes = dict()
with open(filepath) as json_file:
rawCollectionTypes = json.load(json_file)
# Keep and extract the collection names
collectionTypes = defaultdict(list)
for colType, collections in rawCollectionTypes.items():
if verbose:
print("\n" + "-" * 48)
print("-" * 15, colType.upper(), "-" * 15)
print("-" * 48)
for collection_name in collections.values():
collectionTypes[colType].append(collection_name)
if verbose:
print('-->', collection_name)
if verbose:
print("-" * 48)
print("-" * 48 + "\n")
return collectionTypes, rawCollectionTypes
def saveComputationalTimes(computationalTime_byUser):
computationalTime_byUser = dict(sorted(computationalTime_byUser.items(),
key = lambda dict_item: dict_item[0]))
computational_times = list(computationalTime_byUser.values())
# Cast temporal unitn
temporal_unit = lambda total_seconds: 'ms' if total_seconds < 1 else 's' if total_seconds < 60 else 'm'
cast_temporal_unit = lambda computational_time: np.timedelta64(computational_time, temporal_unit(np.timedelta64(computational_time, 's')))
# Total
totalTime = cast_temporal_unit(np.sum(computational_times))
print('\n[TOTAL] Computational time:', totalTime)
# Min, Max, Avg
minTime = cast_temporal_unit(np.min(computational_times))
print('[MIN per user] Computational time:', minTime,
f"(user: {list(computationalTime_byUser.keys())[np.argmin(computational_times)]})")
avgTime = cast_temporal_unit(np.mean(computational_times))
print('[AVG per user] Computational time:', avgTime)
maxTime = cast_temporal_unit(np.max(computational_times))
print('[MAX per user] Computational time:', maxTime,
f"(user: {list(computationalTime_byUser.keys())[np.argmax(computational_times)]}) \n\n")
with open('computationalTime_byUser.txt', 'w') as file:
dictToSave = {
'date': datetime.now().strftime("%Y-%m-%d %H:%M"),
'TotalTime': str(totalTime),
'maxTime': str(maxTime),
'avgTime': str(avgTime),
'minTime': str(minTime)}
# To string
dictToString = json.dumps(dictToSave) # + "\n"
dictToString += json.dumps({f'user{att}': str(value) for att, value in computationalTime_byUser.items()}, indent = 4)
file.write(dictToString)