Subversion Repositories SmartDukaan

Rev

Rev 17778 | Rev 17780 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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