| 13755 |
kshitij.so |
1 |
import pymongo
|
|
|
2 |
from elixir import *
|
|
|
3 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
4 |
from shop2020.model.v1.catalog.impl.DataService import Item
|
|
|
5 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
6 |
from dtr.utils.utils import to_java_date
|
|
|
7 |
from datetime import datetime, timedelta
|
|
|
8 |
import time
|
|
|
9 |
|
|
|
10 |
DataService.initialize(db_hostname='localhost')
|
|
|
11 |
|
|
|
12 |
con = None
|
|
|
13 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4}
|
|
|
14 |
DISCOUNT_TYPE = {'MRP':1,'DP':2}
|
|
|
15 |
LATEST_UPDATED_ITEMS = []
|
|
|
16 |
|
|
|
17 |
now = datetime.now()
|
|
|
18 |
|
|
|
19 |
class __SkuInfo:
|
|
|
20 |
|
|
|
21 |
def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
|
| 13973 |
kshitij.so |
22 |
maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints, status, in_stock, maxprice):
|
| 13755 |
kshitij.so |
23 |
self._id = _id
|
|
|
24 |
self.skuBundleId = skuBundleId
|
|
|
25 |
self.category_id = category_id
|
|
|
26 |
self.mrp = mrp
|
|
|
27 |
self.available_price = available_price
|
|
|
28 |
self.source_id = source_id
|
|
|
29 |
self.rank = rank
|
|
|
30 |
self.maxNlc = maxNlc
|
|
|
31 |
self.minNlc = minNlc
|
|
|
32 |
self.schemeAmount = schemeAmount
|
|
|
33 |
self.minDiscount = minDiscount
|
|
|
34 |
self.maxDiscount = maxDiscount
|
|
|
35 |
self.discountType = discountType
|
|
|
36 |
self.dp = dp
|
|
|
37 |
self.nlcPoints = nlcPoints
|
|
|
38 |
self.bestSellerPoints = bestSellerPoints
|
| 13824 |
kshitij.so |
39 |
self.totalPoints = totalPoints
|
|
|
40 |
self.status = status
|
|
|
41 |
self.in_stock = in_stock
|
|
|
42 |
self.maxprice = maxprice
|
| 13755 |
kshitij.so |
43 |
|
|
|
44 |
|
|
|
45 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
46 |
global con
|
|
|
47 |
if con is None:
|
|
|
48 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
49 |
try:
|
|
|
50 |
con = pymongo.MongoClient(host, port)
|
|
|
51 |
except Exception, e:
|
|
|
52 |
print e
|
|
|
53 |
return None
|
|
|
54 |
return con
|
|
|
55 |
|
|
|
56 |
def populateStuff():
|
|
|
57 |
print "Inside populate"
|
|
|
58 |
global LATEST_UPDATED_ITEMS
|
|
|
59 |
"""Fetch latest updated items across portals
|
|
|
60 |
and calculate max and min R-Nlc"""
|
|
|
61 |
offset= 0
|
|
|
62 |
while(True):
|
|
|
63 |
print "Fetching records offset %d and limit %d" %(offset,300)
|
|
|
64 |
topSkus = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'updatedOn': { "$gt": to_java_date(now - timedelta(hours=4))} }, { 'source_id' : { "$in": SOURCE_MAP.values() } }] }).skip(offset).limit(300))
|
|
|
65 |
if len((topSkus)) == 0:
|
|
|
66 |
break
|
|
|
67 |
#topSkus = collection.find( {'_id':664})
|
|
|
68 |
for sku in topSkus:
|
| 13908 |
kshitij.so |
69 |
"""Fix this """
|
|
|
70 |
#TODO Compute deal flags else where.
|
| 13755 |
kshitij.so |
71 |
info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
|
| 13973 |
kshitij.so |
72 |
None, None, None, None, None, None, sku['status'], sku['in_stock'],None)
|
| 13755 |
kshitij.so |
73 |
exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'sku':info._id}, {'overrideNlc':1} ]} ))
|
|
|
74 |
if len(exceptionalNlc) > 0:
|
|
|
75 |
"""Exceptional nlc found, no need to calculate max and min R-nlc"""
|
|
|
76 |
info.maxNlc = exceptionalNlc[0]['maxNlc']
|
|
|
77 |
info.minNlc = exceptionalNlc[0]['minNlc']
|
| 13824 |
kshitij.so |
78 |
info.maxprice = exceptionalNlc[0]['maxNlc']
|
| 13755 |
kshitij.so |
79 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
80 |
continue
|
|
|
81 |
|
|
|
82 |
skuSchemeDetails = list(get_mongo_connection().Catalog.SkuSchemeDetails.find( {'sku':info._id}).sort([('addedOn',pymongo.DESCENDING)]).limit(1))
|
|
|
83 |
if len(skuSchemeDetails) > 0:
|
|
|
84 |
"""Sku scheme details, populate scheme amount (Recently added)"""
|
| 13824 |
kshitij.so |
85 |
|
|
|
86 |
#TODO Add start date and end date of scehems
|
|
|
87 |
|
| 13755 |
kshitij.so |
88 |
info.schemeAmount = float(skuSchemeDetails[0]['schemeAmount'])
|
|
|
89 |
|
|
|
90 |
skuDealerPrices = list(get_mongo_connection().Catalog.SkuDealerPrices.find( {'sku':info._id} ) )
|
|
|
91 |
if len(skuDealerPrices) > 0:
|
|
|
92 |
info.dp = skuDealerPrices[0]['dp']
|
|
|
93 |
skuDiscount = list(get_mongo_connection().Catalog.SkuDiscountInfo.find( {'sku':info._id} ) )
|
|
|
94 |
if len(skuDiscount) > 0:
|
|
|
95 |
"""Sku rule found, populate max , min Discount and discount type"""
|
|
|
96 |
info.maxDiscount = skuDiscount[0]['max_discount']
|
|
|
97 |
info.minDiscount = skuDiscount[0]['min_discount']
|
|
|
98 |
info.discountType = DISCOUNT_TYPE.get(skuDiscount[0]['discountType'].upper())
|
|
|
99 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
100 |
continue
|
|
|
101 |
|
| 13824 |
kshitij.so |
102 |
categoryDiscount = list(get_mongo_connection().Catalog.CategoryDiscount.find( {"$and" : [{'brand':sku['brand'].upper()}, {'category_id':sku['category_id']} ]} ))
|
| 13755 |
kshitij.so |
103 |
if len(categoryDiscount) > 0:
|
|
|
104 |
info.maxDiscount = categoryDiscount[0]['max_discount']
|
|
|
105 |
info.minDiscount = categoryDiscount[0]['min_discount']
|
|
|
106 |
info.discountType = DISCOUNT_TYPE.get(categoryDiscount[0]['discountType'].upper())
|
|
|
107 |
|
|
|
108 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
109 |
offset = offset + 300
|
|
|
110 |
for lol in LATEST_UPDATED_ITEMS:
|
|
|
111 |
print lol.__dict__
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
def calculateNlc():
|
|
|
115 |
global LATEST_UPDATED_ITEMS
|
|
|
116 |
populated = 0
|
|
|
117 |
while(populated <= len(LATEST_UPDATED_ITEMS)):
|
|
|
118 |
inventory_client = InventoryClient().get_client()
|
|
|
119 |
for obj in LATEST_UPDATED_ITEMS[populated:300+populated]:
|
|
|
120 |
if obj.maxNlc > 0 and obj.minNlc > 0:
|
|
|
121 |
continue
|
|
|
122 |
saholic_sku = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'skuBundleId': obj.skuBundleId}, { 'source_id' : SOURCE_MAP.get('SAHOLIC')}] }))
|
|
|
123 |
identifier = None
|
|
|
124 |
if len(saholic_sku) > 0:
|
|
|
125 |
identifier = saholic_sku[0]['identifier']
|
|
|
126 |
if obj.discountType == DISCOUNT_TYPE.get('MRP'):
|
|
|
127 |
if obj.mrp == 0:
|
|
|
128 |
"""Now mrp is zero, so we have to use saholic MRP"""
|
|
|
129 |
if identifier is not None:
|
|
|
130 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
131 |
obj.mrp = it.mrp
|
|
|
132 |
if obj.mrp > 0:
|
|
|
133 |
obj.minNlc = obj.mrp - (obj.mrp * obj.maxDiscount/100) - obj.schemeAmount
|
| 13824 |
kshitij.so |
134 |
obj.maxNlc = obj.mrp - (obj.mrp * obj.minDiscount/100) - obj.schemeAmount
|
| 13982 |
kshitij.so |
135 |
obj.maxprice = obj.maxNlc
|
| 13755 |
kshitij.so |
136 |
elif obj.discountType == DISCOUNT_TYPE.get('DP'):
|
|
|
137 |
if obj.dp == 0:
|
|
|
138 |
"""Now dp is zero, so we have to use saholic minimum dp for item"""
|
|
|
139 |
if identifier is not None:
|
|
|
140 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
141 |
try:
|
|
|
142 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
|
|
143 |
min_dp = min(pricing.dealerPrice for pricing in vendorPricing)
|
|
|
144 |
obj.dp = min_dp
|
|
|
145 |
except:
|
|
|
146 |
pass
|
|
|
147 |
if obj.dp > 0:
|
|
|
148 |
obj.minNlc = obj.dp - (obj.dp * obj.maxDiscount/100) - obj.schemeAmount
|
| 13824 |
kshitij.so |
149 |
obj.maxNlc = obj.dp - (obj.dp * obj.minDiscount/100) - obj.schemeAmount
|
|
|
150 |
obj.maxprice = obj.maxNlc
|
| 13755 |
kshitij.so |
151 |
else:
|
|
|
152 |
"""No rule found, use saholic min nlc as max and min R-Nlc"""
|
|
|
153 |
if identifier is not None:
|
|
|
154 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
155 |
try:
|
|
|
156 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
|
|
157 |
min_nlc = min(pricing.nlc for pricing in vendorPricing)
|
|
|
158 |
obj.maxNlc = min_nlc
|
| 13824 |
kshitij.so |
159 |
obj.minNlc = min_nlc
|
|
|
160 |
obj.maxprice = obj.maxNlc
|
| 13755 |
kshitij.so |
161 |
except:
|
|
|
162 |
pass
|
|
|
163 |
populated = populated + 300
|
|
|
164 |
time.sleep(10)
|
|
|
165 |
|
|
|
166 |
def calculateNlcPoints():
|
|
|
167 |
global LATEST_UPDATED_ITEMS
|
|
|
168 |
for sku in LATEST_UPDATED_ITEMS:
|
|
|
169 |
if sku.maxNlc and sku.minNlc:
|
| 13824 |
kshitij.so |
170 |
|
|
|
171 |
"""Create map - TODO"""
|
|
|
172 |
|
|
|
173 |
if sku.status == 2:
|
|
|
174 |
eolWeight = .60
|
|
|
175 |
else:
|
|
|
176 |
eolWeight = 1.0
|
|
|
177 |
if sku.category_id == 3:
|
|
|
178 |
basePointPercentage = 5.0
|
|
|
179 |
maxNlcPoints = 200
|
|
|
180 |
elif sku.category_id == 5:
|
|
|
181 |
basePointPercentage = 8.0
|
|
|
182 |
maxNlcPoints = 150
|
|
|
183 |
else:
|
|
|
184 |
basePointPercentage = 10.0
|
|
|
185 |
maxNlcPoints = 150
|
| 13755 |
kshitij.so |
186 |
discFromMinNlc = (sku.minNlc - sku.available_price)/sku.available_price *100
|
|
|
187 |
discFromMaxNlc = (sku.maxNlc - sku.available_price)/sku.available_price *100
|
|
|
188 |
if discFromMinNlc > 0:
|
| 13824 |
kshitij.so |
189 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
| 13755 |
kshitij.so |
190 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
|
|
191 |
nlcPoints = 0
|
|
|
192 |
else:
|
| 13824 |
kshitij.so |
193 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
|
|
194 |
if (min(nlcPoints,maxNlcPoints)) > 0:
|
|
|
195 |
sku.nlcPoints = (min(nlcPoints,maxNlcPoints)) * eolWeight
|
|
|
196 |
else:
|
|
|
197 |
sku.nlcPoints = (min(nlcPoints,maxNlcPoints))
|
| 13755 |
kshitij.so |
198 |
else:
|
|
|
199 |
sku.nlcPoints = 0
|
|
|
200 |
|
|
|
201 |
def commitData():
|
|
|
202 |
global LATEST_UPDATED_ITEMS
|
|
|
203 |
for sku in LATEST_UPDATED_ITEMS:
|
|
|
204 |
#get_mongo_connection().Catalog.Deals.update({'_id':sku._id},{'$set' : sku.__dict__},upsert=True,multi=True)
|
|
|
205 |
get_mongo_connection().Catalog.Deals.update({'_id':sku._id},sku.__dict__,upsert=True)
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
def addBestSellerPoints():
|
|
|
209 |
allItems = list(get_mongo_connection().Catalog.Deals.find({}))
|
|
|
210 |
for sku in allItems:
|
| 13824 |
kshitij.so |
211 |
bestSellerPoints = list(get_mongo_connection().Catalog.BestSellerPoints.find( {"$and":[{'min_rank': { "$lte": sku['rank'] } }, {'max_rank': { "$gte": sku['rank'] } } , { 'category_id' : sku['category_id'] }, { 'source_id' : sku['source_id'] }] } ))
|
| 13755 |
kshitij.so |
212 |
if len(bestSellerPoints) > 0:
|
| 13824 |
kshitij.so |
213 |
print bestSellerPoints[0]['points']
|
|
|
214 |
if (bestSellerPoints[0]['points']) > 0:
|
|
|
215 |
sku['bestSellerPoints'] = (bestSellerPoints[0]['points']) * bestSellerPoints[0]['weightage']
|
|
|
216 |
else:
|
|
|
217 |
sku['bestSellerPoints'] = (bestSellerPoints[0]['points'])
|
| 13755 |
kshitij.so |
218 |
else:
|
| 13824 |
kshitij.so |
219 |
sku['bestSellerPoints'] = -100
|
| 13755 |
kshitij.so |
220 |
sku['totalPoints'] = sku['bestSellerPoints'] + sku['nlcPoints']
|
|
|
221 |
get_mongo_connection().Catalog.Deals.update({'_id':sku['_id']},{'$set':{'bestSellerPoints':sku['bestSellerPoints'],'totalPoints':sku['totalPoints']}},multi=False)
|
|
|
222 |
|
| 13828 |
kshitij.so |
223 |
def elimiateSimilarDeals():
|
| 13912 |
kshitij.so |
224 |
allItems = get_mongo_connection().Catalog.Deals.find().distinct('skuBundleId')
|
|
|
225 |
for skuBundleId in allItems:
|
|
|
226 |
print skuBundleId
|
|
|
227 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
| 13828 |
kshitij.so |
228 |
bestPrice = float("inf")
|
|
|
229 |
bestOne = None
|
|
|
230 |
bestSellerPoints = 0
|
|
|
231 |
toUpdate = []
|
|
|
232 |
for similarItem in similarItems:
|
| 13973 |
kshitij.so |
233 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:
|
| 13828 |
kshitij.so |
234 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
235 |
continue
|
|
|
236 |
if similarItem['available_price'] < bestPrice:
|
|
|
237 |
bestOne = similarItem
|
|
|
238 |
bestPrice = similarItem['available_price']
|
|
|
239 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
240 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
241 |
bestOne = similarItem
|
|
|
242 |
bestPrice = similarItem['available_price']
|
|
|
243 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
244 |
else:
|
|
|
245 |
pass
|
|
|
246 |
if bestOne is not None:
|
|
|
247 |
for similarItem in similarItems:
|
|
|
248 |
toUpdate.append(similarItem['_id'])
|
|
|
249 |
toUpdate.remove(bestOne['_id'])
|
|
|
250 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
251 |
if len(toUpdate) > 0:
|
| 13908 |
kshitij.so |
252 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
| 13755 |
kshitij.so |
253 |
|
|
|
254 |
def main():
|
|
|
255 |
populateStuff()
|
|
|
256 |
calculateNlc()
|
|
|
257 |
calculateNlcPoints()
|
|
|
258 |
commitData()
|
|
|
259 |
addBestSellerPoints()
|
| 13828 |
kshitij.so |
260 |
elimiateSimilarDeals()
|
| 13755 |
kshitij.so |
261 |
|
| 13828 |
kshitij.so |
262 |
|
| 13755 |
kshitij.so |
263 |
if __name__=='__main__':
|
|
|
264 |
main()
|