File size: 8,298 Bytes
478fd8c
 
f8bee93
478fd8c
 
 
 
 
 
 
 
 
ec04543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478fd8c
 
 
 
 
 
 
 
 
 
349ff65
 
478fd8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8bee93
 
478fd8c
f8bee93
cbc3ea4
 
f8bee93
 
 
 
 
 
 
 
cbc3ea4
f8bee93
 
 
 
 
 
 
 
ec04543
 
 
 
cbc3ea4
ec04543
 
 
 
cbc3ea4
ec04543
 
cbc3ea4
478fd8c
 
 
 
 
 
 
 
 
 
ec04543
 
 
 
 
 
 
478fd8c
 
f8bee93
 
 
 
478fd8c
f8bee93
 
 
 
 
 
 
 
 
 
cbc3ea4
f8bee93
 
 
 
 
 
 
 
 
 
 
cbc3ea4
f8bee93
 
 
 
 
 
478fd8c
cbc3ea4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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