Subversion Repositories SmartDukaan

Rev

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