Rev 22307 | Rev 22310 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *from shop2020.clients.InventoryClient import InventoryClientfrom shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Item, Tag_Listing, \Tag_Ranking, PrivateDealsfrom shop2020.thriftpy.model.v1.catalog.ttypes import statusfrom sqlalchemy.sql.expression import or_, descfrom sqlalchemy.sql.functions import nowimport jsonimport optparseimport osimport pymongoimport pysolrimport shutilimport traceback#import pymongo#import pysolrparser = optparse.OptionParser()parser.add_option("-d", "--d", dest="dbHost",default="127.0.0.1",type="string", help="The HOST where the mysql server is running",metavar="DBHOST")parser.add_option("-s", "--s", dest="solrPath",default="http://localhost:8984/solr/collection2",type="string", help="Complete solr path",metavar="SOLRHOST")parser.add_option("-m", "--m", dest="mongoHost",default="http://localhost:8984/solr/collection2",type="string", help="Complete solr path",metavar="HOST")(options, args) = parser.parse_args()DataService.initialize(db_hostname=options.hostname)solr = pysolr.Solr(options.solrPath, timeout=10)def 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 conclass __SkuInfo:def __init__(self, id, ids, brand, model_name, category_id, subCategoryId,thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \category, subCategory):#Skubundle idself.id = id#_id of skusself.ids = idsself.brand = brandself.model_name = model_nameself.category_id = category_idself.subCategoryId = subCategoryIdself.thumbnail = thumbnailself.title= titleself.brand_synonyms = brand_synonymsself.model_name_synonyms = model_name_synonymsself.source_product_names = source_product_namesself.category = categoryself.subCategory = subCategorydef toJSON(self):return json.dumps(self, default=lambda o: o.__dict__,sort_keys=True, indent=4)class __Catalog:def __init__(self, id, rank, title, items):#Skubundle idself.id = idself.rank = rankself.title = titleself._childDocuments_ = itemsdef toJSON(self):return json.dumps(self, default=lambda o: o.__dict__)class __Item:def __init__(self, id, color, tags):self.id = idself.color = colorself._childDocuments_ = tagsdef toJSON(self):return json.dumps(self, default=lambda o: o.__dict__)class __Tag:def __init__(self, id, sellingPrice, mop):self.id = idself.sellingPrice = sellingPriceself.mop = mop#solr = pysolr.Solr(options.solrPath, timeout=10)def todict(obj, classkey=None):if isinstance(obj, dict):data = {}for (k, v) in obj.items():data[k] = todict(v, classkey)return dataelif hasattr(obj, "_ast"):return todict(obj._ast())elif hasattr(obj, "__iter__"):return [todict(v, classkey) for v in obj]elif hasattr(obj, "__dict__"):data = dict([(key, todict(value, classkey))for key, value in obj.__dict__.iteritems()if not callable(value) and not key.startswith('_')])if classkey is not None and hasattr(obj, "__class__"):data[classkey] = obj.__class__.__name__return dataelse:return objdef populateTagItems():tagRankingList = session.query(Tag_Ranking.catalogItemId).order_by(desc(Tag_Ranking.rankPoints)).all()catalogMap = {}#stmt = session.query(PrivateDeals).filter_by(isActive=1).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate)).subquery()#query = session.query(Item, privateDealAlias.dealPrice).outerjoin((privateDealAlias, Item.id==privateDealAlias.item_id)).filter(Item.status != status.PHASED_OUT)tuples = session.query(Tag_Listing, Item).join((Item, Item.id==Tag_Listing.item_id)).filter(or_(Item.status==status.ACTIVE, Item.status==status.PAUSED_BY_RISK)).filter(Tag_Listing.active==True)projection={'thumbnailImageUrl':1}for tag, item in tuples:if not catalogMap.has_key(item.catalog_item_id):catalogObj = {}catalogObj['title'] = " ".join(filter(None, [item.brand, item.model_name, item.model_number]))catalogObj['identifier'] = item.catalog_item_idcatalogObj['items'] = {}filterMap = {"_id":item.catalog_item_id}#Dont include it catalog not availabletry:catalogObj['imageUrl'] = get_mongo_connection(options.mongoHost).siteContent.find_one(filterMap, projection)['thumbnailImageUrl']except:continuetry:catalogObj['rank'] = tagRankingList.index(item.catalog_item_id)except:#A very big numbercatalogObj['rank'] = 500000catalogObj['categoryId'] = 3 if item.category in [10006, 10009] else 6catalogMap[item.catalog_item_id] = catalogObjcatalogObj = catalogMap.get(item.catalog_item_id)if not catalogObj['items'].has_key(item.id):catalogObj['items'][item.id] = {'color': item.color, 'tagPricing':[]}itemMap = catalogObj['items'].get(item.id)itemMap['tagPricing'].append(tag)catalogObjs = []for catalogId, catalogMap in catalogMap.iteritems():itemsMap = catalogMap['items']itemObjs = []for itemId, itemMap in itemsMap.iteritems():tags = itemMap['tagPricing']for tag in tags:itemObj = {'id':('itemtag-%s-%s'%(itemId, tag.tag_id)), 'color_s':itemMap['color'], 'itemId_i': itemId, 'tagId_i':tag.tag_id,'mop_f': tag.mop, 'sellingPrice_f': tag.selling_price}itemObjs.append(itemObj)catalogObj = {'id':'catalog' + str(catalogId), 'rank_i':catalogMap['rank'], 'title_s': catalogMap['title'], '_childDocuments_':itemObjs, 'catalogId_i':catalogId}catalogObjs.append(catalogObj)print catalogObjssolr.delete(q='*:*')solr.add(catalogObjs)#items = Item.query.filter(Item.risky==True).filter(or_(Item.status==status.ACTIVE)).all()# global con# if con is None:# print "Establishing connection %s host and port %d" %(host,port)# try:# con = pymongo.MongoClient(host, port)# except Exception, e:# print e# return None# return condef pushData():#rankPoints = populateRankPoints()populateTagItems()#orderedMap = orderIt#convertToSolrDoc()if __name__=='__main__':pushData()