| 16413 |
manish.sha |
1 |
import time
|
|
|
2 |
import datetime
|
|
|
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 |
|
| 16425 |
manish.sha |
13 |
PUSH_NOTIFICATIONS_RETRY_SQL ="select x.user_id, x.id, x.created, x.notification_campaign_id, x.name, 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 where p.notification_campaign_id in (select id from notification_campaigns where TIMESTAMPDIFF(HOUR, created, NOW())<=2) and p.type='sent' and p.message = 'success' 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 where p.notification_campaign_id in (select id from notification_campaigns where TIMESTAMPDIFF(HOUR, created, NOW())<=2) 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 =1"
|
| 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 = {}
|
|
|
24 |
|
| 16413 |
manish.sha |
25 |
if retry_records_data:
|
|
|
26 |
for record in retry_records_data:
|
| 16425 |
manish.sha |
27 |
if campaignUsersMap.has_key(long(record[3])):
|
|
|
28 |
users = campaignUsersMap.get(long(record[3]))
|
|
|
29 |
users.append(long(record[0]))
|
|
|
30 |
campaignUsersMap[long(record[3])] = users
|
|
|
31 |
else:
|
|
|
32 |
users = []
|
|
|
33 |
users.append(long(record[0]))
|
|
|
34 |
campaignUsersMap[long(record[3])] = users
|
|
|
35 |
if not campaignNamesMap.has_key(long(record[3])):
|
|
|
36 |
name = str(record[4])
|
|
|
37 |
campaignNamesMap[long(record[3])] = name
|
| 16413 |
manish.sha |
38 |
|
| 16425 |
manish.sha |
39 |
|
|
|
40 |
print campaignUsersMap
|
|
|
41 |
|
|
|
42 |
for campaignId, users in campaignUsersMap.iteritems():
|
|
|
43 |
name = campaignNamesMap.get(campaignId)+"- copy"
|
|
|
44 |
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, expiresat, status, created from notification_campaigns where id = %d"%(name,campaignId)
|
|
|
45 |
print campaignInsertSql
|
|
|
46 |
cursor.execute(campaignInsertSql)
|
|
|
47 |
db.commit()
|
|
|
48 |
insertedCampaignId = cursor.lastrowid
|
|
|
49 |
|
|
|
50 |
for user in users:
|
|
|
51 |
pushnotification = Pushnotifications()
|
|
|
52 |
pushnotification.user_id = user
|
|
|
53 |
pushnotification.notification_campaign_id = insertedCampaignId
|
|
|
54 |
pushnotification.type = 'pending'
|
|
|
55 |
pushnotification.status = False
|
| 16426 |
manish.sha |
56 |
pushnotification.created = datetime.datetime.now()
|
| 16425 |
manish.sha |
57 |
session.commit()
|
| 16413 |
manish.sha |
58 |
|
|
|
59 |
db.close()
|
|
|
60 |
if session.is_active:
|
|
|
61 |
print "session is active. closing it."
|
| 16425 |
manish.sha |
62 |
session.close()
|
|
|
63 |
|