-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplanPackages.py
391 lines (302 loc) · 11.1 KB
/
replanPackages.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import concurrent.futures
from datetime import datetime
from utils import files, packages, utils
SUCCESSES = []
ERRORS = []
PACKAGES = []
FORCE_REPLAN = False
ALREADY_ASKED = False
def fill_packages_list(env, orgId, keyType, key):
"""
Fill the PACKAGES list with package details.
Parameters:
- env: The environment.
- orgId: The organization ID.
- keyType: The type of key.
- key: The key.
Returns:
None
"""
add_to_packages_list(packages.get_package_details(env, orgId, keyType, key))
def search_pkg(env: str, orgId: str, key: str, key_type: str, pids: set[str]):
pkgs = packages.search_for_package(env, orgId, key)["packages"]
if len(pkgs) <= 0:
print(f"Package Seeker couldn't find {key}")
return
if len(pkgs) > 1 and key_type == "bc":
raise Exception(
f"Provided key is a barcode, which means it should have returned only 1 package. Please select the appropriate barcode from the objects below and try again:\n{pkgs}"
)
pids.add(pkgs[0]["packageId"])
def search_pkgs(env, orgId, keyType, batch, hubName: str | None = None):
pids = set()
try:
with concurrent.futures.ThreadPoolExecutor() as pool:
for key in batch:
pool.submit(search_pkg, env, orgId, key, keyType, pids)
pool.shutdown(wait=True)
except Exception as e:
print(e)
print(f"Found {len(pids)} packages for batch")
if len(pids) == 0:
print("packageSeeker couldn't find any of the packages from this batch")
return
add_to_packages_list(
packages.bulk_get_package_details(env, orgId, "pi", list(pids))
)
def fill_packages_list_batch(env, orgId, keyType, batch, hubName: str | None):
"""
Fill the PACKAGES list with package details.
Parameters:
- env: The environment.
- orgId: The organization ID.
- keyType: The type of key.
- key: The key.
Returns:
None
"""
add_to_packages_list(packages.bulk_get_package_details(env, orgId, keyType, batch))
def add_to_packages_list(pkgs):
global ALREADY_ASKED, FORCE_REPLAN
print(f"> Found {len(pkgs)} packages")
if len(pkgs) == 0:
return
print(f"{' Applying filters ':=^50}")
if FORCE_REPLAN is False and ALREADY_ASKED is False:
ALREADY_ASKED = True
answer = input(
"> Do you want to force replan all packages? Type 'Y' or leave blank to continue: "
)
if answer.upper() == "Y":
print("Forcing replan for all packages")
FORCE_REPLAN = True
else:
print("We'll ignore any DELIVERED packages")
for pkg in pkgs:
statuses = pkg["packageStatuses"]
# replanning only undelivered packages. Comment this if statement if you need to replan a package marked as delivered in error
if statuses["status"] == "DELIVERED" or (
statuses.get("deliveryFlags")
and (
statuses["deliveryFlags"].get("delivered") is True
and statuses["deliveryFlags"].get("deliveryDay") is not None
)
):
print(
f"> {pkg['packageId']} was already DELIVERED." + " Ignoring"
if not FORCE_REPLAN
else " but will be replanned"
)
if not FORCE_REPLAN:
continue
PACKAGES.append(pkg)
def replan_batch(env, orgId, batch: list[dict], next_delivery_date, hubRequested=None):
"""
Replan packages in batches based on specific criteria.
Parameters:
- env: The environment.
- orgId: The organization ID.
- hubRequested: The requested hub name.
- batch: A list of packages in a batch.
- next_delivery_date: The next delivery date.
Returns:
None
"""
packageIDs = []
for package in batch:
hubName = packages.get_package_hub(package)
# if package is from the correct hub or if we don't want to filter by hub, continues
if hubName == hubRequested or not hubRequested:
prepare_package_for_replan(env, package)
packageIDs.append(package["packageId"])
if packageIDs:
result = packages.bulk_resubmit_packages(
env, orgId, packageIDs, next_delivery_date
)
for success in result["SUCCESS"]:
SUCCESSES.append(success)
for error in result["ERROR"]:
ERRORS.append(error)
else:
print(f">> Packages not from {hubRequested} or packages doesn't exist")
def replan(env, orgId, package, next_delivery_date, hubRequested=None):
"""
Replan a single package based on specific criteria.
Parameters:
- env: The environment.
- orgId: The organization ID.
- hubRequested: The requested hub name.
- package: A dictionary representing the package details.
- next_delivery_date: The next delivery date.
Returns:
None
"""
packageID = package["packageId"]
hubName = packages.get_package_hub(package)
# if package is from the correct hub, continues.
if hubRequested and hubName != hubRequested:
print(
f">> {packageID} Ignored because it isn't from hub {hubRequested} (it is from {hubName})\n"
)
return
prepare_package_for_replan(env, package)
response = packages.resubmit_package(env, orgId, packageID, next_delivery_date)
if response["SUCCESS"]:
SUCCESSES.append(response["SUCCESS"])
if response["ERROR"]:
ERRORS.append(response["ERROR"])
def prepare_package_for_replan(env, package):
status = package["packageStatuses"]["status"]
REVIVE_STATUSES = ["CANCELLED"]
DELIVERY_FAILED_STATUSES = ["REJECTED", "DAMAGED", "DELIVERED"]
# if package is has terminal status,
# revive the package
if status in REVIVE_STATUSES:
packages.revive_package(env, package)
# if package is not being seen by the planner and needs to,
# change status to DELIVERY_FAILED
if status in DELIVERY_FAILED_STATUSES:
packages.mark_package_as_delivery_failed(env, package)
def load_inputs(
env=None,
orgId=None,
hubName=None,
fileName=None,
keyType=None,
keys=None,
next_delivery_date=None,
):
"""
The main function that orchestrates the package resubmission process.
Parameters:
- fileName: The name of the file.
- keyType: The type of key.
- next_delivery_date: The next delivery date.
- env: Optional. The environment. Defaults to None.
- orgId: Optional. The organization ID. Defaults to None.
Returns:
None
"""
if env is None:
env = utils.select_env()
if orgId is None:
orgId = utils.select_org(env)
if fileName is None:
# The file name must be to the requester's hub name (e.g. 8506)
if utils.select_answer(">> Do you have a file with the package keys?") == "Y":
fileName = (
input(">> Type the file name: ")
.strip()
.replace(".txt", "")
.replace("./", "")
.replace(".\\", "")
)
# if user wants to read packages from a file, get them now
keys = files.get_data_from_file(fileName)
if not keys:
return
if keyType is None:
keyType = packages.select_key_type()
if keys is None:
keys = packages.get_list_of_keys(keyType)
if hubName is None:
if utils.select_answer(">> Do you wish filter by hub name?") == "Y":
hubName = input(">> Type the Hub Name: ").strip().upper()
if next_delivery_date is None:
# A date to do the replan must be provided with the yyyy-mm-dd format
# reads a string in a yyyy-mm-dd from user input and creates a datetime object with it
next_delivery_date = datetime.strptime(
input(">> Type the next delivery date: "), "%Y-%m-%d"
)
# converts the datetime object to a string with the yyyy-mm-dd format
next_delivery_date = next_delivery_date.strftime("%Y-%m-%d")
main(
env,
orgId,
keys,
keyType,
next_delivery_date,
hubName,
)
def main(
env: str,
orgId: str,
keys: list[str],
keyType: str,
next_delivery_date: str,
hubName=None,
):
print(f"Key Types: {keyType.upper()}\nKeys: {keys}\n")
load_packages(env, orgId, keyType, keys, hubName)
process_packages(env, orgId, next_delivery_date, hubName)
def load_packages(
env: str,
orgId: str,
keyType: str,
keys: list[str],
hubName: str | None,
usePkgSeeker: bool = False,
):
# Using multithreading to fetch multiple packages simultaneosly
# also split in batches to make less api calls and also accelerate the whole process
# getting packages in Switchboard with keys (ori, bc, etc) provided in the file <fileName>.txt
batches = utils.divide_into_batches(keys)
# with concurrent.futures.ThreadPoolExecutor(8) as pool:
for batch in batches:
if usePkgSeeker:
# pool.submit(search_pkgs, env, orgId, keyType, batch, hubName)
search_pkgs(env, orgId, keyType, batch, hubName)
else:
# pool.submit(fill_packages_list_batch, env, orgId, keyType, batch, hubName)
fill_packages_list_batch(env, orgId, keyType, batch, hubName)
# pool.shutdown(wait=True)
if len(PACKAGES) == 0 and not usePkgSeeker:
print("Trying to search again with Package Seeker + Switchboard")
load_packages(env, orgId, keyType, keys, hubName, usePkgSeeker=True)
# time.sleep(5)
print()
print("===============================")
for pkg in PACKAGES:
if len(PACKAGES) <= 10:
packages.print_package_details(pkg)
print("\n\n")
print("===============================")
print(f">> {len(PACKAGES)} packages loaded out of {len(keys)} keys")
print()
def process_packages(env, orgId, next_delivery_date, hubName=None, packages=None):
if packages is None:
packages = PACKAGES
# If there are a lot of packages to re-submit, we'll split them in batches
# to reduce the amount of API calls by calling the 'bulk resubmit' endpoint
# Using multithreading to replan multiple packages simultaneosly
with concurrent.futures.ThreadPoolExecutor(8) as pool:
if len(packages) > 1:
batches = utils.divide_into_batches(packages)
for batch in batches:
pool.submit(
replan_batch, env, orgId, batch, next_delivery_date, hubName
)
else:
for package in packages:
pool.submit(replan, env, orgId, package, next_delivery_date, hubName)
pool.shutdown(wait=True)
print_results()
def print_results():
print(
"Successful Resubmits ({}/{}): ".format(
len(SUCCESSES), len(SUCCESSES) + len(ERRORS)
)
)
print(SUCCESSES)
# for success in SUCCESSES:
# print(f"> {success}")
print(
"Unsuccessful Resubmits ({}/{}): ".format(
len(ERRORS), len(SUCCESSES) + len(ERRORS)
)
)
print(ERRORS)
# for error in ERRORS:
# print(f"> {error}")
if __name__ == "__main__":
load_inputs()