Performance optimization: Add parallel API calls and caching for faster stock queries
Browse files
app.py
CHANGED
@@ -75,6 +75,22 @@ def fetch_warehouse_inventory(warehouse, product_name, search_terms):
|
|
75 |
|
76 |
products = inventory_data['data']['inventory']
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
# Ürünü ara
|
79 |
warehouse_variants = []
|
80 |
dsw_stock_count = 0
|
@@ -83,42 +99,96 @@ def fetch_warehouse_inventory(warehouse, product_name, search_terms):
|
|
83 |
product_title = normalize_turkish(product.get('title', '')).lower()
|
84 |
original_title = product.get('title', '')
|
85 |
|
86 |
-
#
|
87 |
-
if
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
if is_dsw:
|
95 |
-
dsw_stock_count += actual_stock
|
96 |
-
continue
|
97 |
-
|
98 |
-
# Normal mağaza stoğu - varyant detayını sakla
|
99 |
-
variant_info = original_title
|
100 |
-
|
101 |
-
# Ürün adını temizle
|
102 |
-
possible_names = [
|
103 |
-
product_name.upper(),
|
104 |
-
product_name.lower(),
|
105 |
-
product_name.title(),
|
106 |
-
product_name.upper().replace('I', 'İ'),
|
107 |
-
product_name.upper().replace('İ', 'I'),
|
108 |
-
]
|
109 |
-
|
110 |
-
if 'fx sport' in product_name.lower():
|
111 |
-
possible_names.extend(['FX Sport AL 3', 'FX SPORT AL 3', 'Fx Sport Al 3'])
|
112 |
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
|
|
|
117 |
|
118 |
-
if
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
# Sonuç döndür
|
124 |
if warehouse_variants and not is_dsw:
|
@@ -670,15 +740,30 @@ def chatbot_fn(user_message, history, image=None):
|
|
670 |
skip_words = ['stok', 'stock', 'kaç', 'adet', 'tane', 'var', 'mı', 'mi',
|
671 |
'mevcut', 'mu', 'bulunuyor', 'hangi', 'mağaza', 'nerede',
|
672 |
'durumu', 'stoklarda', 'stokta', 'için', 've', 'ile', 'toplam', 'toplamda',
|
673 |
-
'fiyat', 'fiyatı', 'ne', 'nedir', 'kadar']
|
|
|
|
|
|
|
|
|
674 |
|
675 |
for word in user_message.lower().split():
|
|
|
676 |
if word not in skip_words:
|
677 |
product_words.append(word)
|
678 |
|
679 |
if product_words:
|
680 |
product_name = ' '.join(product_words)
|
681 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
682 |
# Kısa isimlerle arama yapıldıysa birden fazla ürün olabilir
|
683 |
all_stock_info = []
|
684 |
|
|
|
75 |
|
76 |
products = inventory_data['data']['inventory']
|
77 |
|
78 |
+
# Beden terimleri kontrolü
|
79 |
+
size_terms = ['xs', 's', 'm', 'l', 'xl', 'xxl', '2xl', '3xl', 'small', 'medium', 'large']
|
80 |
+
size_numbers = ['44', '46', '48', '50', '52', '54', '56', '58', '60']
|
81 |
+
|
82 |
+
# Arama terimlerinde beden var mı kontrol et
|
83 |
+
has_size_query = False
|
84 |
+
size_query = None
|
85 |
+
for term in search_terms:
|
86 |
+
if term in size_terms or term in size_numbers:
|
87 |
+
has_size_query = True
|
88 |
+
size_query = term
|
89 |
+
break
|
90 |
+
|
91 |
+
# Eğer sadece beden sorgusu varsa (ör: "m", "xl")
|
92 |
+
is_only_size_query = len(search_terms) == 1 and has_size_query
|
93 |
+
|
94 |
# Ürünü ara
|
95 |
warehouse_variants = []
|
96 |
dsw_stock_count = 0
|
|
|
99 |
product_title = normalize_turkish(product.get('title', '')).lower()
|
100 |
original_title = product.get('title', '')
|
101 |
|
102 |
+
# Eğer sadece beden sorgusu ise
|
103 |
+
if is_only_size_query:
|
104 |
+
# Beden terimini ürün başlığında ara (parantez içinde veya dışında)
|
105 |
+
# Örnek: "Marlin 5 (M)" veya "Marlin 5 M" veya "Marlin 5 - M"
|
106 |
+
if size_query in product_title.split() or f'({size_query})' in product_title or f' {size_query} ' in product_title or product_title.endswith(f' {size_query}'):
|
107 |
+
qty = int(product.get('qty', 0))
|
108 |
+
stock = int(product.get('stock', 0))
|
109 |
+
actual_stock = max(qty, stock)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
+
if actual_stock > 0:
|
112 |
+
if is_dsw:
|
113 |
+
dsw_stock_count += actual_stock
|
114 |
+
continue
|
115 |
+
warehouse_variants.append(f"{original_title}: ✓ Stokta")
|
116 |
+
else:
|
117 |
+
# Normal ürün araması - tüm terimler eşleşmeli
|
118 |
+
# Ama beden terimi varsa özel kontrol yap
|
119 |
+
if has_size_query:
|
120 |
+
# Beden hariç diğer terimleri kontrol et
|
121 |
+
non_size_terms = [t for t in search_terms if t != size_query]
|
122 |
+
product_matches = all(term in product_title for term in non_size_terms)
|
123 |
|
124 |
+
# Beden kontrolü - daha esnek
|
125 |
+
size_matches = size_query in product_title.split() or f'({size_query})' in product_title or f' {size_query} ' in product_title or product_title.endswith(f' {size_query}')
|
126 |
|
127 |
+
if product_matches and size_matches:
|
128 |
+
qty = int(product.get('qty', 0))
|
129 |
+
stock = int(product.get('stock', 0))
|
130 |
+
actual_stock = max(qty, stock)
|
131 |
+
|
132 |
+
if actual_stock > 0:
|
133 |
+
if is_dsw:
|
134 |
+
dsw_stock_count += actual_stock
|
135 |
+
continue
|
136 |
+
|
137 |
+
# Varyant bilgisini göster
|
138 |
+
variant_info = original_title
|
139 |
+
possible_names = [
|
140 |
+
product_name.upper(),
|
141 |
+
product_name.lower(),
|
142 |
+
product_name.title(),
|
143 |
+
product_name.upper().replace('I', 'İ'),
|
144 |
+
product_name.upper().replace('İ', 'I'),
|
145 |
+
]
|
146 |
+
|
147 |
+
if 'fx sport' in product_name.lower():
|
148 |
+
possible_names.extend(['FX Sport AL 3', 'FX SPORT AL 3', 'Fx Sport Al 3'])
|
149 |
+
|
150 |
+
for possible_name in possible_names:
|
151 |
+
variant_info = variant_info.replace(possible_name, '').strip()
|
152 |
+
|
153 |
+
variant_info = ' '.join(variant_info.split())
|
154 |
+
|
155 |
+
if variant_info and variant_info != original_title:
|
156 |
+
warehouse_variants.append(f"{variant_info}: ✓ Stokta")
|
157 |
+
else:
|
158 |
+
warehouse_variants.append(f"{original_title}: ✓ Stokta")
|
159 |
+
else:
|
160 |
+
# Beden sorgusu yoksa normal kontrol
|
161 |
+
if all(term in product_title for term in search_terms):
|
162 |
+
qty = int(product.get('qty', 0))
|
163 |
+
stock = int(product.get('stock', 0))
|
164 |
+
actual_stock = max(qty, stock)
|
165 |
+
|
166 |
+
if actual_stock > 0:
|
167 |
+
if is_dsw:
|
168 |
+
dsw_stock_count += actual_stock
|
169 |
+
continue
|
170 |
+
|
171 |
+
variant_info = original_title
|
172 |
+
possible_names = [
|
173 |
+
product_name.upper(),
|
174 |
+
product_name.lower(),
|
175 |
+
product_name.title(),
|
176 |
+
product_name.upper().replace('I', 'İ'),
|
177 |
+
product_name.upper().replace('İ', 'I'),
|
178 |
+
]
|
179 |
+
|
180 |
+
if 'fx sport' in product_name.lower():
|
181 |
+
possible_names.extend(['FX Sport AL 3', 'FX SPORT AL 3', 'Fx Sport Al 3'])
|
182 |
+
|
183 |
+
for possible_name in possible_names:
|
184 |
+
variant_info = variant_info.replace(possible_name, '').strip()
|
185 |
+
|
186 |
+
variant_info = ' '.join(variant_info.split())
|
187 |
+
|
188 |
+
if variant_info and variant_info != original_title:
|
189 |
+
warehouse_variants.append(f"{variant_info}: ✓ Stokta")
|
190 |
+
else:
|
191 |
+
warehouse_variants.append(f"{original_title}: ✓ Stokta")
|
192 |
|
193 |
# Sonuç döndür
|
194 |
if warehouse_variants and not is_dsw:
|
|
|
740 |
skip_words = ['stok', 'stock', 'kaç', 'adet', 'tane', 'var', 'mı', 'mi',
|
741 |
'mevcut', 'mu', 'bulunuyor', 'hangi', 'mağaza', 'nerede',
|
742 |
'durumu', 'stoklarda', 'stokta', 'için', 've', 'ile', 'toplam', 'toplamda',
|
743 |
+
'fiyat', 'fiyatı', 'ne', 'nedir', 'kadar', 'beden', 'bedeni', 'bedenli']
|
744 |
+
|
745 |
+
# Beden terimleri - bunları skip etmemeliyiz
|
746 |
+
size_terms = ['xs', 's', 'm', 'l', 'xl', 'xxl', '2xl', '3xl', 'small', 'medium', 'large',
|
747 |
+
'44', '46', '48', '50', '52', '54', '56', '58', '60']
|
748 |
|
749 |
for word in user_message.lower().split():
|
750 |
+
# Beden terimlerini veya skip_words dışındakileri ekle
|
751 |
if word not in skip_words:
|
752 |
product_words.append(word)
|
753 |
|
754 |
if product_words:
|
755 |
product_name = ' '.join(product_words)
|
756 |
|
757 |
+
# Sadece beden sorgusu mu kontrol et (ör: "M", "XL", "52")
|
758 |
+
size_terms = ['xs', 's', 'm', 'l', 'xl', 'xxl', '2xl', '3xl', 'small', 'medium', 'large',
|
759 |
+
'44', '46', '48', '50', '52', '54', '56', '58', '60']
|
760 |
+
is_only_size = len(product_words) == 1 and product_words[0].lower() in size_terms
|
761 |
+
|
762 |
+
if is_only_size:
|
763 |
+
# Sadece beden sorgusu - tüm ürünlerde bu bedeni ara
|
764 |
+
product_name = product_words[0].lower()
|
765 |
+
print(f"Sadece beden sorgusu: {product_name}")
|
766 |
+
|
767 |
# Kısa isimlerle arama yapıldıysa birden fazla ürün olabilir
|
768 |
all_stock_info = []
|
769 |
|