Subversion Repositories SmartDukaan

Rev

Rev 17781 | Rev 17986 | 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
17933 kshitij.so 60
    try:
61
        CatalogDataService.initialize(db_hostname=options.hostname,setup=False)
62
        totalLength = len(bundleMap.keys())
63
        fetch =100
64
        start = 0
65
        toBreak = False
66
        while(True):
67
            print start
68
            print fetch
69
            if fetch > totalLength:
70
                fetch = totalLength
71
                toBreak = True
72
 
73
            item_list = bundleMap.keys()[start:fetch]
74
            items = session.query(Item,PrivateDeals).outerjoin((PrivateDeals,Item.id==PrivateDeals.item_id)).filter(Item.catalog_item_id.in_(item_list)).all()
75
            print items
76
            for i in items:
77
                d_item = i[0]
78
                d_privatedeal = i[1]
79
                tempList = bundleMap.get(d_item.catalog_item_id)
80
                sellingPrice = d_item.sellingPrice
81
                sellingPriceType = "Normal"
82
                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:
83
                    sellingPrice = d_privatedeal.dealPrice
84
                    sellingPriceType = "Private deal price"
85
                tempList.append({'item_id':d_item.id,'color':d_item.color,'sellingPrice':sellingPrice,'sellingPriceType':sellingPriceType})
86
            if toBreak:
87
                print "Breaking"
88
                break
89
            start = fetch
90
            fetch = start + fetch
91
    finally:
92
        session.close()
17772 kshitij.so 93
 
94
def addInfoToMemCache():
95
    for k, v in bundleMap.iteritems():
96
        for item in v:
97
            availability = inventoryMap.get(item.get('item_id'))
98
            if availability is None:
99
                availability = 0
100
            item['availability'] = availability
101
            if availability < maxQuantity:
102
                item['maxQuantity'] = availability
103
            else:
104
                item['maxQuantity'] = maxQuantity
105
        temp = sorted(v, key = lambda x: (x['availability']),reverse=True)
106
        mc = get_memcache_connection(host=options.mongoHost)
107
        mc.set(str("item_availability_"+str(k)), temp, 24*60*60)
108
 
109
 
110
def fetchItemAvailablity():
111
    global inventoryMap
17933 kshitij.so 112
    try:
113
        InventoryDataService.initialize(db_hostname=options.hostname,setup=False)
114
        allInventory = session.query(ItemAvailabilityCache).filter(ItemAvailabilityCache.sourceId==1).all()
115
        for itemInventory in allInventory:
116
            inventoryMap[itemInventory.itemId] = itemInventory.totalAvailability
117
    finally:
118
        session.close()
17772 kshitij.so 119
 
120
if __name__ == '__main__':
121
    populateSaholicBundles()
122
    addItemIdInfo()
123
    fetchItemAvailablity()
124
    addInfoToMemCache()
125
    mc = get_memcache_connection(host=options.mongoHost)
126
    print mc.get("item_availability_1005556")