| 17772 |
kshitij.so |
1 |
from elixir import *
|
|
|
2 |
from dtr.storage.MemCache import MemCache
|
|
|
3 |
from shop2020.model.v1.catalog.impl import DataService as CatalogDataService
|
|
|
4 |
from shop2020.model.v1.inventory.impl import DataService as InventoryDataService
|
| 17777 |
kshitij.so |
5 |
from shop2020.model.v1.catalog.impl.DataService import Item, PrivateDeals
|
| 17772 |
kshitij.so |
6 |
from shop2020.model.v1.inventory.impl.DataService import ItemAvailabilityCache
|
|
|
7 |
from dtr.utils.utils import get_mongo_connection, SOURCE_MAP
|
|
|
8 |
import optparse
|
|
|
9 |
import traceback
|
| 17993 |
kshitij.so |
10 |
from datetime import datetime
|
|
|
11 |
from shop2020.clients.InventoryClient import InventoryClient
|
| 17772 |
kshitij.so |
12 |
|
|
|
13 |
parser = optparse.OptionParser()
|
|
|
14 |
parser.add_option("-H", "--host", dest="hostname",
|
|
|
15 |
default="localhost",
|
|
|
16 |
type="string", help="The HOST where the DB server is running",
|
|
|
17 |
metavar="host")
|
|
|
18 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
19 |
default="localhost",
|
|
|
20 |
type="string", help="The HOST where the mongo server is running",
|
|
|
21 |
metavar="mongo_host")
|
|
|
22 |
|
|
|
23 |
(options, args) = parser.parse_args()
|
|
|
24 |
|
|
|
25 |
bundleMap = {}
|
|
|
26 |
inventoryMap = {}
|
|
|
27 |
memCon = None
|
|
|
28 |
maxQuantity = 20
|
| 17993 |
kshitij.so |
29 |
flaggedItems = []
|
| 17772 |
kshitij.so |
30 |
|
| 17993 |
kshitij.so |
31 |
def get_inventory_client():
|
|
|
32 |
ic = InventoryClient("inventory_service_server_host2","inventory_service_server_port2").get_client()
|
|
|
33 |
return ic
|
|
|
34 |
|
| 17772 |
kshitij.so |
35 |
def get_memcache_connection(host='127.0.0.1'):
|
|
|
36 |
global memCon
|
|
|
37 |
if memCon is None:
|
|
|
38 |
print "Establishing connection %s host" %(host)
|
|
|
39 |
try:
|
|
|
40 |
memCon = MemCache(host)
|
|
|
41 |
except Exception, e:
|
|
|
42 |
print e
|
|
|
43 |
return None
|
|
|
44 |
return memCon
|
|
|
45 |
|
|
|
46 |
def populateSaholicBundles():
|
|
|
47 |
global bundleMap
|
|
|
48 |
allSaholicBundles = get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'source_id':SOURCE_MAP.get('SAHOLIC')})
|
|
|
49 |
for item in allSaholicBundles:
|
|
|
50 |
try:
|
|
|
51 |
if item.get('identifier') is not None or item.get('identifier').strip()!="":
|
|
|
52 |
#validItems.append({'_id':item.get('_id'),'catalog_item_id':long(item.get('identifier'))})
|
|
|
53 |
bundleMap[long(item.get('identifier'))] = []
|
|
|
54 |
|
|
|
55 |
else:
|
|
|
56 |
print "Identifier not valid for %d"%(item.get('_id'))
|
| 17986 |
kshitij.so |
57 |
print item.get('identifier')
|
| 17772 |
kshitij.so |
58 |
except Exception as e:
|
|
|
59 |
print e
|
|
|
60 |
print "Exception Identifier not valid for %d"%(item.get('_id'))
|
|
|
61 |
|
|
|
62 |
def addItemIdInfo():
|
|
|
63 |
global bundleMap
|
| 17933 |
kshitij.so |
64 |
try:
|
|
|
65 |
CatalogDataService.initialize(db_hostname=options.hostname,setup=False)
|
|
|
66 |
totalLength = len(bundleMap.keys())
|
|
|
67 |
fetch =100
|
|
|
68 |
start = 0
|
|
|
69 |
toBreak = False
|
|
|
70 |
while(True):
|
|
|
71 |
print start
|
|
|
72 |
print fetch
|
|
|
73 |
if fetch > totalLength:
|
|
|
74 |
fetch = totalLength
|
|
|
75 |
toBreak = True
|
|
|
76 |
|
|
|
77 |
item_list = bundleMap.keys()[start:fetch]
|
|
|
78 |
items = session.query(Item,PrivateDeals).outerjoin((PrivateDeals,Item.id==PrivateDeals.item_id)).filter(Item.catalog_item_id.in_(item_list)).all()
|
|
|
79 |
for i in items:
|
|
|
80 |
d_item = i[0]
|
|
|
81 |
d_privatedeal = i[1]
|
|
|
82 |
tempList = bundleMap.get(d_item.catalog_item_id)
|
|
|
83 |
sellingPrice = d_item.sellingPrice
|
|
|
84 |
sellingPriceType = "Normal"
|
|
|
85 |
if d_privatedeal is not None and d_privatedeal.isActive==1 and d_privatedeal.startDate < datetime.now() and d_privatedeal.endDate > datetime.now() and d_privatedeal.dealPrice >0:
|
|
|
86 |
sellingPrice = d_privatedeal.dealPrice
|
|
|
87 |
sellingPriceType = "Private deal price"
|
|
|
88 |
tempList.append({'item_id':d_item.id,'color':d_item.color,'sellingPrice':sellingPrice,'sellingPriceType':sellingPriceType})
|
|
|
89 |
if toBreak:
|
|
|
90 |
print "Breaking"
|
|
|
91 |
break
|
|
|
92 |
start = fetch
|
|
|
93 |
fetch = start + fetch
|
|
|
94 |
finally:
|
|
|
95 |
session.close()
|
| 17772 |
kshitij.so |
96 |
|
|
|
97 |
def addInfoToMemCache():
|
|
|
98 |
for k, v in bundleMap.iteritems():
|
|
|
99 |
for item in v:
|
|
|
100 |
availability = inventoryMap.get(item.get('item_id'))
|
|
|
101 |
if availability is None:
|
|
|
102 |
availability = 0
|
|
|
103 |
item['availability'] = availability
|
|
|
104 |
if availability < maxQuantity:
|
|
|
105 |
item['maxQuantity'] = availability
|
|
|
106 |
else:
|
|
|
107 |
item['maxQuantity'] = maxQuantity
|
|
|
108 |
temp = sorted(v, key = lambda x: (x['availability']),reverse=True)
|
|
|
109 |
mc = get_memcache_connection(host=options.mongoHost)
|
| 17986 |
kshitij.so |
110 |
mc.set(str("item_availability_"+str(k)), temp, 60*60)
|
| 17772 |
kshitij.so |
111 |
|
| 17993 |
kshitij.so |
112 |
def flagNoAvailableItems():
|
|
|
113 |
global flaggedItems
|
|
|
114 |
for k, v in bundleMap.iteritems():
|
|
|
115 |
for item in v:
|
|
|
116 |
availability = inventoryMap.get(item.get('item_id'))
|
|
|
117 |
if availability is None:
|
|
|
118 |
flaggedItems.append(item.get('item_id'))
|
|
|
119 |
|
| 17772 |
kshitij.so |
120 |
|
|
|
121 |
def fetchItemAvailablity():
|
|
|
122 |
global inventoryMap
|
| 17933 |
kshitij.so |
123 |
try:
|
|
|
124 |
InventoryDataService.initialize(db_hostname=options.hostname,setup=False)
|
|
|
125 |
allInventory = session.query(ItemAvailabilityCache).filter(ItemAvailabilityCache.sourceId==1).all()
|
|
|
126 |
for itemInventory in allInventory:
|
|
|
127 |
inventoryMap[itemInventory.itemId] = itemInventory.totalAvailability
|
|
|
128 |
finally:
|
|
|
129 |
session.close()
|
| 17772 |
kshitij.so |
130 |
|
|
|
131 |
if __name__ == '__main__':
|
|
|
132 |
populateSaholicBundles()
|
|
|
133 |
addItemIdInfo()
|
|
|
134 |
fetchItemAvailablity()
|
| 17993 |
kshitij.so |
135 |
flagNoAvailableItems()
|
|
|
136 |
if len(flaggedItems) > 0:
|
|
|
137 |
print "Flagged items ",flaggedItems
|
|
|
138 |
get_inventory_client().updateItemAvailabilityForItemIds(flaggedItems)
|
|
|
139 |
fetchItemAvailablity()
|
| 17772 |
kshitij.so |
140 |
addInfoToMemCache()
|
|
|
141 |
mc = get_memcache_connection(host=options.mongoHost)
|
|
|
142 |
print mc.get("item_availability_1005556")
|