Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
17935 kshitij.so 1
from elasticsearch import Elasticsearch
2
from dtr.utils.utils import get_mongo_connection
3
import optparse
19572 kshitij.so 4
from pymongo import ASCENDING
17935 kshitij.so 5
 
6
parser = optparse.OptionParser()
7
parser.add_option("-m", "--m", dest="mongoHost",
8
                      default="localhost",
9
                      type="string", help="The HOST where the mongo server is running",
10
                      metavar="HOST")
18195 kshitij.so 11
parser.add_option("-e", "--e", dest="elastic_search_host",
12
                      default="localhost",
13
                      type="string", help="The HOST where the elastic server is running",
14
                      metavar="HOST")
15
parser.add_option("-p", "--p", dest="elastic_search_port",
18197 kshitij.so 16
                      default="9200",
18195 kshitij.so 17
                      type="string", help="The PORT where the elastic server is running",
18
                      metavar="HOST")
17935 kshitij.so 19
 
18195 kshitij.so 20
 
17935 kshitij.so 21
(options, args) = parser.parse_args()
22
 
23
 
19542 kshitij.so 24
subCategoryList = [19, 20, 27, 29, 33, 28]
18195 kshitij.so 25
es = Elasticsearch([{'host': options.elastic_search_host, 'port': options.elastic_search_port}])
17935 kshitij.so 26
xstr = lambda s: s or ""
27
 
28
class __SkuInfo:
29
 
19571 kshitij.so 30
    def __init__(self, id, title, category_id, subCategoryId, subCategory, internalRank):
17935 kshitij.so 31
        self.id = id
32
        self.title = title
33
        self.category_id = category_id
34
        self.subCategoryId = subCategoryId
35
        self.subCategory = subCategory
19571 kshitij.so 36
        self.internalRank = internalRank
17935 kshitij.so 37
 
38
def main():
39
    added = []
40
    items = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'subCategoryId':{"$in":subCategoryList}}))
41
    for item in items:
42
        if item['skuBundleId'] in added:
43
            continue
19572 kshitij.so 44
        deal_obj = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':item['skuBundleId'],'internalRank':{"$gt":0}}).sort([('internalRank',ASCENDING)]))
19136 kshitij.so 45
        if len(deal_obj) == 0:
19571 kshitij.so 46
            internalRank = 999999
19136 kshitij.so 47
        else:
19571 kshitij.so 48
            internalRank = deal_obj[0]['internalRank']
17935 kshitij.so 49
        title = xstr(item['brand'])+" "+xstr(item['model_name'])
19571 kshitij.so 50
        s_info = __SkuInfo(int(item['skuBundleId']),title,int(item['category_id']),int(item['subCategoryId']),item['subCategory'], internalRank)
17935 kshitij.so 51
        es.index(index='my_index', doc_type='my_type', id=s_info.id,body=s_info.__dict__)
52
        added.append(int(item['skuBundleId']))
53
 
54
def validateListings():
55
    offset , limit = 0, 100
56
 
57
    body = {
58
            "query" : {
59
        "match_all" : {}
60
        }
61
            }
62
    toDelete = []
63
    while(True):
64
        result = es.search("my_index", "my_type", body,from_=offset,size=limit)
65
        print result
66
        if len(result['hits']['hits']) > 0:
67
            for x in result['hits']['hits']:
68
                skuBundleId =x['_source']['id']
69
                subCategoryId = x['_source']['subCategoryId']
70
                category_id = x['_source']['category_id']
71
                exist = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'skuBundleId':skuBundleId}))
72
                if len(exist) ==0:
73
                    toDelete.append(skuBundleId)
74
                else:
75
                    for item in exist:
76
                        if item['subCategoryId']!=subCategoryId or item['category_id'] !=category_id:
77
                            if skuBundleId not in toDelete:
78
                                print "Deleting item ",skuBundleId
79
                                toDelete.append(skuBundleId)
80
            offset = offset+limit
81
        else:
82
            break
83
 
84
    for id in toDelete:
85
        print "Deleting id ",id
86
        es.delete(index='my_index', doc_type='my_type', id=id)
18087 kshitij.so 87
 
17935 kshitij.so 88
 
89
if __name__ == '__main__':
90
    main()
91
    validateListings()