Rev 14120 | Rev 14263 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
import pymongofrom elixir import *from shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Itemfrom shop2020.clients.InventoryClient import InventoryClientfrom dtr.utils.utils import to_java_datefrom datetime import datetime, timedeltaimport timeDataService.initialize(db_hostname='localhost')con = NoneSOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4}DISCOUNT_TYPE = {'MRP':1,'DP':2}LATEST_UPDATED_ITEMS = []now = datetime.now()class __SkuInfo:def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints, status, in_stock, maxprice, brand):self._id = _idself.skuBundleId = skuBundleIdself.category_id = category_idself.mrp = mrpself.available_price = available_priceself.source_id = source_idself.rank = rankself.maxNlc = maxNlcself.minNlc = minNlcself.schemeAmount = schemeAmountself.minDiscount = minDiscountself.maxDiscount = maxDiscountself.discountType = discountTypeself.dp = dpself.nlcPoints = nlcPointsself.bestSellerPoints = bestSellerPointsself.totalPoints = totalPointsself.status = statusself.in_stock = in_stockself.maxprice = maxpriceself.brand = branddef get_mongo_connection(host='localhost', port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef populateStuff():print "Inside populate"global LATEST_UPDATED_ITEMS"""Fetch latest updated items across portalsand calculate max and min R-Nlc"""offset= 0while(True):print "Fetching records offset %d and limit %d" %(offset,300)topSkus = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'updatedOn': { "$gt": to_java_date(now - timedelta(hours=4))} }, { 'source_id' : { "$in": SOURCE_MAP.values() } }] }).skip(offset).limit(300))if len((topSkus)) == 0:break#topSkus = collection.find( {'_id':664})for sku in topSkus:"""Fix this """#TODO Compute deal flags else where.info = __SkuInfo(sku['_id'], sku['skuBundleId'], sku['category_id'], sku['mrp'], sku['available_price'], sku['source_id'], sku['rank'], None, None, 0.0, None, \None, None, None, None, None, None, sku['status'], sku['in_stock'],None,sku['brand'].strip().upper())exceptionalNlc = list(get_mongo_connection().Catalog.ExceptionalNlc.find( {"$and" : [ {'sku':info._id}, {'overrideNlc':1} ]} ))if len(exceptionalNlc) > 0:"""Exceptional nlc found, no need to calculate max and min R-nlc"""info.maxNlc = exceptionalNlc[0]['maxNlc']info.minNlc = exceptionalNlc[0]['minNlc']info.maxprice = exceptionalNlc[0]['maxNlc']LATEST_UPDATED_ITEMS.append(info)continueskuSchemeDetails = list(get_mongo_connection().Catalog.SkuSchemeDetails.find( {'sku':info._id}).sort([('addedOn',pymongo.DESCENDING)]).limit(1))if len(skuSchemeDetails) > 0:"""Sku scheme details, populate scheme amount (Recently added)"""#TODO Add start date and end date of scehemsinfo.schemeAmount = float(skuSchemeDetails[0]['schemeAmount'])skuDealerPrices = list(get_mongo_connection().Catalog.SkuDealerPrices.find( {'sku':info._id} ) )if len(skuDealerPrices) > 0:info.dp = skuDealerPrices[0]['dp']skuDiscount = list(get_mongo_connection().Catalog.SkuDiscountInfo.find( {'sku':info._id} ) )if len(skuDiscount) > 0:"""Sku rule found, populate max , min Discount and discount type"""info.maxDiscount = skuDiscount[0]['max_discount']info.minDiscount = skuDiscount[0]['min_discount']info.discountType = DISCOUNT_TYPE.get(skuDiscount[0]['discountType'].upper())LATEST_UPDATED_ITEMS.append(info)continuecategoryDiscount = list(get_mongo_connection().Catalog.CategoryDiscount.find( {"$and" : [{'brand':sku['brand'].strip().upper()}, {'category_id':sku['category_id']} ]} ))if len(categoryDiscount) > 0:info.maxDiscount = categoryDiscount[0]['max_discount']info.minDiscount = categoryDiscount[0]['min_discount']info.discountType = DISCOUNT_TYPE.get(categoryDiscount[0]['discountType'].upper())LATEST_UPDATED_ITEMS.append(info)offset = offset + 300for lol in LATEST_UPDATED_ITEMS:print lol.__dict__def calculateNlc():global LATEST_UPDATED_ITEMSpopulated = 0while(populated <= len(LATEST_UPDATED_ITEMS)):inventory_client = InventoryClient().get_client()for obj in LATEST_UPDATED_ITEMS[populated:300+populated]:if obj.maxNlc > 0 and obj.minNlc > 0:continuesaholic_sku = list(get_mongo_connection().Catalog.MasterData.find( {"$and":[{'skuBundleId': obj.skuBundleId}, { 'source_id' : SOURCE_MAP.get('SAHOLIC')}] }))identifier = Noneif len(saholic_sku) > 0:identifier = saholic_sku[0]['identifier']if obj.discountType == DISCOUNT_TYPE.get('MRP'):if obj.mrp == 0:"""Now mrp is zero, so we have to use saholic MRP"""if identifier is not None:it = Item.query.filter_by(catalog_item_id=identifier).first()obj.mrp = it.mrpif obj.mrp > 0:obj.minNlc = obj.mrp - (obj.mrp * obj.maxDiscount/100) - obj.schemeAmountobj.maxNlc = obj.mrp - (obj.mrp * obj.minDiscount/100) - obj.schemeAmountobj.maxprice = obj.maxNlcelif obj.discountType == DISCOUNT_TYPE.get('DP'):if obj.dp == 0:"""Now dp is zero, so we have to use saholic minimum dp for item"""if identifier is not None:it = Item.query.filter_by(catalog_item_id=identifier).first()try:vendorPricing = inventory_client.getAllItemPricing(it.id)min_dp = min(pricing.dealerPrice for pricing in vendorPricing)obj.dp = min_dpexcept:passif obj.dp > 0:obj.minNlc = obj.dp - (obj.dp * obj.maxDiscount/100) - obj.schemeAmountobj.maxNlc = obj.dp - (obj.dp * obj.minDiscount/100) - obj.schemeAmountobj.maxprice = obj.maxNlcelse:"""No rule found, use saholic min nlc as max and min R-Nlc"""if identifier is not None:it = Item.query.filter_by(catalog_item_id=identifier).first()try:vendorPricing = inventory_client.getAllItemPricing(it.id)min_nlc = min(pricing.nlc for pricing in vendorPricing)obj.maxNlc = min_nlcobj.minNlc = min_nlcobj.maxprice = obj.maxNlcexcept:passpopulated = populated + 300time.sleep(10)def calculateNlcPoints():global LATEST_UPDATED_ITEMSfor sku in LATEST_UPDATED_ITEMS:if sku.maxNlc and sku.minNlc:"""Create map - TODO"""if sku.status == 2:eolWeight = .60else:eolWeight = 1.0if sku.category_id == 3:basePointPercentage = 5.0maxNlcPoints = 200elif sku.category_id == 5:basePointPercentage = 8.0maxNlcPoints = 150else:basePointPercentage = 10.0maxNlcPoints = 150discFromMinNlc = (sku.minNlc - sku.available_price)/sku.available_price *100discFromMaxNlc = (sku.maxNlc - sku.available_price)/sku.available_price *100if discFromMinNlc > 0:nlcPoints = 100/basePointPercentage * discFromMinNlcelif discFromMinNlc < 0 and discFromMaxNlc > 0:nlcPoints = 0else:nlcPoints = 100/basePointPercentage * discFromMinNlcif (min(nlcPoints,maxNlcPoints)) > 0:sku.nlcPoints = (min(nlcPoints,maxNlcPoints)) * eolWeightelse:sku.nlcPoints = (min(nlcPoints,maxNlcPoints))else:sku.nlcPoints = 0def commitData():global LATEST_UPDATED_ITEMSfor sku in LATEST_UPDATED_ITEMS:#get_mongo_connection().Catalog.Deals.update({'_id':sku._id},{'$set' : sku.__dict__},upsert=True,multi=True)get_mongo_connection().Catalog.Deals.update({'_id':sku._id},{"$set":sku.__dict__},upsert=True)def addBestSellerPoints():allItems = list(get_mongo_connection().Catalog.Deals.find({}))for sku in allItems:bestSellerPoints = list(get_mongo_connection().Catalog.BestSellerPoints.find( {"$and":[{'min_rank': { "$lte": sku['rank'] } }, {'max_rank': { "$gte": sku['rank'] } } , { 'category_id' : sku['category_id'] }, { 'source_id' : sku['source_id'] }] } ))if len(bestSellerPoints) > 0:print bestSellerPoints[0]['points']if (bestSellerPoints[0]['points']) > 0:sku['bestSellerPoints'] = (bestSellerPoints[0]['points']) * bestSellerPoints[0]['weightage']else:sku['bestSellerPoints'] = (bestSellerPoints[0]['points'])else:sku['bestSellerPoints'] = -100#sku['totalPoints'] = sku['bestSellerPoints'] + sku['nlcPoints']get_mongo_connection().Catalog.Deals.update({'_id':sku['_id']},{'$set':{'bestSellerPoints':sku['bestSellerPoints']}},multi=False)for sku in allItems:deal_item = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':sku['skuBundleId']}).sort('bestSellerPoints',pymongo.DESCENDING).limit(1))sku['catalogBestSellerPoints'] = deal_item[0]['bestSellerPoints']sku['totalPoints'] = sku['catalogBestSellerPoints'] + sku['nlcPoints']get_mongo_connection().Catalog.Deals.update({'_id':sku['_id']},{'$set':{'catalogBestSellerPoints':sku['catalogBestSellerPoints'],'totalPoints':sku['totalPoints']}},multi=False)def elimiateSimilarDeals():allItems = get_mongo_connection().Catalog.Deals.find().distinct('skuBundleId')for skuBundleId in allItems:print skuBundleIdsimilarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))bestPrice = float("inf")bestOne = NonebestSellerPoints = 0toUpdate = []for similarItem in similarItems:if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})continueif similarItem['available_price'] < bestPrice:bestOne = similarItembestPrice = similarItem['available_price']bestSellerPoints = similarItem['bestSellerPoints']elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:bestOne = similarItembestPrice = similarItem['available_price']bestSellerPoints = similarItem['bestSellerPoints']else:passif bestOne is not None:for similarItem in similarItems:toUpdate.append(similarItem['_id'])toUpdate.remove(bestOne['_id'])get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})if len(toUpdate) > 0:get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)def main():populateStuff()calculateNlc()calculateNlcPoints()commitData()addBestSellerPoints()elimiateSimilarDeals()if __name__=='__main__':main()