Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
6918 kshitij.so 1
#!/usr/bin/python
2
 
3
'''
4
Alerts regarding inventory update.
5
'''
6
 
7
import sys
8
import MySQLdb
9
import optparse
10
import time
11
import datetime
12
import smtplib
13
from email.mime.text import MIMEText
14
 
15
 
16
parser = optparse.OptionParser()
17
usage = "Usage: %prog -H [DB Address]"
18
parser.add_option("-H", "--host", dest="host",default="",type="string",help="Database Address",metavar="HOST")
19
(options, args) = parser.parse_args()
20
if ( not(options.host)):
21
    print "Fatal: Required arguments are missing!"
22
    print "Use: -h / --help to get help."
23
    sys.exit(1)
24
db = MySQLdb.connect(options.host,"root","shop2020","inventory" )
25
now = datetime.datetime.now()
26
cursor = db.cursor()
27
MESSAGE=[]
28
 
29
 
30
def getUpdate(warehouseName,warehouseId,checkInterval):
31
 
32
    try:
33
        print "Warehouse Name:",warehouseName
34
        print "Warehouse Id:",warehouseId
35
        print "Time Period:",checkInterval
36
        print str(now)
37
        sql = '''SELECT lastCheckedOn FROM warehouse where id=%d''' % \
38
        (warehouseId)
39
        cursor.execute(sql)
40
        data = cursor.fetchone()
41
        print "Data [0]:",data[0]
42
        if (data[0] is None):
43
            print "Inside None if"
44
            mailText = 'Last Checked On is NULL for '+warehouseName+" Id = "+str(warehouseId)+"\n\n"
45
            print mailText
46
            MESSAGE.append(mailText)
47
        else:
48
            diffMinutes =int((time.mktime(now.timetuple()) - time.mktime(data[0].timetuple()))/60)
49
            print "Diff",diffMinutes
50
        if ( diffMinutes > checkInterval):
51
            print "Diffcheck"
7038 kshitij.so 52
            minutes=diffMinutes%60
53
            hours=int(diffMinutes/60)
54
            checkHours=int(checkInterval/60)
55
            mailText = "Warehouse "+warehouseName+" Id = "+str(warehouseId)+" was last checked "+str(hours)+" hours and "+str(minutes)+" minutes ago while its interval is "+str(checkHours)+" hours"+"\n\n"
6918 kshitij.so 56
            print mailText
57
            MESSAGE.append(mailText)
58
            print "checking timediff"
59
 
60
    except:
61
        "MYSQL Exception"
62
 
63
 
64
def checkWarehouseHoliday(warehouseHolidays):
65
    currentDay=str(now.weekday())
66
    print "Current Day",currentDay
67
    print "WarehouseHoliday",warehouseHolidays
68
    if currentDay in warehouseHolidays:
69
        return True
70
    else:
71
        return False
72
 
73
def sendMail():
74
 
75
    print MESSAGE
76
    msg = ''.join(MESSAGE)
77
    smtpServer = smtplib.SMTP('localhost')
78
    smtpServer.set_debuglevel(1)
79
    msg = MIMEText(msg)
80
    sender = 'inventory-monitor@shop2020.in'
7126 kshitij.so 81
    recipients = ['rajneesh.arora@shop2020.in','rajveer.singh@shop2020.in','kshitij.sood@shop2020.in','amar.kumar@shop2020.in',
7457 kshitij.so 82
                  'chaitnaya.vats@shop2020.in','khushal.bhatia@shop2020.in','chandan.kumar@shop2020.in','manoj.kumar@shop2020.in']
6918 kshitij.so 83
    msg['Subject'] = "Warehouse Inventory Monitor"
84
    msg['From'] = sender
85
    msg['To'] = ", ".join(recipients)
86
 
87
    try:
88
        smtpServer.sendmail(sender, recipients, msg.as_string())
89
        print "Successfully sent email"
90
 
91
    except:
92
        print "Error: unable to send email"
93
 
94
def main():
95
    try:
96
        sql = '''select * from warehouseMonitor'''
97
        cursor.execute(sql)
98
        listWarehouse = cursor.fetchall()
99
        for warehouse in listWarehouse:
100
            if ( checkWarehouseHoliday(str(warehouse[3])) ):
101
                continue
102
            if ( warehouse[4]==1 ):
103
                getUpdate(warehouse[1],warehouse[2],warehouse[5])
104
        if (MESSAGE[0] is not None):
105
            sendMail()    
106
 
107
    except:
108
        "MYSQL Exception"
109
 
110
    finally:
111
        print "Closing db connection"
112
        db.close()  
113
 
114
if __name__ == "__main__":
7038 kshitij.so 115
    main()