macros_evolution_space / find_matches.py
Maximofn's picture
feat(SRC): :rocket: Add all logic
478fd8c
raw
history blame
3.51 kB
from pathlib import Path
import json
from queries import query_formularios, query_usuarios, get_macros_differences
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 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=False, 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):
# Create a list to store the macros differences
macros_differences_list = []
# 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
user_data = json.load(open(user_data, 'r'))
# Query usuarios
dates_that_match = query_usuarios(user_data, dates_list_from_user, debug=False, limit_days=31)
if len(dates_that_match) > 0:
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)
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")
return macros_differences_list