-
Notifications
You must be signed in to change notification settings - Fork 10
/
ffsc.py
88 lines (72 loc) · 3.44 KB
/
ffsc.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
from multiprocessing import Process, JoinableQueue
from typing import List, Union
from waitlist.storage.database import Shipfit, InvType, FitModule
from waitlist.base import db, manager
if __name__ == '___main__':
manager.run()
class ConvertConsumer(Process):
def __init__(self, tasks, name):
Process.__init__(self)
self.__tasks = tasks
self.name = name
def run(self):
while True:
work = self.__tasks.get()
if work is None:
break
ConvertConsumer.convert(work[0], work[1])
@staticmethod
def convert(offset, limit):
fits = db.session.query(Shipfit).limit(limit).offset(offset).all()
for fit in fits:
if fit.modules is not None and fit.modules != '':
filtered_modules_string = ''
for moduleDefStr in fit.modules.split(':'):
if moduleDefStr == '':
continue
try:
module_def_arr: List[Union[str, int], Union[str, int]] = moduleDefStr.split(';')
if len(module_def_arr) != 2:
print("Skipping Module Fit ID=", fit.id, " Module Def Str:", moduleDefStr)
continue
# lets check here if that module exists
module_def_arr[0] = int(module_def_arr[0])
module_def_arr[1] = int(module_def_arr[1])
if module_def_arr[1] > 2147483647 or module_def_arr[1] < 0:
module_def_arr[1] = 2147483647
module = db.session.query(InvType).get(module_def_arr[0])
if module is None:
print("No Module with ID=", str(module_def_arr[0]))
continue
db_module = FitModule(moduleID=module_def_arr[0], amount=module_def_arr[1])
fit.moduleslist.append(db_module)
filtered_modules_string += str(module_def_arr[0])+';'+str(module_def_arr[1])+':'
except ValueError as e:
print("Fit ID=", str(fit.id), " Module Def Str:", moduleDefStr)
raise e
except IndexError as ie:
print("Fit ID=", str(fit.id), " Module Def Str:", moduleDefStr)
raise ie
if filtered_modules_string == '' or filtered_modules_string == '::':
filtered_modules_string = ':'
else:
filtered_modules_string += ':'
if fit.modules != filtered_modules_string:
print("Correcting: ", fit.modules, " -> ", filtered_modules_string)
fit.modules = filtered_modules_string
db.session.commit()
db.session.close()
if __name__ == '__main__':
consumer_threads = []
task_queue = JoinableQueue(maxsize=100)
for i in range(6):
t = ConvertConsumer(task_queue, "dec"+str(i))
t.start()
consumer_threads.append(t)
numberOfFits = db.session.query(Shipfit).count()
stepSize = 1000
for s in range(0, numberOfFits, stepSize):
task_queue.put((s, stepSize))
# send them all the signal to break
for _ in range(6):
task_queue.put(None)