macros_evolution_space / find_matches.py
Maximofn's picture
fix(SRC): :rocket: Return list and dict to after call to the function calculate stadistical data from list
cbc3ea4
from pathlib import Path
import json
from queries import query_formularios, query_usuarios, get_macros_differences, get_days_between_dates
formularios_weight_difference_path = 'anonymized_formularios_weight_difference'
usuarios_macros_difference_path = 'usuarios_macros_difference'
# Find users dates that match the query
def find_user_dates_matches(query, debug=False):
# Create a dictionary to store the matches
matches_dict = {}
# Get the query values
query_cumplimiento_entrenamiento = None
query_cumplimiento_dieta = None
query_compromiso = None
query_esfuerzo_dieta = None
query_objetivo = None
for query_item in query:
query_item_key = list(query_item.keys())[0]
query_item_value = query_item[query_item_key]['value']
if query_item_key == 'cumplimientoEntrenamiento':
query_cumplimiento_entrenamiento = query_item_value
elif query_item_key == 'cumplimientoDieta':
query_cumplimiento_dieta = query_item_value
elif query_item_key == 'compromiso':
query_compromiso = query_item_value
elif query_item_key == 'esfuerzoParaCumplirDieta':
query_esfuerzo_dieta = query_item_value
elif query_item_key == 'objetivo':
query_objetivo = query_item_value
# If cumplimiento entrenamiento or cumplimiento dieta is mal, then return empty dict
if query_cumplimiento_entrenamiento == 'mal' or query_cumplimiento_dieta == 'mal':
print("[find_user_dates_matches] Cumplimiento entrenamiento or cumplimiento dieta is \"mal\"")
return matches_dict
# If query_esfuerzo_dieta is costo subir macros, then return subir macros dict
if query_esfuerzo_dieta == 'costo subir macros':
print("[find_user_dates_matches] query_esfuerzo_dieta is \"costo subir macros\"")
subir_macros_dict = {}
subir_macros_dict['costo subir macros'] = []
return subir_macros_dict
# If query_esfuerzo_dieta is costo bajar macros, then return bajar macros dict
elif query_esfuerzo_dieta == 'costo bajar macros':
print("[find_user_dates_matches] query_esfuerzo_dieta is \"costo bajar macros\"")
bajar_macros_dict = {}
bajar_macros_dict['costo bajar macros'] = []
return bajar_macros_dict
# Get all the files in the formularios_path
files = Path(formularios_weight_difference_path).glob('*.json')
files = list(files)
files.sort()
# Iterate over the user data
for i, file in enumerate(files):
with open(file, 'r') as f:
data = json.load(f)
if file.name == '[email protected]':
dates = query_formularios(data, query, debug=True, file_name=file.name)
else:
dates = query_formularios(data, query, debug=debug)
if len(dates) > 0:
file_name = file.name
matches_dict[file_name] = dates
if debug:
if i > 0:
print("")
print(f"{file_name} has {len(dates)} dates that match the query:")
for date in dates:
print(f"\t{date}")
for query_item in query:
key = list(query_item.keys())[0]
data_value = data[date][key]
query_value = query_item[key]['value']
operator = query_item[key]['operator']
if type(data_value) == int or type(data_value) == float:
data_value = f"{data_value:.2f}"
if operator == 'in' or operator == 'contains':
print(f"\t\t{key} data: \"{data_value}\", query: \"{query_value}\"")
else:
print(f"\t\t{key} data: \"{data_value}\" \"{operator}\" query: \"{query_value}\"")
return matches_dict
def find_macros_that_match_dates_of_users(matches_dict, debug=False):
limit_days = 11
# Create a list to store the macros differences
macros_differences_dict = {}
macros_differences_list = []
macros_differences_list_subir_macros = ["0 20 0 0 0 0 20 0"]
macros_differences_dict_subir_macros = {
"costo subir macros": {
"date_of_match": "1990-01-01",
"date_of_macros_asignation": "1990-01-02",
"days_between_match_and_macros_asignation": 1,
"macros_difference": "0 20 0 0 0 0 20 0"
}
}
macros_differences_list_bajar_macros = ["0 -20 0 0 0 0 -20 0"]
macros_differences_dict_bajar_macros = {
"costo bajar macros": {
"date_of_match": "1990-01-01",
"date_of_macros_asignation": "1990-01-02",
"days_between_match_and_macros_asignation": 1,
"macros_difference": "0 -20 0 0 0 0 -20 0"
}
}
# If matches_dict is empty, then return empty list
if len(matches_dict) == 0:
print("[find_macros_that_match_dates_of_users] matches_dict is empty")
return macros_differences_list, macros_differences_dict
# If matches_dict is costo subir macros, then return empty list
if list(matches_dict.keys())[0] == 'costo subir macros':
print("[find_macros_that_match_dates_of_users] matches_dict is \"costo subir macros\"")
return macros_differences_list_subir_macros, macros_differences_dict_subir_macros
elif list(matches_dict.keys())[0] == 'costo bajar macros':
print("[find_macros_that_match_dates_of_users] matches_dict is \"costo bajar macros\"")
return macros_differences_list_bajar_macros, macros_differences_dict_bajar_macros
# Iterate over the matches dictionary
for match_user in matches_dict:
if debug: print(f"match_user: {match_user}")
# Get dates list
dates_list_from_user = matches_dict[match_user]
# Get user data
user_data = usuarios_macros_difference_path + '/' + match_user
# Check if user data exists
if not Path(user_data).exists():
if debug: print(f"User data not found: {user_data}")
continue
# Load user data
user_data = json.load(open(user_data, 'r'))
# Get dates that match and macros differences dates
dates_that_match, macros_differences_dates = query_usuarios(user_data, dates_list_from_user, debug=False, limit_days=limit_days)
# If there are dates that match
if len(dates_that_match) > 0:
# Iterate over the dates that match
for i, date_of_match in enumerate(dates_that_match):
if debug: print(f"\tdates that match: {dates_that_match}")
# Get macros differences
macros_differences = get_macros_differences(user_data, dates_that_match)
if type(macros_differences) == list:
if len(macros_differences) > 0:
for macros_difference in macros_differences:
macros_differences_list.append(macros_difference)
days_between = get_days_between_dates(macros_differences_dates[i], date_of_match)
match_dict = {
"date_of_match": date_of_match,
"date_of_macros_asignation": macros_differences_dates[i],
"days_between_match_and_macros_asignation": days_between,
"macros_difference": macros_difference
}
macros_differences_dict[match_user] = match_dict
if debug: print(f"\tmacros_differences: {macros_difference}")
if debug: print("")
else:
macros_differences_list.append(macros_differences)
if debug: print(f"\tmacros_differences: {macros_differences}\n")
match_dict = {
"date_of_macros_asignation": dates_that_match[0],
"macros_difference": macros_differences
}
macros_differences_dict[match_user] = match_dict
return macros_differences_list, macros_differences_dict