Subversion Repositories SmartDukaan

Rev

Rev 18194 | Rev 18196 | 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
from shop2020.config.client.ConfigClient import ConfigClient
4
import optparse
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",
16
                      default="localhost",
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
 
18194 kshitij.so 24
subCategoryList = [19, 20, 27]
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
 
30
    def __init__(self, id, title, category_id, subCategoryId, subCategory):
31
        self.id = id
32
        self.title = title
33
        self.category_id = category_id
34
        self.subCategoryId = subCategoryId
35
        self.subCategory = subCategory
36
 
37
def main():
38
    added = []
39
    items = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'subCategoryId':{"$in":subCategoryList}}))
40
    for item in items:
41
        if item['skuBundleId'] in added:
42
            continue
43
        title = xstr(item['brand'])+" "+xstr(item['model_name'])
44
        s_info = __SkuInfo(int(item['skuBundleId']),title,int(item['category_id']),int(item['subCategoryId']),item['subCategory'])
45
        es.index(index='my_index', doc_type='my_type', id=s_info.id,body=s_info.__dict__)
46
        added.append(int(item['skuBundleId']))
47
 
48
def validateListings():
49
    offset , limit = 0, 100
50
 
51
    body = {
52
            "query" : {
53
        "match_all" : {}
54
        }
55
            }
56
    toDelete = []
57
    while(True):
58
        result = es.search("my_index", "my_type", body,from_=offset,size=limit)
59
        print result
60
        if len(result['hits']['hits']) > 0:
61
            for x in result['hits']['hits']:
62
                skuBundleId =x['_source']['id']
63
                subCategoryId = x['_source']['subCategoryId']
64
                category_id = x['_source']['category_id']
65
                exist = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'skuBundleId':skuBundleId}))
66
                if len(exist) ==0:
67
                    toDelete.append(skuBundleId)
68
                else:
69
                    for item in exist:
70
                        if item['subCategoryId']!=subCategoryId or item['category_id'] !=category_id:
71
                            if skuBundleId not in toDelete:
72
                                print "Deleting item ",skuBundleId
73
                                toDelete.append(skuBundleId)
74
            offset = offset+limit
75
        else:
76
            break
77
 
78
    for id in toDelete:
79
        print "Deleting id ",id
80
        es.delete(index='my_index', doc_type='my_type', id=id)
18087 kshitij.so 81
 
17935 kshitij.so 82
 
83
if __name__ == '__main__':
84
    main()
85
    validateListings()