| 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
|
| 19144 |
kshitij.so |
6 |
from dtr.utils.utils import to_java_date, getCashBack
|
| 13755 |
kshitij.so |
7 |
from datetime import datetime, timedelta
|
|
|
8 |
import time
|
| 14258 |
kshitij.so |
9 |
import optparse
|
| 14325 |
kshitij.so |
10 |
from dtr.storage.MemCache import MemCache
|
| 16635 |
manish.sha |
11 |
import traceback
|
| 19211 |
kshitij.so |
12 |
import math
|
| 19631 |
kshitij.so |
13 |
from pymongo import ASCENDING, DESCENDING
|
| 13755 |
kshitij.so |
14 |
|
| 20347 |
kshitij.so |
15 |
|
| 14258 |
kshitij.so |
16 |
parser = optparse.OptionParser()
|
| 14263 |
kshitij.so |
17 |
parser.add_option("-H", "--host", dest="hostname",
|
| 14258 |
kshitij.so |
18 |
default="localhost",
|
|
|
19 |
type="string", help="The HOST where the DB server is running",
|
|
|
20 |
metavar="host")
|
|
|
21 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
22 |
default="localhost",
|
|
|
23 |
type="string", help="The HOST where the mongo server is running",
|
|
|
24 |
metavar="mongo_host")
|
| 19631 |
kshitij.so |
25 |
parser.add_option("-s", "--s", dest="shuffle",
|
|
|
26 |
default="false",
|
|
|
27 |
type="string", help="Shuffle ranks for accessories",
|
|
|
28 |
metavar="shuffle")
|
| 13755 |
kshitij.so |
29 |
|
| 19631 |
kshitij.so |
30 |
|
| 14258 |
kshitij.so |
31 |
(options, args) = parser.parse_args()
|
| 14325 |
kshitij.so |
32 |
|
|
|
33 |
mc = MemCache(options.mongoHost)
|
|
|
34 |
|
| 14258 |
kshitij.so |
35 |
DataService.initialize(db_hostname=options.hostname)
|
|
|
36 |
|
| 13755 |
kshitij.so |
37 |
con = None
|
| 17034 |
kshitij.so |
38 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5,'PAYTM.COM':6,'HOMESHOP18.COM':7}
|
| 13755 |
kshitij.so |
39 |
DISCOUNT_TYPE = {'MRP':1,'DP':2}
|
|
|
40 |
LATEST_UPDATED_ITEMS = []
|
| 16253 |
kshitij.so |
41 |
STATUS_WEIGHTAGE = {1 : 1.0, 2 : 2.0, 3 : 1.0, 4 : 0.5}
|
| 19584 |
kshitij.so |
42 |
subCatRankList = []
|
| 13755 |
kshitij.so |
43 |
|
| 16868 |
kshitij.so |
44 |
DEAL_POINTS_MAP = {}
|
|
|
45 |
|
| 13755 |
kshitij.so |
46 |
now = datetime.now()
|
|
|
47 |
|
| 20347 |
kshitij.so |
48 |
DEAL_PRIORITY = [4, 2, 1, 5, 6, 7, 3] #Increasing order of source deal priority
|
|
|
49 |
|
| 16868 |
kshitij.so |
50 |
class __DealPoints:
|
|
|
51 |
|
|
|
52 |
def __init__(self, manualDealThresholdPrice, points):
|
|
|
53 |
self.manualDealThresholdPrice = manualDealThresholdPrice
|
|
|
54 |
self.points = points
|
|
|
55 |
|
|
|
56 |
|
| 13755 |
kshitij.so |
57 |
class __SkuInfo:
|
|
|
58 |
|
|
|
59 |
def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
|
| 16534 |
kshitij.so |
60 |
maxDiscount, discountType, dp, nlcPoints, status, in_stock, maxprice, brand, dealType, brand_id, manualDealThresholdPrice,\
|
| 20369 |
kshitij.so |
61 |
codAvailable,showDp,gross_price, subCategoryId,subCategory,shippingCost,netPriceAfterCashBack):
|
| 13755 |
kshitij.so |
62 |
self._id = _id
|
|
|
63 |
self.skuBundleId = skuBundleId
|
|
|
64 |
self.category_id = category_id
|
|
|
65 |
self.mrp = mrp
|
|
|
66 |
self.available_price = available_price
|
|
|
67 |
self.source_id = source_id
|
|
|
68 |
self.rank = rank
|
|
|
69 |
self.maxNlc = maxNlc
|
|
|
70 |
self.minNlc = minNlc
|
|
|
71 |
self.schemeAmount = schemeAmount
|
|
|
72 |
self.minDiscount = minDiscount
|
|
|
73 |
self.maxDiscount = maxDiscount
|
|
|
74 |
self.discountType = discountType
|
|
|
75 |
self.dp = dp
|
|
|
76 |
self.nlcPoints = nlcPoints
|
| 13824 |
kshitij.so |
77 |
self.status = status
|
|
|
78 |
self.in_stock = in_stock
|
|
|
79 |
self.maxprice = maxprice
|
| 14035 |
kshitij.so |
80 |
self.brand = brand
|
| 14306 |
kshitij.so |
81 |
self.dealType = dealType
|
| 15063 |
kshitij.so |
82 |
self.brand_id = brand_id
|
| 15271 |
kshitij.so |
83 |
self.manualDealThresholdPrice = manualDealThresholdPrice
|
| 16019 |
kshitij.so |
84 |
self.codAvailable = codAvailable
|
| 16253 |
kshitij.so |
85 |
self.showDp = showDp
|
| 16456 |
kshitij.so |
86 |
self.gross_price = gross_price
|
| 17649 |
kshitij.so |
87 |
self.subCategoryId = subCategoryId
|
| 17678 |
kshitij.so |
88 |
self.subCategory = subCategory
|
| 17713 |
kshitij.so |
89 |
self.shippingCost = shippingCost
|
| 19144 |
kshitij.so |
90 |
self.netPriceAfterCashBack = netPriceAfterCashBack
|
| 13755 |
kshitij.so |
91 |
|
|
|
92 |
|
| 14258 |
kshitij.so |
93 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13755 |
kshitij.so |
94 |
global con
|
|
|
95 |
if con is None:
|
|
|
96 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
97 |
try:
|
|
|
98 |
con = pymongo.MongoClient(host, port)
|
|
|
99 |
except Exception, e:
|
|
|
100 |
print e
|
|
|
101 |
return None
|
|
|
102 |
return con
|
|
|
103 |
|
|
|
104 |
def populateStuff():
|
|
|
105 |
print "Inside populate"
|
|
|
106 |
global LATEST_UPDATED_ITEMS
|
|
|
107 |
"""Fetch latest updated items across portals
|
|
|
108 |
and calculate max and min R-Nlc"""
|
|
|
109 |
offset= 0
|
|
|
110 |
while(True):
|
|
|
111 |
print "Fetching records offset %d and limit %d" %(offset,300)
|
| 20675 |
kshitij.so |
112 |
topSkus = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'updatedOn': { "$gt": to_java_date(now - timedelta(hours=1))} }, { 'source_id' : { "$in": SOURCE_MAP.values() }},{'category_id':{"$in":[3,5]}}] }).skip(offset).limit(300))
|
| 13755 |
kshitij.so |
113 |
if len((topSkus)) == 0:
|
|
|
114 |
break
|
|
|
115 |
#topSkus = collection.find( {'_id':664})
|
|
|
116 |
for sku in topSkus:
|
| 13908 |
kshitij.so |
117 |
"""Fix this """
|
|
|
118 |
#TODO Compute deal flags else where.
|
| 19144 |
kshitij.so |
119 |
if sku['source_id'] != SOURCE_MAP.get('PAYTM.COM'):
|
|
|
120 |
netPriceAfterCashBack = getNetPriceForItem(sku['_id'], sku['source_id'], sku['category_id'], sku['available_price'])
|
|
|
121 |
else:
|
|
|
122 |
if sku['codAvailable'] ==0:
|
|
|
123 |
netPriceAfterCashBack = getNetPriceForItem(sku['_id'], sku['source_id'], sku['category_id'], sku['gross_price'])
|
|
|
124 |
else:
|
|
|
125 |
netPriceAfterCashBack = getNetPriceForItem(sku['_id'], sku['source_id'], sku['category_id'], sku['available_price'])
|
|
|
126 |
|
| 13755 |
kshitij.so |
127 |
info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \
|
| 17678 |
kshitij.so |
128 |
None, None, None, None, sku['status'], sku['in_stock'],sku['maxPrice'],sku['brand'].strip().upper(), 0, sku['brand_id'], None, sku['codAvailable'], 0, sku['gross_price'], sku['subCategoryId'], \
|
| 20369 |
kshitij.so |
129 |
sku['subCategory'],sku['shippingCost'],netPriceAfterCashBack)
|
| 15854 |
kshitij.so |
130 |
exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'skuBundleId':info.skuBundleId}, {'overrideNlc':1} ]} ))
|
| 13755 |
kshitij.so |
131 |
if len(exceptionalNlc) > 0:
|
|
|
132 |
"""Exceptional nlc found, no need to calculate max and min R-nlc"""
|
|
|
133 |
info.maxNlc = exceptionalNlc[0]['maxNlc']
|
|
|
134 |
info.minNlc = exceptionalNlc[0]['minNlc']
|
| 14842 |
kshitij.so |
135 |
if info.maxprice == 0:
|
|
|
136 |
info.maxprice = exceptionalNlc[0]['maxNlc']
|
| 13755 |
kshitij.so |
137 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
138 |
continue
|
|
|
139 |
|
| 15854 |
kshitij.so |
140 |
skuSchemeDetails = list(get_mongo_connection().Catalog.SkuSchemeDetails.find( {'skuBundleId':info.skuBundleId}))
|
| 13755 |
kshitij.so |
141 |
if len(skuSchemeDetails) > 0:
|
|
|
142 |
"""Sku scheme details, populate scheme amount (Recently added)"""
|
| 13824 |
kshitij.so |
143 |
|
|
|
144 |
#TODO Add start date and end date of scehems
|
|
|
145 |
|
| 13755 |
kshitij.so |
146 |
info.schemeAmount = float(skuSchemeDetails[0]['schemeAmount'])
|
|
|
147 |
|
| 15854 |
kshitij.so |
148 |
skuDealerPrices = list(get_mongo_connection().Catalog.SkuDealerPrices.find( {'skuBundleId':info.skuBundleId} ) )
|
| 13755 |
kshitij.so |
149 |
if len(skuDealerPrices) > 0:
|
|
|
150 |
info.dp = skuDealerPrices[0]['dp']
|
| 16253 |
kshitij.so |
151 |
info.showDp = skuDealerPrices[0]['showDp']
|
| 15854 |
kshitij.so |
152 |
skuDiscount = list(get_mongo_connection().Catalog.SkuDiscountInfo.find( {'skuBundleId':info.skuBundleId} ) )
|
| 13755 |
kshitij.so |
153 |
if len(skuDiscount) > 0:
|
|
|
154 |
"""Sku rule found, populate max , min Discount and discount type"""
|
|
|
155 |
info.maxDiscount = skuDiscount[0]['max_discount']
|
|
|
156 |
info.minDiscount = skuDiscount[0]['min_discount']
|
|
|
157 |
info.discountType = DISCOUNT_TYPE.get(skuDiscount[0]['discountType'].upper())
|
|
|
158 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
159 |
continue
|
|
|
160 |
|
| 14035 |
kshitij.so |
161 |
categoryDiscount = list(get_mongo_connection().Catalog.CategoryDiscount.find( {"$and" : [{'brand':sku['brand'].strip().upper()}, {'category_id':sku['category_id']} ]} ))
|
| 13755 |
kshitij.so |
162 |
if len(categoryDiscount) > 0:
|
|
|
163 |
info.maxDiscount = categoryDiscount[0]['max_discount']
|
|
|
164 |
info.minDiscount = categoryDiscount[0]['min_discount']
|
|
|
165 |
info.discountType = DISCOUNT_TYPE.get(categoryDiscount[0]['discountType'].upper())
|
|
|
166 |
|
|
|
167 |
LATEST_UPDATED_ITEMS.append(info)
|
|
|
168 |
offset = offset + 300
|
|
|
169 |
for lol in LATEST_UPDATED_ITEMS:
|
|
|
170 |
print lol.__dict__
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
def calculateNlc():
|
|
|
174 |
global LATEST_UPDATED_ITEMS
|
|
|
175 |
populated = 0
|
|
|
176 |
while(populated <= len(LATEST_UPDATED_ITEMS)):
|
|
|
177 |
inventory_client = InventoryClient().get_client()
|
|
|
178 |
for obj in LATEST_UPDATED_ITEMS[populated:300+populated]:
|
| 19633 |
kshitij.so |
179 |
if (obj.maxNlc > 0 and obj.minNlc > 0) or obj.category_id==6:
|
| 13755 |
kshitij.so |
180 |
continue
|
|
|
181 |
saholic_sku = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'skuBundleId': obj.skuBundleId}, { 'source_id' : SOURCE_MAP.get('SAHOLIC')}] }))
|
|
|
182 |
identifier = None
|
|
|
183 |
if len(saholic_sku) > 0:
|
|
|
184 |
identifier = saholic_sku[0]['identifier']
|
|
|
185 |
if obj.discountType == DISCOUNT_TYPE.get('MRP'):
|
|
|
186 |
if obj.mrp == 0:
|
|
|
187 |
"""Now mrp is zero, so we have to use saholic MRP"""
|
|
|
188 |
if identifier is not None:
|
|
|
189 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
190 |
obj.mrp = it.mrp
|
|
|
191 |
if obj.mrp > 0:
|
| 14842 |
kshitij.so |
192 |
print obj._id
|
| 13755 |
kshitij.so |
193 |
obj.minNlc = obj.mrp - (obj.mrp * obj.maxDiscount/100) - obj.schemeAmount
|
| 13824 |
kshitij.so |
194 |
obj.maxNlc = obj.mrp - (obj.mrp * obj.minDiscount/100) - obj.schemeAmount
|
| 14842 |
kshitij.so |
195 |
if obj.maxprice == 0:
|
|
|
196 |
obj.maxprice = obj.maxNlc
|
| 13755 |
kshitij.so |
197 |
elif obj.discountType == DISCOUNT_TYPE.get('DP'):
|
|
|
198 |
if obj.dp == 0:
|
|
|
199 |
"""Now dp is zero, so we have to use saholic minimum dp for item"""
|
|
|
200 |
if identifier is not None:
|
|
|
201 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
202 |
try:
|
|
|
203 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
|
|
204 |
min_dp = min(pricing.dealerPrice for pricing in vendorPricing)
|
|
|
205 |
obj.dp = min_dp
|
| 16253 |
kshitij.so |
206 |
obj.showDp = 1
|
| 13755 |
kshitij.so |
207 |
except:
|
|
|
208 |
pass
|
|
|
209 |
if obj.dp > 0:
|
|
|
210 |
obj.minNlc = obj.dp - (obj.dp * obj.maxDiscount/100) - obj.schemeAmount
|
| 13824 |
kshitij.so |
211 |
obj.maxNlc = obj.dp - (obj.dp * obj.minDiscount/100) - obj.schemeAmount
|
| 14842 |
kshitij.so |
212 |
if obj.maxprice == 0:
|
|
|
213 |
obj.maxprice = obj.maxNlc
|
| 13755 |
kshitij.so |
214 |
else:
|
|
|
215 |
"""No rule found, use saholic min nlc as max and min R-Nlc"""
|
|
|
216 |
if identifier is not None:
|
|
|
217 |
it = Item.query.filter_by(catalog_item_id=identifier).first()
|
|
|
218 |
try:
|
|
|
219 |
vendorPricing = inventory_client.getAllItemPricing(it.id)
|
|
|
220 |
min_nlc = min(pricing.nlc for pricing in vendorPricing)
|
|
|
221 |
obj.maxNlc = min_nlc
|
| 13824 |
kshitij.so |
222 |
obj.minNlc = min_nlc
|
| 14842 |
kshitij.so |
223 |
if obj.maxprice == 0:
|
|
|
224 |
obj.maxprice = obj.maxNlc
|
| 13755 |
kshitij.so |
225 |
except:
|
|
|
226 |
pass
|
|
|
227 |
populated = populated + 300
|
|
|
228 |
time.sleep(10)
|
|
|
229 |
|
| 14306 |
kshitij.so |
230 |
def addManualDealsInfo():
|
|
|
231 |
for sku in LATEST_UPDATED_ITEMS:
|
|
|
232 |
manualDeal = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':sku.source_id, 'sku':sku._id}))
|
|
|
233 |
if len(manualDeal) > 0:
|
|
|
234 |
sku.dealType = manualDeal[0]['dealType']
|
| 14310 |
kshitij.so |
235 |
|
|
|
236 |
"""Remove deal flag from expired deals"""
|
| 14311 |
kshitij.so |
237 |
manualDeals = list(get_mongo_connection().Catalog.Deals.find({'dealType':1}))
|
| 14310 |
kshitij.so |
238 |
for manualDeal in manualDeals:
|
| 14389 |
kshitij.so |
239 |
d_manualDeal = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':manualDeal['source_id'], 'sku':manualDeal['_id']}))
|
|
|
240 |
if len(d_manualDeal) > 0:
|
| 14310 |
kshitij.so |
241 |
continue
|
|
|
242 |
else:
|
| 16507 |
kshitij.so |
243 |
get_mongo_connection().Catalog.Deals.update({'_id':manualDeal['_id']},{"$set":{'dealType':0}},upsert=False, multi=False)
|
| 14306 |
kshitij.so |
244 |
|
| 13755 |
kshitij.so |
245 |
def calculateNlcPoints():
|
|
|
246 |
global LATEST_UPDATED_ITEMS
|
| 14842 |
kshitij.so |
247 |
print "inside nlc oints"
|
| 13755 |
kshitij.so |
248 |
for sku in LATEST_UPDATED_ITEMS:
|
| 19637 |
kshitij.so |
249 |
if sku.maxNlc and sku.minNlc and sku.category_id!=6:
|
| 14842 |
kshitij.so |
250 |
print sku._id
|
| 13824 |
kshitij.so |
251 |
|
|
|
252 |
"""Create map - TODO"""
|
|
|
253 |
|
|
|
254 |
if sku.status == 2:
|
|
|
255 |
eolWeight = .60
|
|
|
256 |
else:
|
|
|
257 |
eolWeight = 1.0
|
|
|
258 |
if sku.category_id == 3:
|
|
|
259 |
basePointPercentage = 5.0
|
|
|
260 |
maxNlcPoints = 200
|
|
|
261 |
elif sku.category_id == 5:
|
|
|
262 |
basePointPercentage = 8.0
|
|
|
263 |
maxNlcPoints = 150
|
| 17540 |
kshitij.so |
264 |
elif sku.category_id == 6:
|
|
|
265 |
basePointPercentage = 5.0
|
|
|
266 |
maxNlcPoints = 200
|
|
|
267 |
|
| 13824 |
kshitij.so |
268 |
else:
|
|
|
269 |
basePointPercentage = 10.0
|
|
|
270 |
maxNlcPoints = 150
|
| 14842 |
kshitij.so |
271 |
discFromMinNlc = float((sku.minNlc - sku.available_price))/sku.available_price *100
|
|
|
272 |
discFromMaxNlc = float((sku.maxNlc - sku.available_price))/sku.available_price *100
|
|
|
273 |
print discFromMinNlc
|
|
|
274 |
print discFromMaxNlc
|
| 13755 |
kshitij.so |
275 |
if discFromMinNlc > 0:
|
| 13824 |
kshitij.so |
276 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
| 13755 |
kshitij.so |
277 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
|
|
278 |
nlcPoints = 0
|
|
|
279 |
else:
|
| 13824 |
kshitij.so |
280 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
|
|
281 |
if (min(nlcPoints,maxNlcPoints)) > 0:
|
|
|
282 |
sku.nlcPoints = (min(nlcPoints,maxNlcPoints)) * eolWeight
|
|
|
283 |
else:
|
|
|
284 |
sku.nlcPoints = (min(nlcPoints,maxNlcPoints))
|
| 13755 |
kshitij.so |
285 |
else:
|
|
|
286 |
sku.nlcPoints = 0
|
|
|
287 |
|
|
|
288 |
def commitData():
|
|
|
289 |
global LATEST_UPDATED_ITEMS
|
|
|
290 |
for sku in LATEST_UPDATED_ITEMS:
|
|
|
291 |
#get_mongo_connection().Catalog.Deals.update({'_id':sku._id},{'$set' : sku.__dict__},upsert=True,multi=True)
|
| 14020 |
amit.gupta |
292 |
get_mongo_connection().Catalog.Deals.update({'_id':sku._id},{"$set":sku.__dict__},upsert=True)
|
| 13755 |
kshitij.so |
293 |
|
|
|
294 |
|
|
|
295 |
def addBestSellerPoints():
|
|
|
296 |
allItems = list(get_mongo_connection().Catalog.Deals.find({}))
|
|
|
297 |
for sku in allItems:
|
| 13824 |
kshitij.so |
298 |
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 |
299 |
if len(bestSellerPoints) > 0:
|
| 13824 |
kshitij.so |
300 |
print bestSellerPoints[0]['points']
|
|
|
301 |
if (bestSellerPoints[0]['points']) > 0:
|
| 16253 |
kshitij.so |
302 |
sku['bestSellerPoints'] = (bestSellerPoints[0]['points']) * bestSellerPoints[0]['weightage'] * STATUS_WEIGHTAGE.get(sku['status'])
|
| 13824 |
kshitij.so |
303 |
else:
|
|
|
304 |
sku['bestSellerPoints'] = (bestSellerPoints[0]['points'])
|
| 13755 |
kshitij.so |
305 |
else:
|
| 17708 |
kshitij.so |
306 |
if sku['category_id'] == 6:
|
| 17763 |
kshitij.so |
307 |
sku['bestSellerPoints'] = 0
|
| 17708 |
kshitij.so |
308 |
else:
|
|
|
309 |
sku['bestSellerPoints'] = -120
|
| 14114 |
kshitij.so |
310 |
#sku['totalPoints'] = sku['bestSellerPoints'] + sku['nlcPoints']
|
|
|
311 |
get_mongo_connection().Catalog.Deals.update({'_id':sku['_id']},{'$set':{'bestSellerPoints':sku['bestSellerPoints']}},multi=False)
|
|
|
312 |
|
| 14390 |
kshitij.so |
313 |
shortageSkus = get_mongo_connection().Catalog.MasterData.find({"$and":[{'is_shortage': 1 }, { 'source_id' : { "$in": SOURCE_MAP.values() } }] }).distinct('_id')
|
| 14388 |
kshitij.so |
314 |
|
| 14114 |
kshitij.so |
315 |
for sku in allItems:
|
|
|
316 |
deal_item = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':sku['skuBundleId']}).sort('bestSellerPoints',pymongo.DESCENDING).limit(1))
|
| 14142 |
kshitij.so |
317 |
sku['catalogBestSellerPoints'] = deal_item[0]['bestSellerPoints']
|
| 14388 |
kshitij.so |
318 |
shortagePoints = 50 if sku['_id'] in shortageSkus else 0
|
| 16868 |
kshitij.so |
319 |
|
|
|
320 |
dealPoints = DEAL_POINTS_MAP.get(sku['skuBundleId'])
|
|
|
321 |
|
|
|
322 |
if dealPoints is not None and dealPoints.manualDealThresholdPrice >= sku['available_price']:
|
|
|
323 |
sku['dealPoints'] = dealPoints.points
|
|
|
324 |
sku['manualDealThresholdPrice'] = dealPoints.manualDealThresholdPrice
|
|
|
325 |
else:
|
|
|
326 |
sku['dealPoints'] = 0
|
|
|
327 |
sku['manualDealThresholdPrice'] = None
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
sku['totalPoints'] = sku['catalogBestSellerPoints'] + sku['nlcPoints'] + shortagePoints + sku['dealPoints']
|
|
|
331 |
get_mongo_connection().Catalog.Deals.update({'_id':sku['_id']},{'$set':{'catalogBestSellerPoints':sku['catalogBestSellerPoints'],'totalPoints':sku['totalPoints'],'dealPoints':sku['dealPoints'], \
|
|
|
332 |
'manualDealThresholdPrice':sku['manualDealThresholdPrice']}},multi=False)
|
| 15271 |
kshitij.so |
333 |
|
|
|
334 |
|
| 13755 |
kshitij.so |
335 |
|
| 14325 |
kshitij.so |
336 |
def populateNegativeDeals():
|
|
|
337 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
|
|
338 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
339 |
|
| 16868 |
kshitij.so |
340 |
def eliminateSimilarDeals():
|
| 13912 |
kshitij.so |
341 |
allItems = get_mongo_connection().Catalog.Deals.find().distinct('skuBundleId')
|
|
|
342 |
for skuBundleId in allItems:
|
|
|
343 |
print skuBundleId
|
| 19144 |
kshitij.so |
344 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('netPriceAfterCashBack',pymongo.ASCENDING)]))
|
| 13828 |
kshitij.so |
345 |
bestPrice = float("inf")
|
|
|
346 |
bestOne = None
|
|
|
347 |
toUpdate = []
|
| 16019 |
kshitij.so |
348 |
prepaidBestPrice = float("inf")
|
|
|
349 |
prepaidBestOne = None
|
| 13828 |
kshitij.so |
350 |
for similarItem in similarItems:
|
| 16019 |
kshitij.so |
351 |
if similarItem['codAvailable'] ==1:
|
|
|
352 |
if mc.get("negative_deals") is None:
|
|
|
353 |
populateNegativeDeals()
|
| 16171 |
kshitij.so |
354 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
355 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
356 |
continue
|
| 17675 |
kshitij.so |
357 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0 and similarItem['category_id']!=6:
|
| 16019 |
kshitij.so |
358 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
359 |
continue
|
| 19144 |
kshitij.so |
360 |
if similarItem['netPriceAfterCashBack'] < bestPrice:
|
| 20347 |
kshitij.so |
361 |
|
| 16019 |
kshitij.so |
362 |
bestOne = similarItem
|
| 19144 |
kshitij.so |
363 |
bestPrice = similarItem['netPriceAfterCashBack']
|
| 20347 |
kshitij.so |
364 |
elif similarItem['netPriceAfterCashBack'] == bestPrice:
|
|
|
365 |
|
|
|
366 |
try:
|
|
|
367 |
if (DEAL_PRIORITY.index(int(similarItem['source_id'])) > DEAL_PRIORITY.index(int(bestOne['source_id']))):
|
|
|
368 |
continue
|
|
|
369 |
except:
|
|
|
370 |
traceback.print_exc()
|
|
|
371 |
|
| 16019 |
kshitij.so |
372 |
bestOne = similarItem
|
| 19144 |
kshitij.so |
373 |
bestPrice = similarItem['netPriceAfterCashBack']
|
| 16019 |
kshitij.so |
374 |
else:
|
|
|
375 |
pass
|
| 13828 |
kshitij.so |
376 |
else:
|
| 16019 |
kshitij.so |
377 |
if mc.get("negative_deals") is None:
|
|
|
378 |
populateNegativeDeals()
|
| 16171 |
kshitij.so |
379 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
380 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
381 |
continue
|
| 17750 |
kshitij.so |
382 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0 and similarItem['category_id']!=6:
|
| 16019 |
kshitij.so |
383 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
384 |
continue
|
| 19144 |
kshitij.so |
385 |
if similarItem['netPriceAfterCashBack'] < prepaidBestPrice:
|
| 16019 |
kshitij.so |
386 |
prepaidBestOne = similarItem
|
| 19144 |
kshitij.so |
387 |
prepaidBestPrice = similarItem['netPriceAfterCashBack']
|
| 20347 |
kshitij.so |
388 |
elif similarItem['netPriceAfterCashBack'] == prepaidBestPrice:
|
|
|
389 |
|
|
|
390 |
try:
|
| 20368 |
kshitij.so |
391 |
if (DEAL_PRIORITY.index(int(similarItem['source_id'])) > DEAL_PRIORITY.index(int(prepaidBestOne['source_id']))):
|
| 20347 |
kshitij.so |
392 |
continue
|
|
|
393 |
except:
|
|
|
394 |
traceback.print_exc()
|
|
|
395 |
|
| 16019 |
kshitij.so |
396 |
prepaidBestOne = similarItem
|
| 19144 |
kshitij.so |
397 |
prepaidBestPrice = similarItem['netPriceAfterCashBack']
|
| 16019 |
kshitij.so |
398 |
else:
|
|
|
399 |
pass
|
| 16026 |
kshitij.so |
400 |
if bestOne is not None or prepaidBestOne is not None:
|
| 13828 |
kshitij.so |
401 |
for similarItem in similarItems:
|
|
|
402 |
toUpdate.append(similarItem['_id'])
|
| 16026 |
kshitij.so |
403 |
if bestOne is not None:
|
|
|
404 |
toUpdate.remove(bestOne['_id'])
|
|
|
405 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
|
|
|
406 |
if prepaidBestOne is not None:
|
| 16071 |
kshitij.so |
407 |
if bestOne is not None:
|
| 19144 |
kshitij.so |
408 |
if prepaidBestOne['netPriceAfterCashBack'] < bestOne['netPriceAfterCashBack']:
|
| 16071 |
kshitij.so |
409 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
410 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
|
|
411 |
else:
|
|
|
412 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
413 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
| 13828 |
kshitij.so |
414 |
if len(toUpdate) > 0:
|
| 16019 |
kshitij.so |
415 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
|
| 13755 |
kshitij.so |
416 |
|
| 16868 |
kshitij.so |
417 |
def populateDealPointsBundle():
|
|
|
418 |
global DEAL_POINTS_MAP
|
|
|
419 |
activeDealPoints = get_mongo_connection().Catalog.DealPoints.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}})
|
|
|
420 |
for activeDeal in activeDealPoints:
|
|
|
421 |
dealPoints = __DealPoints(activeDeal['dealThresholdPrice'], activeDeal['dealPoints'])
|
| 19144 |
kshitij.so |
422 |
DEAL_POINTS_MAP[activeDeal['skuBundleId']] = dealPoints
|
| 16868 |
kshitij.so |
423 |
|
| 19144 |
kshitij.so |
424 |
def getNetPriceForItem(itemId, source_id, category_id ,price):
|
|
|
425 |
cash_back_type = 0
|
|
|
426 |
cash_back = 0
|
|
|
427 |
try:
|
|
|
428 |
cashBack = getCashBack(itemId, source_id, category_id, mc, options.mongoHost)
|
|
|
429 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
|
|
430 |
cash_back_type = 0
|
|
|
431 |
cash_back = 0
|
|
|
432 |
|
|
|
433 |
else:
|
|
|
434 |
if cashBack['cash_back_type'] in (1,2):
|
|
|
435 |
|
|
|
436 |
if cashBack.get('maxCashBack') is not None:
|
|
|
437 |
|
|
|
438 |
if cashBack.get('cash_back_type') ==1 and (float(cashBack.get('cash_back'))*price)/100 > cashBack.get('maxCashBack'):
|
|
|
439 |
cashBack['cash_back_type'] = 2
|
|
|
440 |
cashBack['cash_back'] = cashBack['maxCashBack']
|
|
|
441 |
elif cashBack.get('cash_back_type') ==2 and cashBack.get('cash_back') > cashBack.get('maxCashBack'):
|
|
|
442 |
cashBack['cash_back'] = cashBack['maxCashBack']
|
|
|
443 |
else:
|
|
|
444 |
pass
|
|
|
445 |
|
|
|
446 |
|
|
|
447 |
|
|
|
448 |
cash_back_type = cashBack['cash_back_type']
|
|
|
449 |
cash_back = float(cashBack['cash_back'])
|
|
|
450 |
except Exception as cashBackEx:
|
|
|
451 |
pass
|
|
|
452 |
|
|
|
453 |
if cash_back_type ==1:
|
| 19211 |
kshitij.so |
454 |
return (price - math.floor(float(cash_back)*price/100))
|
| 19144 |
kshitij.so |
455 |
elif cash_back_type ==2:
|
|
|
456 |
return (price - cash_back)
|
|
|
457 |
else:
|
|
|
458 |
return price
|
|
|
459 |
|
|
|
460 |
|
| 19584 |
kshitij.so |
461 |
def getSubCategoryRanking():
|
|
|
462 |
subCatRanks = get_mongo_connection().Catalog.SubCategoriesRank.find().sort([('rank',ASCENDING)])
|
|
|
463 |
for subCatRank in subCatRanks:
|
|
|
464 |
subCatRankList.append(subCatRank['subCategoryId'])
|
|
|
465 |
|
|
|
466 |
def startBucketing():
|
|
|
467 |
|
|
|
468 |
"""Lets create initial buckets of 25 products each from all valid subCategories"""
|
|
|
469 |
initialMap = {}
|
|
|
470 |
for subCat in subCatRankList:
|
|
|
471 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':{"$gt":0}}).sort([('internalRank',ASCENDING)]).limit(25))
|
|
|
472 |
temp_list = []
|
|
|
473 |
for product in products:
|
|
|
474 |
if product['skuBundleId'] not in temp_list:
|
|
|
475 |
temp_list.append(product['skuBundleId'])
|
|
|
476 |
initialMap[subCat] = temp_list
|
|
|
477 |
laterMap = {}
|
|
|
478 |
for subCat in subCatRankList:
|
|
|
479 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':{"$gt":0}}).sort([('internalRank',ASCENDING)]).skip(25))
|
|
|
480 |
temp_list = []
|
|
|
481 |
for product in products:
|
|
|
482 |
if product['skuBundleId'] not in temp_list:
|
|
|
483 |
temp_list.append(product['skuBundleId'])
|
|
|
484 |
laterMap[subCat] = temp_list
|
|
|
485 |
|
|
|
486 |
for subCat in subCatRankList:
|
|
|
487 |
no_of_products = len(initialMap.get(subCat))
|
|
|
488 |
if no_of_products < 25:
|
|
|
489 |
toFetch = 25 - no_of_products
|
|
|
490 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':0}).limit(toFetch))
|
|
|
491 |
for product in products:
|
|
|
492 |
initialMap.get(subCat).append(product['skuBundleId'])
|
|
|
493 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':0}).skip(toFetch))
|
|
|
494 |
for product in products:
|
|
|
495 |
laterMap.get(subCat).append(product['skuBundleId'])
|
|
|
496 |
else:
|
|
|
497 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':0}))
|
|
|
498 |
for product in products:
|
|
|
499 |
laterMap.get(subCat).append(product['skuBundleId'])
|
| 19144 |
kshitij.so |
500 |
|
| 19584 |
kshitij.so |
501 |
"""Now we have created two diff. maps having product sorted by rank.Lets create buckets (5 products) from initialMap and (10 products) from laterMap"""
|
|
|
502 |
|
|
|
503 |
dealRankPoints = 0
|
|
|
504 |
for k, v in initialMap.iteritems():
|
|
|
505 |
dealRankPoints = dealRankPoints + len(v)
|
|
|
506 |
for k, v in laterMap.iteritems():
|
|
|
507 |
dealRankPoints = dealRankPoints + len(v)
|
|
|
508 |
|
|
|
509 |
while(len(initialMap.keys())>0):
|
|
|
510 |
for subCat in subCatRankList:
|
|
|
511 |
v = initialMap.get(subCat)
|
|
|
512 |
if v is None or len(v) ==0:
|
|
|
513 |
initialMap.pop(subCat, None)
|
|
|
514 |
continue
|
|
|
515 |
get_mongo_connection().Catalog.Deals.update({'skuBundleId':v[0]},{"$set":{'dealRankPoints':dealRankPoints}},upsert=False,multi=True)
|
|
|
516 |
dealRankPoints = dealRankPoints-1
|
|
|
517 |
v.pop(0)
|
|
|
518 |
if len(v) == 0:
|
|
|
519 |
initialMap.pop(subCat, None)
|
|
|
520 |
|
|
|
521 |
|
|
|
522 |
print "============================================================"
|
|
|
523 |
|
|
|
524 |
while(len(laterMap.keys())>0):
|
|
|
525 |
for subCat in subCatRankList:
|
|
|
526 |
v = laterMap.get(subCat)
|
|
|
527 |
if v is None or len(v) ==0:
|
|
|
528 |
laterMap.pop(subCat, None)
|
|
|
529 |
continue
|
|
|
530 |
get_mongo_connection().Catalog.Deals.update({'skuBundleId':v[0]},{"$set":{'dealRankPoints':dealRankPoints}},upsert=False,multi=True)
|
|
|
531 |
dealRankPoints = dealRankPoints-1
|
|
|
532 |
v.pop(0)
|
|
|
533 |
if len(v) == 0:
|
|
|
534 |
laterMap.pop(subCat, None)
|
|
|
535 |
|
| 19631 |
kshitij.so |
536 |
def shuffle():
|
|
|
537 |
for subCat in subCatRankList:
|
|
|
538 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':0},{'skuBundleId':1}))
|
|
|
539 |
if len(products) >0:
|
|
|
540 |
max_rank = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat}).sort([('internalRank',DESCENDING)]).limit(1))
|
|
|
541 |
rank_to_assign = max_rank[0]['internalRank'] + 1
|
|
|
542 |
for p in products:
|
|
|
543 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':p['skuBundleId']},{"$set":{'internalRank':rank_to_assign}},upsert=False,multi=True)
|
|
|
544 |
rank_to_assign = rank_to_assign + 1
|
|
|
545 |
|
|
|
546 |
new_rank =1
|
|
|
547 |
start = 0
|
|
|
548 |
end = 5
|
|
|
549 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':{"$gt":0}},{'skuBundleId':1}).sort([('internalRank',ASCENDING)]).limit(25))
|
|
|
550 |
while(True):
|
|
|
551 |
temp_list = products[start:end]
|
|
|
552 |
shuffled_list = temp_list[1:] + temp_list[:1]
|
|
|
553 |
if len(shuffled_list) > 0:
|
|
|
554 |
for p in shuffled_list:
|
|
|
555 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':p['skuBundleId']},{"$set":{'internalRank':new_rank}},upsert=False,multi=True)
|
|
|
556 |
new_rank = new_rank + 1
|
|
|
557 |
start = end
|
|
|
558 |
end = end + 5
|
|
|
559 |
else:
|
|
|
560 |
break
|
|
|
561 |
products = list(get_mongo_connection().Catalog.MasterData.find({'subCategoryId':subCat,'internalRank':{"$gt":0}},{'skuBundleId':1}).sort([('internalRank',ASCENDING)]).skip(25))
|
|
|
562 |
start = 0
|
|
|
563 |
end = 10
|
|
|
564 |
while(True):
|
|
|
565 |
temp_list = products[start:end]
|
|
|
566 |
shuffled_list = temp_list[1:] + temp_list[:1]
|
|
|
567 |
if len(shuffled_list) > 0:
|
|
|
568 |
for p in shuffled_list:
|
|
|
569 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':p['skuBundleId']},{"$set":{'internalRank':new_rank}},upsert=False,multi=True)
|
|
|
570 |
new_rank = new_rank + 1
|
|
|
571 |
start = end
|
|
|
572 |
end = end + 10
|
|
|
573 |
else:
|
|
|
574 |
break
|
|
|
575 |
|
| 13755 |
kshitij.so |
576 |
def main():
|
| 14263 |
kshitij.so |
577 |
try:
|
| 19194 |
kshitij.so |
578 |
print "Starting populating stuff ", datetime.now()
|
| 14263 |
kshitij.so |
579 |
populateStuff()
|
| 19194 |
kshitij.so |
580 |
print "Starting calculate nlc stuff ", datetime.now()
|
| 14263 |
kshitij.so |
581 |
calculateNlc()
|
| 19194 |
kshitij.so |
582 |
print "Starting adding manualdeals stuff ", datetime.now()
|
| 14306 |
kshitij.so |
583 |
addManualDealsInfo()
|
| 14263 |
kshitij.so |
584 |
finally:
|
|
|
585 |
session.close()
|
| 19194 |
kshitij.so |
586 |
print "Starting calculate nlc points ", datetime.now()
|
| 13755 |
kshitij.so |
587 |
calculateNlcPoints()
|
| 19194 |
kshitij.so |
588 |
print "commiting data ", datetime.now()
|
| 13755 |
kshitij.so |
589 |
commitData()
|
| 19194 |
kshitij.so |
590 |
print "Populate deal points bundle ", datetime.now()
|
| 16868 |
kshitij.so |
591 |
populateDealPointsBundle()
|
| 19194 |
kshitij.so |
592 |
print "Add best seller points ", datetime.now()
|
| 13755 |
kshitij.so |
593 |
addBestSellerPoints()
|
| 19194 |
kshitij.so |
594 |
print "eliminate similar deals ", datetime.now()
|
| 16868 |
kshitij.so |
595 |
eliminateSimilarDeals()
|
| 19194 |
kshitij.so |
596 |
print "done ", datetime.now()
|
| 20675 |
kshitij.so |
597 |
# getSubCategoryRanking()
|
|
|
598 |
# if options.shuffle=='true':
|
|
|
599 |
# print "Shuffling products"
|
|
|
600 |
# shuffle()
|
|
|
601 |
# startBucketing()
|
|
|
602 |
# print "Done with bucketing",datetime.now()
|
| 19584 |
kshitij.so |
603 |
|
| 13755 |
kshitij.so |
604 |
|
| 13828 |
kshitij.so |
605 |
|
| 13755 |
kshitij.so |
606 |
if __name__=='__main__':
|
| 14842 |
kshitij.so |
607 |
main()
|