| 13107 |
kshitij.so |
1 |
import pymongo
|
|
|
2 |
from datetime import datetime, timedelta, time, date
|
|
|
3 |
from shop2020.thriftpy.model.v1.order.ttypes import AmazonFCWarehouseLocation
|
| 13108 |
kshitij.so |
4 |
from shop2020.utils.Utils import to_java_date, to_py_date
|
| 13480 |
kshitij.so |
5 |
from shop2020.thriftpy.model.v1.order.ttypes import AmazonFbaSalesSnapshot, AmazonHourlySaleSnapshot, \
|
|
|
6 |
AmazonFbaOrderItem
|
| 13530 |
kshitij.so |
7 |
import operator
|
| 13107 |
kshitij.so |
8 |
|
| 13480 |
kshitij.so |
9 |
|
| 13107 |
kshitij.so |
10 |
con = None
|
| 16667 |
manish.sha |
11 |
prefix = {'FBA':0,'FBB':1,'FBG':2, 'FBD':3}
|
| 13107 |
kshitij.so |
12 |
|
|
|
13 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
14 |
global con
|
|
|
15 |
if con is None:
|
|
|
16 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
17 |
try:
|
|
|
18 |
con = pymongo.MongoClient(host, port)
|
|
|
19 |
except Exception, e:
|
|
|
20 |
print e
|
|
|
21 |
return None
|
|
|
22 |
return con
|
|
|
23 |
|
|
|
24 |
def to_amazonFbaSaleSnapshot(saleSnapshot):
|
|
|
25 |
amazonFbaSaleSnapshot = AmazonFbaSalesSnapshot(dateOfSale=0, item_id=0, totalOrderCount=0, amazonFbaInventory=0, isOutOfStock=False, salePrice=0, minFbaPrice=0, minMfnPrice=0, totalSale=0, promotionSale=0, promotionOrderCount=0, ourPrice=0, minFbaPriceSnapshotDate=0, minMfnPriceSnapshotDate=0, ourPriceSnapshotDate=0, salePriceSnapshotDate=0, fcLocation=None)
|
|
|
26 |
if saleSnapshot is None:
|
|
|
27 |
return amazonFbaSaleSnapshot
|
|
|
28 |
amazonFbaSaleSnapshot.__dict__.update(saleSnapshot)
|
|
|
29 |
return amazonFbaSaleSnapshot
|
|
|
30 |
|
| 13109 |
kshitij.so |
31 |
def to_amazonFbaHourlySnapshot(saleSnapshot):
|
|
|
32 |
amazonHourlySaleSnapshot = AmazonHourlySaleSnapshot(snapshotTime=0, item_id=0, totalOrderCount=0, amazonFbaInventory=0, isOutOfStock=False, totalSale=0, promotionSale=0, promotionOrderCount=0, fcLocation=None)
|
|
|
33 |
if saleSnapshot is None:
|
|
|
34 |
return amazonHourlySaleSnapshot
|
|
|
35 |
amazonHourlySaleSnapshot.__dict__.update(saleSnapshot)
|
|
|
36 |
return amazonHourlySaleSnapshot
|
|
|
37 |
|
| 13480 |
kshitij.so |
38 |
def to_amazonFbaOrderItem(orderData):
|
|
|
39 |
amazonFbaOrderItem = AmazonFbaOrderItem(amazonOrderId=None, purchaseDate=None, orderStatus=None, item_id=None, fcLocation=None, totalAmount=None, promotionDiscount=None, quantity=None)
|
|
|
40 |
if orderData is None:
|
|
|
41 |
return amazonFbaOrderItem
|
|
|
42 |
amazonFbaOrderItem.__dict__.update(orderData)
|
|
|
43 |
return amazonFbaOrderItem
|
|
|
44 |
|
| 13107 |
kshitij.so |
45 |
def getAmazonFbaSalesLatestSnapshotForItemLocationWise(item_id,fcLocation):
|
|
|
46 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonDailySaleSnapshot
|
|
|
47 |
list = []
|
|
|
48 |
list.append({"dateOfSale": to_java_date(datetime.combine((datetime.now()-timedelta(days=1)).date(), time(00,00,00)))})
|
|
|
49 |
list.append({"item_id":item_id})
|
|
|
50 |
list.append({"fcLocation":AmazonFCWarehouseLocation._VALUES_TO_NAMES.get(fcLocation)})
|
|
|
51 |
r = collection.find({"$and":list}).sort([('dateOfSale',pymongo.DESCENDING)]).limit(1)
|
|
|
52 |
if r.count() > 0:
|
|
|
53 |
return to_amazonFbaSaleSnapshot(r.next())
|
|
|
54 |
else:
|
|
|
55 |
return to_amazonFbaSaleSnapshot(None)
|
| 13108 |
kshitij.so |
56 |
|
|
|
57 |
def getInStockAmazonFbaSalesLatestSnapshotForItemLocationWise(item_id,fcLocation,outOfStockTime):
|
|
|
58 |
fbasalessnapshot = []
|
|
|
59 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonDailySaleSnapshot
|
|
|
60 |
toDate = datetime.now()-timedelta(days = 1)
|
|
|
61 |
fromDate = outOfStockTime
|
|
|
62 |
list = []
|
|
|
63 |
list.append({"item_id":item_id})
|
|
|
64 |
list.append({"fcLocation":AmazonFCWarehouseLocation._VALUES_TO_NAMES.get(fcLocation)})
|
|
|
65 |
query = {}
|
|
|
66 |
query['$gt'] = fromDate
|
|
|
67 |
query['$lte'] = to_java_date(datetime.combine(toDate.date(), time(00,00,00)))
|
|
|
68 |
list.append({"dateOfSale":query})
|
|
|
69 |
cursor = collection.find({"$and":list}).sort([('dateOfSale',pymongo.ASCENDING)])
|
|
|
70 |
totalSale = 0
|
|
|
71 |
noOfDays = 0
|
|
|
72 |
for sale in cursor:
|
|
|
73 |
totalSale = totalSale + sale.get('totalOrderCount')
|
|
|
74 |
noOfDays = noOfDays + 1
|
|
|
75 |
return totalSale, noOfDays
|
| 13107 |
kshitij.so |
76 |
|
|
|
77 |
def getAmazonFbaSalesSnapshotForDays(interval):
|
|
|
78 |
fbasalessnapshot = []
|
|
|
79 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonDailySaleSnapshot
|
|
|
80 |
toDate = datetime.now()-timedelta(days = 1)
|
|
|
81 |
fromDate = toDate-timedelta(days = interval)
|
|
|
82 |
query = {}
|
|
|
83 |
query['$gte'] = to_java_date(datetime.combine(fromDate.date(), time(00,00,00)))
|
|
|
84 |
query['$lte'] = to_java_date(datetime.combine(toDate.date(), time(00,00,00)))
|
|
|
85 |
cursor = collection.find({"dateOfSale":query}).sort([('dateOfSale',pymongo.ASCENDING)])
|
|
|
86 |
for sale in cursor:
|
|
|
87 |
fbasalessnapshot.append(to_amazonFbaSaleSnapshot(sale))
|
|
|
88 |
return fbasalessnapshot
|
|
|
89 |
|
| 13108 |
kshitij.so |
90 |
def getLastOutOfStock(item_id,fcLocation):
|
|
|
91 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonDailySaleSnapshot
|
|
|
92 |
list = []
|
|
|
93 |
list.append({"item_id":item_id})
|
|
|
94 |
list.append({"fcLocation":AmazonFCWarehouseLocation._VALUES_TO_NAMES.get(fcLocation)})
|
|
|
95 |
list.append({"isOutOfStock":True})
|
|
|
96 |
r = collection.find({"$and":list}).sort([('dateOfSale',pymongo.DESCENDING)]).limit(1)
|
|
|
97 |
if r.count() > 0:
|
|
|
98 |
lastOutOfStock = to_amazonFbaSaleSnapshot(r.next())
|
|
|
99 |
else:
|
|
|
100 |
lastOutOfStock = to_amazonFbaSaleSnapshot(None)
|
|
|
101 |
if lastOutOfStock.dateOfSale == 0:
|
|
|
102 |
return lastOutOfStock, 0, 0
|
|
|
103 |
else:
|
| 13130 |
kshitij.so |
104 |
totalSale, noOfDays =getInStockAmazonFbaSalesLatestSnapshotForItemLocationWise(item_id,fcLocation,lastOutOfStock.dateOfSale)
|
|
|
105 |
return lastOutOfStock, totalSale, noOfDays
|
| 13109 |
kshitij.so |
106 |
|
|
|
107 |
def getLatestHourlySnapshot():
|
|
|
108 |
fbaHourlySnapshot = []
|
|
|
109 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonHourlySaleSnapshot
|
|
|
110 |
r = collection.find().sort([('snapshotTime',pymongo.DESCENDING)]).limit(1)
|
|
|
111 |
lastestSnapshotTime = r.next().get('snapshotTime')
|
|
|
112 |
cursor = collection.find({"snapshotTime":{"$in":[lastestSnapshotTime]}})
|
|
|
113 |
for sale in cursor:
|
|
|
114 |
fbaHourlySnapshot.append(to_amazonFbaHourlySnapshot(sale))
|
|
|
115 |
return fbaHourlySnapshot
|
| 13480 |
kshitij.so |
116 |
|
|
|
117 |
def getAmazonOrderData(startDate, endDate, offset, limit):
|
|
|
118 |
fbaOrderData = []
|
|
|
119 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonSaleData
|
|
|
120 |
list = []
|
|
|
121 |
query = {}
|
|
|
122 |
list.append({"orderStatus":"Shipped"})
|
|
|
123 |
query['$gte'] = to_java_date(startDate)
|
|
|
124 |
query['$lte'] = to_java_date(endDate)
|
|
|
125 |
list.append({"purchaseDate":query})
|
| 13530 |
kshitij.so |
126 |
print list
|
| 13480 |
kshitij.so |
127 |
if offset is None and limit is None:
|
|
|
128 |
cursor = collection.find({"$and":list}).sort([('purchaseDate',pymongo.ASCENDING)])
|
| 13530 |
kshitij.so |
129 |
print cursor.count()
|
| 13480 |
kshitij.so |
130 |
else:
|
|
|
131 |
cursor = collection.find({"$and":list}).skip(offset).limit(limit).sort([('purchaseDate',pymongo.ASCENDING)])
|
|
|
132 |
for orderData in cursor:
|
|
|
133 |
fbaOrderData.append(to_amazonFbaOrderItem(orderData))
|
|
|
134 |
return fbaOrderData
|
|
|
135 |
|
|
|
136 |
def getDistinctItemsFromOrderData(startDate, endDate):
|
|
|
137 |
item_ids = []
|
|
|
138 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonSaleData
|
|
|
139 |
list = []
|
|
|
140 |
query = {}
|
|
|
141 |
list.append({"orderStatus":"Shipped"})
|
|
|
142 |
query['$gte'] = to_java_date(startDate)
|
|
|
143 |
query['$lte'] = to_java_date(endDate)
|
|
|
144 |
list.append({"purchaseDate":query})
|
|
|
145 |
cursor = collection.find({"$and":list}).distinct('item_id')
|
|
|
146 |
for item_id in cursor:
|
|
|
147 |
item_ids.append(item_id)
|
|
|
148 |
return item_ids
|
|
|
149 |
|
|
|
150 |
def getOrderByAmazonOrderIdAndSku(amazonOrderId, sku):
|
|
|
151 |
list = []
|
|
|
152 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonSaleData
|
|
|
153 |
list.append({"amazonOrderId": amazonOrderId})
|
|
|
154 |
list.append({"item_id":int(sku[3:])})
|
|
|
155 |
list.append({"fcLocation":AmazonFCWarehouseLocation._VALUES_TO_NAMES.get(prefix.get(sku[0:3]))})
|
|
|
156 |
r = collection.find({"$and":list})
|
|
|
157 |
if r.count() > 1:
|
|
|
158 |
raise
|
|
|
159 |
elif r.count()==0:
|
|
|
160 |
raise
|
|
|
161 |
else:
|
|
|
162 |
return to_amazonFbaOrderItem(r.next())
|
| 13530 |
kshitij.so |
163 |
def getAmazonOrderDataByItemId(startDate, endDate, offset, limit, item_id):
|
|
|
164 |
fbaOrderData = []
|
|
|
165 |
collection = get_mongo_connection().AmazonSaleSnapshot.AmazonSaleData
|
|
|
166 |
list = []
|
|
|
167 |
query = {}
|
|
|
168 |
list.append({"item_id":item_id})
|
|
|
169 |
query['$gte'] = to_java_date(startDate)
|
|
|
170 |
query['$lte'] = to_java_date(endDate)
|
|
|
171 |
list.append({"purchaseDate":query})
|
|
|
172 |
print list
|
|
|
173 |
if offset is None and limit is None:
|
|
|
174 |
cursor = collection.find({"$and":list}).sort([('purchaseDate',pymongo.ASCENDING)])
|
|
|
175 |
print cursor.count()
|
|
|
176 |
else:
|
|
|
177 |
cursor = collection.find({"$and":list}).skip(offset).limit(limit).sort([('purchaseDate',pymongo.ASCENDING)])
|
|
|
178 |
for orderData in cursor:
|
|
|
179 |
fbaOrderData.append(to_amazonFbaOrderItem(orderData))
|
|
|
180 |
return fbaOrderData
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
def dict_nlargest(d,n):
|
|
|
185 |
import heapq
|
|
|
186 |
return heapq.nlargest(n ,d, key = lambda k: d[k])
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
def main():
|
|
|
190 |
endDate = (datetime.now()).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
191 |
print endDate
|
|
|
192 |
startDate = (datetime.now() -timedelta(days=30)).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
193 |
print startDate
|
|
|
194 |
orderItemData = getAmazonOrderDataByItemId(startDate, endDate,None,None,16914)
|
|
|
195 |
priceSku = {}
|
|
|
196 |
print len(orderItemData)
|
|
|
197 |
count = 0
|
|
|
198 |
for x in orderItemData:
|
| 13531 |
kshitij.so |
199 |
if x.promotionDiscount > 0 or x.orderStatus == 'Cancelled':
|
| 13530 |
kshitij.so |
200 |
count +=1
|
|
|
201 |
continue
|
|
|
202 |
print x.totalAmount
|
|
|
203 |
print x.quantity
|
|
|
204 |
print "*************"
|
|
|
205 |
price = x.totalAmount / x.quantity
|
|
|
206 |
if priceSku.has_key(price):
|
|
|
207 |
qty = priceSku.get(price)
|
|
|
208 |
priceSku[price] = qty + x.quantity
|
|
|
209 |
else:
|
|
|
210 |
priceSku[price] = x.quantity
|
|
|
211 |
|
|
|
212 |
print priceSku
|
|
|
213 |
print "promotion order count ",count
|
|
|
214 |
newdict = {}
|
|
|
215 |
for key, value in sorted(priceSku.iteritems(), key=lambda (k,v): (v,k),reverse=True):
|
|
|
216 |
print "%s: %s" % (key, value)
|
|
|
217 |
rng = str(int(key)-25)+'-'+str(int(key))
|
|
|
218 |
print rng
|
|
|
219 |
total_qty = 0
|
|
|
220 |
min = 0
|
|
|
221 |
max = 0
|
|
|
222 |
for x in range(int(key-25),int(key+1)):
|
|
|
223 |
qty = priceSku.get(x)
|
|
|
224 |
if qty is not None:
|
|
|
225 |
if min==0:
|
|
|
226 |
min = x
|
|
|
227 |
if max < x:
|
|
|
228 |
max = x
|
|
|
229 |
total_qty = qty + total_qty
|
|
|
230 |
newdict[str(min)+'-'+str(max)] = total_qty
|
|
|
231 |
print newdict
|
|
|
232 |
|
|
|
233 |
d = {}
|
|
|
234 |
|
|
|
235 |
for key, value in sorted(priceSku.iteritems(), key=lambda (k,v): (v,k),reverse=True):
|
|
|
236 |
print "%s: %s" % (key, value)
|
|
|
237 |
rng = str(int(key))+'-'+str(int(key)+26)
|
|
|
238 |
print rng
|
|
|
239 |
total_qty = 0
|
|
|
240 |
min = 0
|
|
|
241 |
max = 0
|
|
|
242 |
for x in range(int(key),int(key+26)):
|
|
|
243 |
qty = priceSku.get(x)
|
|
|
244 |
if qty is not None:
|
|
|
245 |
if min==0:
|
|
|
246 |
min = x
|
|
|
247 |
if max < x:
|
|
|
248 |
max = x
|
|
|
249 |
total_qty = qty + total_qty
|
|
|
250 |
d[str(min)+'-'+str(max)] = total_qty
|
|
|
251 |
print d
|
| 13108 |
kshitij.so |
252 |
|
| 13530 |
kshitij.so |
253 |
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
| 13480 |
kshitij.so |
262 |
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
| 13107 |
kshitij.so |
266 |
if __name__ == "__main__":
|
|
|
267 |
main()
|