| 16413 |
manish.sha |
1 |
import time
|
| 16436 |
manish.sha |
2 |
from datetime import datetime, timedelta
|
| 16413 |
manish.sha |
3 |
|
|
|
4 |
import MySQLdb
|
|
|
5 |
from dtr.storage import DataService
|
|
|
6 |
from dtr.storage.DataService import Pushnotifications
|
|
|
7 |
from elixir import *
|
|
|
8 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
|
|
9 |
from sqlalchemy.sql import func
|
|
|
10 |
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
|
|
|
11 |
between
|
|
|
12 |
|
| 18384 |
amit.gupta |
13 |
PUSH_NOTIFICATIONS_RETRY_SQL ="select x.user_id, x.id, x.created, x.notification_campaign_id, x.name, TIMESTAMPDIFF(HOUR, x.created, x.expiresat) tdiff, x.sent-IF (y.received is null, 0, y.received) failureCount, x.last_active, x.expiresat from (select p.id,p.user_id, p.created, p.notification_campaign_id, n.name, count(*) sent, ua.last_active, n.expiresat from pushnotifications p join notification_campaigns n on p.notification_campaign_id = n.id join useractive ua on p.user_id = ua.user_id join (select * from (select * from devices order by id desc) dev group by user_id) d on p.user_id = d.user_id where p.notification_campaign_id in (select id from notification_campaigns where TIMESTAMPDIFF(HOUR, created, NOW())<=2 and (name not like 'Batch Credit%' and name not like '%copy%')) and p.type='sent' and p.message = 'success' and d.versioncode >=14 and ua.last_active >= p.created and ua.last_active <= n.expiresat group by p.id) as x left join (select p.id, p.user_id, p.created, p.notification_campaign_id, n.name, count(*) received, ua.last_active, n.expiresat from pushnotifications p join notification_campaigns n on p.notification_campaign_id = n.id join useractive ua on p.user_id = ua.user_id join (select * from (select * from devices order by id desc) dev group by user_id) d on p.user_id = d.user_id where p.notification_campaign_id in (select id from notification_campaigns where TIMESTAMPDIFF(HOUR, created, NOW())<=2 and (name not like 'Batch Credit%' and name not like '%copy%')) and d.versioncode >=14 and p.type='recieved' group by p.id) as y on (x.user_id = y.user_id and x.notification_campaign_id = y.notification_campaign_id) having failureCount =2"
|
| 16413 |
manish.sha |
14 |
|
|
|
15 |
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
|
|
16 |
cursor = db.cursor()
|
|
|
17 |
DataService.initialize(db_hostname="localhost")
|
|
|
18 |
|
|
|
19 |
cursor.execute(PUSH_NOTIFICATIONS_RETRY_SQL)
|
|
|
20 |
retry_records_data = cursor.fetchall()
|
| 16425 |
manish.sha |
21 |
|
|
|
22 |
campaignUsersMap = {}
|
|
|
23 |
campaignNamesMap = {}
|
| 16436 |
manish.sha |
24 |
campaignTimeDiffMap = {}
|
| 16425 |
manish.sha |
25 |
|
| 16413 |
manish.sha |
26 |
if retry_records_data:
|
|
|
27 |
for record in retry_records_data:
|
| 16425 |
manish.sha |
28 |
if campaignUsersMap.has_key(long(record[3])):
|
|
|
29 |
users = campaignUsersMap.get(long(record[3]))
|
|
|
30 |
users.append(long(record[0]))
|
|
|
31 |
campaignUsersMap[long(record[3])] = users
|
|
|
32 |
else:
|
|
|
33 |
users = []
|
|
|
34 |
users.append(long(record[0]))
|
|
|
35 |
campaignUsersMap[long(record[3])] = users
|
| 16436 |
manish.sha |
36 |
|
| 16425 |
manish.sha |
37 |
if not campaignNamesMap.has_key(long(record[3])):
|
|
|
38 |
name = str(record[4])
|
|
|
39 |
campaignNamesMap[long(record[3])] = name
|
| 16436 |
manish.sha |
40 |
|
|
|
41 |
if not campaignTimeDiffMap.has_key(long(record[3])):
|
|
|
42 |
diff = long(record[5])
|
|
|
43 |
campaignTimeDiffMap[long(record[3])] = diff
|
| 16425 |
manish.sha |
44 |
|
|
|
45 |
print campaignUsersMap
|
|
|
46 |
|
|
|
47 |
for campaignId, users in campaignUsersMap.iteritems():
|
|
|
48 |
name = campaignNamesMap.get(campaignId)+"- copy"
|
| 16436 |
manish.sha |
49 |
created = datetime.now()
|
|
|
50 |
expiresat = created+timedelta(hours = campaignTimeDiffMap.get(campaignId))
|
|
|
51 |
print created, expiresat
|
|
|
52 |
campaignInsertSql = "insert into notification_campaigns(name, title, message, type, url, notification_campaigns.sql, expiresat, status, created) select '%s', title, message, type, url, notification_campaigns.sql, '%s', status, '%s' from notification_campaigns where id = %d"%(name, expiresat.strftime('%Y-%m-%d %H:%M:%S'), created.strftime('%Y-%m-%d %H:%M:%S'), campaignId)
|
| 16425 |
manish.sha |
53 |
print campaignInsertSql
|
|
|
54 |
cursor.execute(campaignInsertSql)
|
|
|
55 |
db.commit()
|
|
|
56 |
insertedCampaignId = cursor.lastrowid
|
|
|
57 |
|
|
|
58 |
for user in users:
|
|
|
59 |
pushnotification = Pushnotifications()
|
|
|
60 |
pushnotification.user_id = user
|
|
|
61 |
pushnotification.notification_campaign_id = insertedCampaignId
|
|
|
62 |
pushnotification.type = 'pending'
|
|
|
63 |
pushnotification.status = False
|
| 16436 |
manish.sha |
64 |
pushnotification.created = datetime.now()
|
| 16425 |
manish.sha |
65 |
session.commit()
|
| 16413 |
manish.sha |
66 |
|
|
|
67 |
db.close()
|
|
|
68 |
if session.is_active:
|
|
|
69 |
print "session is active. closing it."
|
| 16425 |
manish.sha |
70 |
session.close()
|
|
|
71 |
|