-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts_status.py
56 lines (45 loc) · 1.91 KB
/
products_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
__author__ = 'yonatan'
"""
updates the status key of the products/products_jp in a collection
logic:
1. checks the date when it was last updated (the 'download_data.dl_version' value)
2. if it was updated in the last 2 days then it is instock -> "status.instock = True"
3. else "status.instock = False"
and the days diff is calculated
* the flipkart collection status is already updated at download
"""
import datetime
import sys
from termcolor import colored
from . import constants
db = constants.db
month = 31
def update_status(coll="products"):
collection = db[coll]
today = datetime.datetime.today()
today_date = str(today.date())
yesterday = str((today - datetime.timedelta(1)).date())
collection.update_many({"download_data.dl_version": {"$ne": today_date}}, {'$set': {"status.instock": False}})
collection.update_many({"download_data.dl_version": yesterday}, {'$set': {"status.instock": True}})
total = collection.count()
print colored("total = %s" % str(total), "green", attrs=['bold'])
instock = collection.find({"status.instock": True}).count()
print colored("instock = %s" % str(instock), "yellow")
out = collection.find({"status.instock": False}).count()
print colored("out of stock = %s" % str(out), "yellow")
sanity = total - instock
if sanity == out:
print colored("sanity check ok", "green", attrs=['bold'])
else:
print colored("this is insane", "red", "on_yellow")
for day in range(2, month):
date = str((today - datetime.timedelta(day)).date())
res = collection.update_many({"download_data.dl_version": date}, {'$set': {"status.days_out": day}})
print colored("%s items out of stock for %s days " % (str(res.modified_count), str(day)), "magenta",
attrs=['bold'])
# add email
if __name__ == "__main__":
if len(sys.argv) > 1:
update_status(sys.argv[1])
else:
update_status()