-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
164 lines (125 loc) · 6.36 KB
/
main.py
File metadata and controls
164 lines (125 loc) · 6.36 KB
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
""" Main Merging Function
This Python script controls the four processes. Follow instructions in terminal to:
1. Create a duplicate detection job
2. Group and download the duplicates
3. Prepare the duplicates for merging
4. Execute the merge requests
You should update the logic in prepare_duplicates.py to meet your
needs before running this end to end and create a .env file.
"""
from core.find_duplicates import create_duplicate_detection_job
from core.group_duplicates import download_and_group_duplicates
from core.prepare_duplicate import prepare_duplicates
from core.update_duplicates import update_duplicates
from core.utils.session_manager import SessionManager
from core.utils.dynamics_config import get_config
def main():
"""Provides a menu of options and walks through the process"""
# Create session once at startup
print("=== Dynamics 365 Duplicate Resolver ===\n")
dotenv_name = input("Specify .env file to authenticate: ").strip()
if dotenv_name is None:
config = get_config()
else:
config = get_config(dotenv_name)
manager = SessionManager(config)
manager.get_authenticated_session()
if not manager.validate_session():
print("Authentication failed!")
return
print("\n✓ Authentication successful!\n")
entity = None
data_folder = None
asyncoperationid = None
target_is = None
duplicate_groups = None
perform_parenting_check = None
duplicates = None
max_number_of_dupes = None
while True:
print("\n=== Dynamics 365 Duplicate Resolver ===")
print("1. Create duplicate detection job")
print("2. Group duplicates")
print("3. Prepare duplicates")
print("4. Merge duplicates")
print("5. Clear all and start again")
print("0. Exit")
choice = input("\nChoose an option: ").strip()
if choice == "1":
entity = input("Entity name: ").strip()
view = input("View name: ").strip()
print("\nCreating Dynamics Duplicate Detection Job\n")
asyncoperationid = create_duplicate_detection_job(entity, view, manager)
print(f"\n✓ Success! Job ID: {asyncoperationid}\n")
print("Once complete, run 2: Group duplicates")
elif choice == "2":
entity = entity if entity else input("Entity name: ").strip()
data_folder = data_folder if data_folder else input("Folder name to store data in: ").strip()
max_number_of_dupes = max_number_of_dupes if max_number_of_dupes else input("Optional number of duplicates to import: ").strip()
asyncoperationid = asyncoperationid if asyncoperationid else input("Duplicate Detection Job ID: ").strip()
try:
max_number_of_dupes = int(max_number_of_dupes) if max_number_of_dupes else None
except ValueError:
max_number_of_dupes = None
max_number_str = "all duplicate" if max_number_of_dupes is None else max_number_of_dupes
print(f"\nDownloading and grouping {max_number_str} {entity}s to store in the {data_folder}/group folder.\n")
duplicate_groups = download_and_group_duplicates(
entity = entity,
asyncoperationid = asyncoperationid,
data_folder = data_folder,
manager = manager,
max_number_of_dupes = max_number_of_dupes
)
number_of_groups = len(duplicate_groups.keys())
number_of_duplicates = sum(len(v) for v in duplicate_groups.values())
print(f"\n✓ Success! {number_of_duplicates} Duplicates downloaded into {number_of_groups} groups\n")
elif choice == "3":
entity = entity if entity else input("Entity name: ").strip()
data_folder = data_folder if data_folder else input("Folder name to store data in: ").strip()
target_is = target_is if target_is else input("Should the target be the oldest record or newest record? (oldest_record or newest_record):").strip()
max_number_str = "all duplicate" if max_number_of_dupes is None else max_number_of_dupes
print(f"\nPreparing and grouping {max_number_str} {entity}s by the {target_is} to store in the {data_folder}/prepare folder.\n")
duplicates = prepare_duplicates(
entity = entity,
target_is = target_is,
manager = manager,
data_folder = data_folder,
duplicate_groups = duplicate_groups)
elif choice == "4":
entity = entity if entity else input("Entity name: ").strip()
data_folder = data_folder if data_folder else input("Folder name to store data in: ").strip()
if perform_parenting_check is None:
perform_parenting_check = input("Should a parent check be performed? (True or False):").strip().lower() == "true"
max_number_of_dupes = input("Optionally maximum number of duplicates to merge (Number or Enter): ").strip()
try:
max_number_of_dupes = int(max_number_of_dupes) if max_number_of_dupes else None
except ValueError:
max_number_of_dupes = None
max_number_str = "all duplicate" if max_number_of_dupes == -1 else max_number_of_dupes
parent_check_str = "with a parenting check " if perform_parenting_check else ""
print(f"\nExecuting merge requests on {max_number_str} {entity}s {parent_check_str}and storing result in the {data_folder}/update folder.\n")
update_duplicates(
entity = entity,
data_folder = data_folder,
manager = manager,
dataframe = duplicates,
perform_parenting_check = perform_parenting_check,
max_number_to_execute = max_number_of_dupes)
elif choice == "5":
entity = None
data_folder = None
asyncoperationid = None
target_is = None
duplicate_groups = None
perform_parenting_check = None
duplicates = None
max_number_of_dupes = None
print("\nAll Varaiables cleared. Ready to start again.\n")
elif choice == "0":
manager.close_session()
print("Goodbye!")
break
else:
print("Invalid choice!")
if __name__ == "__main__":
main()