Subversion Repositories SmartDukaan

Rev

Rev 15914 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15914 manas 1
'''
2
Created on 01-Jul-2015
3
 
4
@author: manas
5
'''
6
from datetime import date, datetime, timedelta
7
import MySQLdb
8
import time
9
import xlwt
10
from datetime import datetime
11
 
12
DB_HOST = "localhost"
13
DB_USER = "root"
14
DB_PASSWORD = "shop2020"
15
DB_NAME = "dtr"
16
TMP_FILE = "/tmp/User_Address_Report.xls"  
17
con = None
18
 
19
date_format = xlwt.XFStyle()
20
date_format.num_format_str = 'yyyy/mm/dd'
21
 
22
datetime_format = xlwt.XFStyle()
23
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
24
 
25
default_format = xlwt.XFStyle()
26
 
27
NON_REGISTERED_USERS="""
16337 manas 28
SELECT  p.user_id as count FROM pushnotifications p join 
29
notification_campaigns n on p.notification_campaign_id=n.id 
30
WHERE p.message = 'NotRegistered' 
31
group by p.user_id having count(1) >=5;
15914 manas 32
"""
33
 
34
#select * from pushnotifications where datediff(now(),created) <= 7 and user_id=%s and status=1;
35
CHECK_NON_REGISTERED_USERS="""
36
select * from pushnotifications where datediff(now(),created) <=7 and datediff(now(),created)>=2 and user_id=%s and status=1;
37
"""
38
 
39
CHECK_STATUS_LAST_DAY_USERS="""
40
select * from pushnotifications where datediff(now(),created) <= 1 and user_id=%s and status=1;
41
"""
42
 
43
LAST_ACTIVE_QUERY="""
44
select last_active,datediff(now(),last_active) from useractive where user_id=%s; 
45
"""
46
 
47
INSERT_USER_ACTIVITY_STATUS= """INSERT INTO 
48
user_activity_status (user_id, comment,last_active,last_active_diff,first_not_registered_time,created) VALUES (%s,%s,%s,%s,%s,%s);"""
49
 
50
CHECK_USER_ACTIVITY_STATUS="""
51
select user_id,comment,last_active from user_activity_status where user_id=%s;
52
"""
53
 
54
DELETE_USER_ACTIVITY_STATUS="""
55
delete from user_activity_status where user_id=%s;
56
"""
57
 
58
CHECK_USER_PRESENT="""select * from user_activity_status where user_id=%s"""
59
 
60
 
61
CHECK_MAX_SUCCESS="""
62
select max(created) from pushnotifications where user_id=%s and message='success';
63
"""
64
 
65
CHECK_DATE_NOT_REGISTERED="""
66
select min(created) from pushnotifications where user_id=%s and message='NotRegistered' and created<'%s';
67
"""
68
 
69
CHECK_DATE_NOT_REGISTERED_ELSE="""
70
select min(created) from pushnotifications where user_id=%s and message='NotRegistered'
71
"""
72
 
73
def getDbConnection():
74
    return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
75
 
76
def fetchNonRegisteredUsers():
77
    conn = getDbConnection()
78
    datesql=NON_REGISTERED_USERS 
79
    cursor = conn.cursor()
80
    cursor.execute(datesql)
81
    result = cursor.fetchall()
82
    print result
83
    for r in result:
84
        checkNonRegisteredUsers(r[0])
85
 
86
 
87
def checkNonRegisteredUsers(user_id):
88
    conn = getDbConnection()
89
    datesql=CHECK_NON_REGISTERED_USERS%user_id
90
    cursor = conn.cursor()
91
    cursor.execute(datesql)
92
    result = len(cursor.fetchall())
93
    if result !=0:
94
        print 'Ignoring the userId since it has received one notification in the last 7 days ' + str(user_id)
95
    else:
96
        checkUserInTable(user_id)
97
 
98
 
99
def checkUserInTable(user_id):
100
    conn = getDbConnection()
101
    datesql= CHECK_USER_PRESENT%user_id
102
    cursor = conn.cursor()
103
    cursor.execute(datesql)
104
    result = len(cursor.fetchall())
105
    if result==0:
106
        print 'User not present. Adding UserId' + str(user_id)
107
        lastActiveStatus(user_id)
108
    else:
109
        print 'UserId already present' + str(user_id)  
110
        checkSuccessLastDay(user_id)   
111
 
112
 
113
def checkSuccessLastDay(user_id):
114
    conn = getDbConnection()
115
    datesql= CHECK_STATUS_LAST_DAY_USERS%user_id
116
    cursor = conn.cursor()
117
    cursor.execute(datesql)
118
    result = cursor.fetchall()
119
    if len(result)==0:
120
        print 'User already present and need to check his status' + str(user_id)
121
        checkUserActivityStatus(user_id,0)
122
    else:
123
        print 'User Needs to be deleted if its present' + str(user_id)
124
        checkUserActivityStatus(user_id,1)
125
 
126
 
127
 
128
def lastActiveStatus(user_id):
129
    conn = getDbConnection()
130
    datesql=LAST_ACTIVE_QUERY%user_id
131
    cursor = conn.cursor()
132
    cursor.execute(datesql)
133
    result = cursor.fetchall()
134
    last_active_difference=0
135
    last_active=''
136
    status=''
137
    dateNotRegistered = checknotRegisteredDate(user_id)
138
    if len(result)==0:
139
        status='No activity'
140
        last_active_difference = 'NA'
141
        last_active='NA'
142
    else:    
143
        for r in result:
144
            last_active_difference = r[1]
145
            last_active=r[0]
146
            print last_active
147
            if last_active_difference >=30:
148
                status= 'Uninstall'
149
            elif last_active_difference <30 and last_active_difference>=15:
150
                status = 'Probable uninstall'    
151
            elif last_active_difference <15:
152
                status = 'Notification issue'    
153
            else:
154
                status='Others'
155
    cursor.execute(INSERT_USER_ACTIVITY_STATUS, (user_id,status,last_active,last_active_difference,dateNotRegistered,datetime.now()))
156
    conn.commit()
157
 
158
def checknotRegisteredDate(user_id):
159
    conn = getDbConnection()
160
    datesql=CHECK_MAX_SUCCESS%user_id
161
    cursor = conn.cursor()
162
    cursor.execute(datesql)
163
    result = cursor.fetchall()
164
    for r in result:
165
        if r[0] is None:
166
            conn = getDbConnection()
167
            dateSql=CHECK_DATE_NOT_REGISTERED_ELSE%user_id
168
            #print 'In if' + dateSql
169
            cursor = conn.cursor()
170
            cursor.execute(dateSql)
171
            date = cursor.fetchall()
172
            #print 'Result in if ' + str(date)
173
            for d in date:
174
                return d[0]
175
        else:
176
            conn = getDbConnection()
177
            datesql=CHECK_DATE_NOT_REGISTERED%(user_id,r[0])
178
            #print datesql
179
            cursor = conn.cursor()
180
            cursor.execute(datesql)
181
            date = cursor.fetchall()
182
            #print 'Result in else ' + str(date)
183
            for r in date:
184
                if r[0] is None:
185
                    conn = getDbConnection()
186
                    datesql=CHECK_DATE_NOT_REGISTERED_ELSE%(user_id)
187
                    cursor = conn.cursor()
188
                    cursor.execute(datesql)
189
                    date = cursor.fetchall()
190
                    for r in date:
191
                        return r[0]
192
                    #print 'In else in if ' + str(date) 
193
                else:
194
                    return r[0]    
195
 
196
def lastActiveReturnStatus(user_id):
197
    conn = getDbConnection()
198
    datesql=LAST_ACTIVE_QUERY%user_id
199
    cursor = conn.cursor()
200
    cursor.execute(datesql)
201
    result = cursor.fetchall()
202
    last_active_difference=0
203
    last_active=''
204
    status=''
205
    if len(result)==0:
206
        status='No activity'
207
        last_active_difference = 'NA'
208
        last_active='NA'
209
    else:    
210
        for r in result:
211
            last_active_difference = r[1]
212
            last_active=r[0]
213
            print last_active
214
            if last_active_difference >=30:
215
                status= 'Uninstall'
216
            elif last_active_difference <30 and last_active_difference>=15:
217
                status = 'Probable uninstall'    
218
            elif last_active_difference <15:
219
                status = 'Notification issue'    
220
            else:
221
                status='Others'
222
    return status    
223
 
224
def checkUserActivityStatus(user_id,flag):
225
    conn = getDbConnection()
226
    datesql=CHECK_USER_ACTIVITY_STATUS%user_id
227
    cursor = conn.cursor()
228
    cursor.execute(datesql)
229
    result = cursor.fetchall()
230
    if len(result) ==0:
231
        print 'User not added into the system, hence ignored' + str(user_id)
232
    else:
233
        if flag==1:
234
            conn = getDbConnection()
235
            datesql=DELETE_USER_ACTIVITY_STATUS%user_id
236
            cursor = conn.cursor()
237
            cursor.execute(datesql)
238
            print str(user_id) +' User successfully deleted as he has become active'    
239
        elif flag==0:
240
            for r in result:
241
                if r[1]==lastActiveStatus(user_id):
242
                    print 'Status same as earlier hence not updated for UserId' + str(user_id)
243
                    pass
244
                else:
245
                    print 'Result different for userId' + str(user_id)
246
                    print r[1] + 'Status in '  + lastActiveReturnStatus(user_id)
247
                    #Need to update the status for the user here.    
248
 
249
def main():
250
    dropTable()
251
    fetchNonRegisteredUsers()
252
#    print(checknotRegisteredDate("3102"))
253
 
254
def dropTable():
255
    conn = getDbConnection()
256
    datesql="delete from user_activity_status"
257
    cursor = conn.cursor()
258
    cursor.execute(datesql)
259
    conn.commit()
260
 
261
if __name__ == '__main__':
262
    main()
263
#select min(created) from pushnotifications where message='NotRegistered' and user_id=2913;
264
#select p.status from pushnotifications p join notification_campaigns n on p.notification_campaign_id =n.id where datediff(now(),n.created)=1 and p.user_id=2913;