| 13828 |
kshitij.so |
1 |
from elixir import *
|
|
|
2 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
3 |
from shop2020.model.v1.catalog.impl.DataService import PrivateDeals, Item
|
|
|
4 |
from sqlalchemy.sql.functions import now
|
|
|
5 |
from datetime import datetime, timedelta
|
|
|
6 |
import pymongo
|
|
|
7 |
from dtr.utils.utils import to_java_date
|
| 13843 |
kshitij.so |
8 |
import optparse
|
| 14325 |
kshitij.so |
9 |
from dtr.storage.MemCache import MemCache
|
| 13828 |
kshitij.so |
10 |
|
|
|
11 |
dealsMap = {}
|
|
|
12 |
con = None
|
|
|
13 |
dealsCatalogIds = []
|
|
|
14 |
itemCatalogMap = {}
|
|
|
15 |
|
| 13843 |
kshitij.so |
16 |
parser = optparse.OptionParser()
|
| 14259 |
kshitij.so |
17 |
parser.add_option("-H", "--host", dest="hostname",
|
| 13843 |
kshitij.so |
18 |
default="localhost",
|
|
|
19 |
type="string", help="The HOST where the DB server is running",
|
| 14259 |
kshitij.so |
20 |
metavar="db_host")
|
| 14253 |
kshitij.so |
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")
|
| 13828 |
kshitij.so |
25 |
|
| 13849 |
kshitij.so |
26 |
(options, args) = parser.parse_args()
|
| 14325 |
kshitij.so |
27 |
|
|
|
28 |
mc = MemCache(options.mongoHost)
|
|
|
29 |
|
| 13843 |
kshitij.so |
30 |
DataService.initialize(db_hostname=options.hostname)
|
|
|
31 |
|
| 14253 |
kshitij.so |
32 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
33 |
global con
|
|
|
34 |
if con is None:
|
|
|
35 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
36 |
try:
|
|
|
37 |
con = pymongo.MongoClient(host, port)
|
|
|
38 |
except Exception, e:
|
|
|
39 |
print e
|
|
|
40 |
return None
|
|
|
41 |
return con
|
|
|
42 |
|
|
|
43 |
def getPrivateDeals():
|
| 14259 |
kshitij.so |
44 |
try:
|
|
|
45 |
global dealsMap
|
|
|
46 |
dealsMap = dict()
|
|
|
47 |
all_active_items_query = session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate))
|
|
|
48 |
print all_active_items_query
|
|
|
49 |
all_active_private_deals = all_active_items_query.all()
|
|
|
50 |
if all_active_private_deals is not None or all_active_private_deals!=[]:
|
|
|
51 |
for active_private_deal in all_active_private_deals:
|
|
|
52 |
item = Item.get_by(id = active_private_deal.item_id)
|
|
|
53 |
if item.sellingPrice > active_private_deal.dealPrice and item.status==3:
|
|
|
54 |
dealsMap[active_private_deal.item_id] = active_private_deal
|
|
|
55 |
finally:
|
|
|
56 |
session.close()
|
| 13828 |
kshitij.so |
57 |
|
|
|
58 |
def getItemsToUpdate():
|
|
|
59 |
global dealsCatalogIds
|
|
|
60 |
global itemCatalogMap
|
| 14130 |
kshitij.so |
61 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0}}))
|
|
|
62 |
for bestSeller in bestSellers:
|
|
|
63 |
saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))
|
|
|
64 |
for d in saholicCatalogIds:
|
|
|
65 |
if d['source_id']!=4:
|
|
|
66 |
continue
|
|
|
67 |
dealsCatalogIds.append(long(d['identifier'].strip()))
|
| 14253 |
kshitij.so |
68 |
# dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':0}}))
|
|
|
69 |
# for deal in dealFlagged:
|
|
|
70 |
# if not (deal['_id']) in dealsCatalogIds:
|
|
|
71 |
# dealsCatalogIds.append(long(deal['identifier'].strip()))
|
| 13828 |
kshitij.so |
72 |
items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
|
|
|
73 |
for item in items:
|
|
|
74 |
temp = []
|
|
|
75 |
if not itemCatalogMap.has_key(item.catalog_item_id):
|
|
|
76 |
temp.append(item)
|
| 14179 |
kshitij.so |
77 |
print "****",item.catalog_item_id
|
| 13828 |
kshitij.so |
78 |
itemCatalogMap[item.catalog_item_id] = temp
|
|
|
79 |
else:
|
|
|
80 |
val = itemCatalogMap.get(item.catalog_item_id)
|
|
|
81 |
for l in val:
|
|
|
82 |
temp.append(l)
|
|
|
83 |
temp.append(item)
|
|
|
84 |
itemCatalogMap[item.catalog_item_id] = temp
|
|
|
85 |
|
| 14130 |
kshitij.so |
86 |
for saholicCatalogId in bestSellers:
|
| 14129 |
kshitij.so |
87 |
if saholicCatalogId['source_id']!=4:
|
|
|
88 |
continue
|
| 13916 |
kshitij.so |
89 |
d_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))
|
| 13828 |
kshitij.so |
90 |
available_price = None
|
| 14179 |
kshitij.so |
91 |
in_stock = 0
|
|
|
92 |
if d_items is not None:
|
| 13828 |
kshitij.so |
93 |
for d_item in d_items:
|
| 14179 |
kshitij.so |
94 |
in_stock = 0
|
| 13828 |
kshitij.so |
95 |
if d_item.status == 3:
|
|
|
96 |
in_stock =1
|
| 14179 |
kshitij.so |
97 |
else:
|
|
|
98 |
continue
|
|
|
99 |
if dealsMap.get(d_item.id) is not None:
|
|
|
100 |
available_price = dealsMap.get(d_item.id).dealPrice
|
|
|
101 |
if (available_price !=None):
|
| 13828 |
kshitij.so |
102 |
break
|
| 14179 |
kshitij.so |
103 |
if (available_price is None):
|
|
|
104 |
in_stock = 0
|
|
|
105 |
if d_items is not None:
|
|
|
106 |
for d_item in d_items:
|
|
|
107 |
if d_item.status == 3:
|
|
|
108 |
available_price = d_item.sellingPrice
|
|
|
109 |
in_stock =1
|
|
|
110 |
break
|
| 13828 |
kshitij.so |
111 |
print long(saholicCatalogId['identifier'])
|
|
|
112 |
print in_stock
|
|
|
113 |
print available_price
|
|
|
114 |
print dealsMap.get(d_item.id)
|
|
|
115 |
print "++++++++++++++++++++++++++"
|
| 13850 |
kshitij.so |
116 |
if available_price > 0 or available_price is not None:
|
| 14127 |
kshitij.so |
117 |
get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':in_stock}}, multi=True)
|
| 13916 |
kshitij.so |
118 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price , 'in_stock':in_stock}}, multi=True)
|
| 13828 |
kshitij.so |
119 |
else:
|
| 14127 |
kshitij.so |
120 |
get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':in_stock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
|
| 13916 |
kshitij.so |
121 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock}}, multi=True)
|
| 13828 |
kshitij.so |
122 |
|
| 13916 |
kshitij.so |
123 |
try:
|
|
|
124 |
recomputeDeal(saholicCatalogId['skuBundleId'])
|
|
|
125 |
except:
|
|
|
126 |
print "Unable to compute deal for ",saholicCatalogId['skuBundleId']
|
|
|
127 |
|
| 14325 |
kshitij.so |
128 |
def populateNegativeDeals():
|
|
|
129 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
|
|
130 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
131 |
|
| 13916 |
kshitij.so |
132 |
def recomputeDeal(skuBundleId):
|
|
|
133 |
"""Lets recompute deal for this bundle"""
|
|
|
134 |
print "Recomputing for bundleId",skuBundleId
|
|
|
135 |
|
|
|
136 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
137 |
bestPrice = float("inf")
|
|
|
138 |
bestOne = None
|
|
|
139 |
bestSellerPoints = 0
|
|
|
140 |
toUpdate = []
|
|
|
141 |
for similarItem in similarItems:
|
| 14327 |
kshitij.so |
142 |
if mc.get("negative_deals") is None:
|
| 14325 |
kshitij.so |
143 |
populateNegativeDeals()
|
| 14326 |
kshitij.so |
144 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
| 13916 |
kshitij.so |
145 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
146 |
continue
|
|
|
147 |
if similarItem['available_price'] < bestPrice:
|
|
|
148 |
bestOne = similarItem
|
|
|
149 |
bestPrice = similarItem['available_price']
|
|
|
150 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
151 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
152 |
bestOne = similarItem
|
|
|
153 |
bestPrice = similarItem['available_price']
|
|
|
154 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
155 |
else:
|
|
|
156 |
pass
|
|
|
157 |
if bestOne is not None:
|
|
|
158 |
for similarItem in similarItems:
|
|
|
159 |
toUpdate.append(similarItem['_id'])
|
|
|
160 |
toUpdate.remove(bestOne['_id'])
|
|
|
161 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
162 |
if len(toUpdate) > 0:
|
|
|
163 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
|
|
164 |
|
| 13828 |
kshitij.so |
165 |
def main():
|
|
|
166 |
getPrivateDeals()
|
| 14259 |
kshitij.so |
167 |
try:
|
|
|
168 |
getItemsToUpdate()
|
|
|
169 |
finally:
|
|
|
170 |
session.close()
|
| 13828 |
kshitij.so |
171 |
|
|
|
172 |
if __name__=='__main__':
|
|
|
173 |
main()
|