Subversion Repositories SmartDukaan

Rev

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