| Line 1... |
Line 1... |
| 1 |
import MySQLdb
|
1 |
import MySQLdb
|
| 2 |
import xlwt
|
- |
|
| 3 |
from email import encoders
|
- |
|
| 4 |
from email.mime.base import MIMEBase
|
- |
|
| 5 |
from email.mime.multipart import MIMEMultipart
|
- |
|
| 6 |
from xlrd import open_workbook
|
- |
|
| 7 |
from xlutils.copy import copy
|
- |
|
| 8 |
import smtplib
|
- |
|
| 9 |
from dtr.storage.DataService import brand_preferences
|
2 |
from dtr.storage.DataService import brand_preferences
|
| - |
|
3 |
from elixir import *
|
| - |
|
4 |
from dtr.storage import DataService, Mongo
|
| - |
|
5 |
from dtr.storage.DataService import user_filters
|
| - |
|
6 |
from sqlalchemy.sql.expression import func, between
|
| - |
|
7 |
from datetime import datetime,timedelta
|
| - |
|
8 |
DataService.initialize(echo=True)
|
| 10 |
|
9 |
|
| 11 |
DB_HOST = "localhost"
|
10 |
DB_HOST = "localhost"
|
| 12 |
DB_USER = "root"
|
11 |
DB_USER = "root"
|
| 13 |
DB_PASSWORD = "shop2020"
|
12 |
DB_PASSWORD = "shop2020"
|
| 14 |
DB_NAME = "dtr"
|
13 |
DB_NAME = "dtr"
|
| 15 |
#TMP_FILE = "/home/kshitij/Downloads/User_Segmentation_Links.xls"
|
- |
|
| 16 |
TMP_FILE = "User_Segmentation_Links.xls"
|
- |
|
| 17 |
|
- |
|
| 18 |
SENDER = "cnc.center@shop2020.in"
|
- |
|
| 19 |
PASSWORD = "5h0p2o2o"
|
- |
|
| 20 |
SUBJECT = "User Segmentation Report"
|
- |
|
| 21 |
SMTP_SERVER = "smtp.gmail.com"
|
- |
|
| 22 |
SMTP_PORT = 587
|
- |
|
| 23 |
|
- |
|
| 24 |
date_format = xlwt.XFStyle()
|
- |
|
| 25 |
date_format.num_format_str = 'yyyy/mm/dd'
|
- |
|
| 26 |
|
- |
|
| 27 |
datetime_format = xlwt.XFStyle()
|
- |
|
| 28 |
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
|
- |
|
| 29 |
|
- |
|
| 30 |
default_format = xlwt.XFStyle()
|
- |
|
| 31 |
|
14 |
|
| 32 |
USER_SEGMENTATION_LINK_QUERY="""
|
15 |
USER_SEGMENTATION_LINK_QUERY="""
|
| 33 |
select x.user_id,x.brand,x.category_id,x.count,x.source,x.weight
|
16 |
select x.user_id,x.brand,x.category_id,x.count,x.source,x.weight
|
| 34 |
from
|
17 |
from
|
| 35 |
(
|
18 |
(
|
| 36 |
select c.user_id,c.brand,c.category_id,count(c.id) as count,"clicks" as source,(uw.weightage * count(c.id)) as weight
|
19 |
select c.user_id,c.brand,c.category_id,count(c.id) as count,"clicks" as source,(uw.weightage * count(c.id)) as weight
|
| 37 |
from clicks c join user_segmentation_weigthage uw on "clicks"=uw.weightage_type
|
20 |
from clicks c join user_segmentation_weigthage uw on "clicks"=uw.weightage_type
|
| 38 |
where c.category_id is not null and c.category_id !=0 and c.brand is not null and
|
21 |
where c.category_id is not null and c.category_id !=0 and c.brand is not null and
|
| 39 |
c.brand not like '' and c.user_id!=0 group by c.category_id,c.brand,c.user_id
|
22 |
c.brand not like '' and c.user_id!=0 and datediff(curdate(),c.created)<7 group by c.category_id,c.brand,c.user_id
|
| 40 |
|
23 |
|
| 41 |
UNION
|
24 |
UNION
|
| 42 |
|
25 |
|
| 43 |
select m.userId as user_id,m.brand,m.categoryId as category_id,count(1) as count,"orders" as source,(uw.weightage * count(1)) as weight
|
26 |
select m.userId as user_id,m.brand,m.categoryId as category_id,count(1) as count,"orders" as source,(uw.weightage * count(1)) as weight
|
| 44 |
from merchantsuborders m join user_segmentation_weigthage uw on "orders"=uw.weightage_type
|
27 |
from merchantsuborders m join user_segmentation_weigthage uw on "orders"=uw.weightage_type
|
| 45 |
where m.categoryId is not null and m.brand is not null and m.userId!=0 group by m.categoryId,m.brand,m.userId
|
28 |
where m.categoryId is not null and m.brand is not null and m.userId!=0 and datediff(curdate(),m.createdTime)<7 group by m.categoryId,m.brand,m.userId
|
| 46 |
|
29 |
|
| 47 |
UNION
|
30 |
UNION
|
| 48 |
|
31 |
|
| 49 |
select fo.user_id,fo.brand,fo.category as category_id,count(fo.id) as count,"orders" as source, (uw.weightage * count(fo.id)) as weight
|
32 |
select fo.user_id,fo.brand,fo.category as category_id,count(fo.id) as count,"orders" as source, (uw.weightage * count(fo.id)) as weight
|
| 50 |
from flipkartorders fo join user_segmentation_weigthage uw on "orders"=uw.weightage_type
|
33 |
from flipkartorders fo join user_segmentation_weigthage uw on "orders"=uw.weightage_type
|
| 51 |
where fo.category is not null and fo.brand is not null and fo.brand not like '' and fo.user_id!=0 and fo.user_id is not null group by fo.category,fo.brand,fo.user_id
|
34 |
where fo.category is not null and fo.brand is not null and fo.brand not like '' and fo.user_id!=0 and fo.user_id is not null
|
| - |
|
35 |
and datediff(curdate(),fo.created)<7 group by fo.category,fo.brand,fo.user_id
|
| 52 |
|
36 |
|
| 53 |
UNION
|
37 |
UNION
|
| 54 |
|
38 |
|
| 55 |
select bp.user_id,bp.brand,bp.category_id,count(1) as count,"preferences" as source,(uw.weightage* count(1)) as weight
|
39 |
select bp.user_id,bp.brand,bp.category_id,count(1) as count,"preferences" as source,(uw.weightage* count(1)) as weight
|
| 56 |
from brand_preferences bp join user_segmentation_weigthage uw on "preferences"=uw.weightage_type
|
40 |
from brand_preferences bp join user_segmentation_weigthage uw on "preferences"=uw.weightage_type
|
| 57 |
where user_id!=0 and status='show' group by bp.category_id,bp.brand,bp.user_id
|
41 |
where user_id!=0 and status='show' and datediff(curdate(),bp.created)<7 group by bp.category_id,bp.brand,bp.user_id
|
| 58 |
) as x
|
42 |
) as x
|
| 59 |
|
43 |
|
| 60 |
"""
|
44 |
"""
|
| 61 |
|
45 |
|
| - |
|
46 |
USER_STORE_PRODUCT_VIEW="""
|
| - |
|
47 |
SELECT u.user_id,count(u.id) AS count, SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(u.url, '/', -1 ),'?',1),'%',1)
|
| - |
|
48 |
AS skuBundleId,"product_view" as source,(uw.weightage* count(u.id)) as weight
|
| - |
|
49 |
FROM user_urls u join user_segmentation_weigthage uw on "product_view"=uw.weightage_type
|
| - |
|
50 |
WHERE u.url LIKE '%store_products/view%' and u.user_id!=0 and
|
| - |
|
51 |
SUBSTRING_INDEX(SUBSTRING_INDEX(u.url, '/', -1 ),'?',1) not like ''
|
| - |
|
52 |
and datediff(curdate(),u.created)<7
|
| - |
|
53 |
group by u.user_id,skuBundleId;
|
| - |
|
54 |
"""
|
| - |
|
55 |
|
| - |
|
56 |
REPLACE_QUERY="""
|
| - |
|
57 |
REPLACE INTO userlinkssegmentation( user_id, brand, source,category_id, count, weight )
|
| - |
|
58 |
SELECT user_id, brand, source,category_id, count, weight
|
| - |
|
59 |
FROM userlinkssegmentation7days
|
| - |
|
60 |
WHERE user_id =%s and brand=%s and category_id=%s
|
| - |
|
61 |
"""
|
| - |
|
62 |
|
| - |
|
63 |
|
| - |
|
64 |
userMap = {} #key - user_id val - brand map
|
| - |
|
65 |
userMobileCategory={}
|
| - |
|
66 |
userTabletCategory={}
|
| 62 |
|
67 |
|
| 63 |
def getDbConnection():
|
68 |
def getDbConnection():
|
| 64 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
69 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
| 65 |
|
70 |
|
| - |
|
71 |
|
| 66 |
def generateUserSegmentationLinksReport():
|
72 |
def deleteUserForLinks():
|
| 67 |
datesql= USER_SEGMENTATION_LINK_QUERY
|
73 |
print '********** Deleting records from last 7 days tables *********'
|
| 68 |
conn = getDbConnection()
|
74 |
conn = getDbConnection()
|
| 69 |
|
- |
|
| 70 |
global workbook
|
- |
|
| 71 |
workbook = xlwt.Workbook()
|
- |
|
| 72 |
|
- |
|
| 73 |
worksheet = workbook.add_sheet("User Segmentation")
|
- |
|
| 74 |
boldStyle = xlwt.XFStyle()
|
- |
|
| 75 |
|
- |
|
| 76 |
style = xlwt.XFStyle()
|
- |
|
| 77 |
pattern = xlwt.Pattern()
|
- |
|
| 78 |
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
|
- |
|
| 79 |
pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
|
- |
|
| 80 |
style.pattern = pattern
|
- |
|
| 81 |
f = xlwt.Font()
|
- |
|
| 82 |
f.bold = True
|
- |
|
| 83 |
boldStyle.font = f
|
- |
|
| 84 |
|
- |
|
| 85 |
column = 0
|
- |
|
| 86 |
row = 0
|
- |
|
| 87 |
worksheet.write(row,column,'User Id',style)
|
- |
|
| 88 |
worksheet.write(row,column+1,'Product Name',style)
|
- |
|
| 89 |
worksheet.write(row,column+2,'Brand',style)
|
- |
|
| 90 |
worksheet.write(row,column+3,'Category Id',style)
|
- |
|
| 91 |
worksheet.write(row,column+4,'Count',style)
|
- |
|
| 92 |
worksheet.write(row,column+5,'Source',style)
|
- |
|
| 93 |
worksheet.write(row,column+6,'Weight',style)
|
- |
|
| 94 |
|
- |
|
| 95 |
cursor = conn.cursor()
|
75 |
cursor = conn.cursor()
|
| 96 |
cursor.execute(datesql)
|
- |
|
| 97 |
result = cursor.fetchall()
|
76 |
deleteAllUserGroups = "delete from userlinkssegmentation7days"
|
| 98 |
|
77 |
try:
|
| 99 |
for r in result:
|
- |
|
| 100 |
row=row+1
|
- |
|
| 101 |
worksheet.write(row,column,r[0])
|
- |
|
| 102 |
worksheet.write(row,column+1,r[1])
|
- |
|
| 103 |
worksheet.write(row,column+2,r[2])
|
78 |
cursor.execute(deleteAllUserGroups)
|
| 104 |
worksheet.write(row,column+3,r[3])
|
79 |
conn.commit()
|
| 105 |
worksheet.write(row,column+4,r[4])
|
80 |
except:
|
| 106 |
worksheet.write(row,column+5,r[5])
|
81 |
conn.rollback()
|
| 107 |
worksheet.write(row,column+6,r[6])
|
82 |
finally:
|
| 108 |
conn.close()
|
83 |
conn.close()
|
| 109 |
workbook.save(TMP_FILE)
|
84 |
print '********** Completed Deleting records from last 7 days tables *********'
|
| 110 |
|
- |
|
| 111 |
|
85 |
|
| 112 |
def addUsersForLinks():
|
86 |
def addUsersForLinks():
|
| - |
|
87 |
print '********** Inserting records in last 7 days table *********'
|
| 113 |
user_id=''
|
88 |
user_id=''
|
| 114 |
brand=''
|
89 |
brand=''
|
| 115 |
category_id=''
|
90 |
category_id=''
|
| 116 |
source=''
|
91 |
source=''
|
| 117 |
count=''
|
92 |
count=''
|
| Line 133... |
Line 108... |
| 133 |
else:
|
108 |
else:
|
| 134 |
category_id=r[2]
|
109 |
category_id=r[2]
|
| 135 |
count=r[3]
|
110 |
count=r[3]
|
| 136 |
source=r[4]
|
111 |
source=r[4]
|
| 137 |
weight=r[5]
|
112 |
weight=r[5]
|
| 138 |
|
- |
|
| 139 |
sql = "insert into userlinkssegmentation (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
113 |
sql = "insert into userlinkssegmentation7days (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| 140 |
cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
|
114 |
cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
|
| 141 |
conn.commit()
|
115 |
conn.commit()
|
| - |
|
116 |
conn.close()
|
| - |
|
117 |
print '********** Completed inserting records for orders,preferences and clicks in last 7 days table *********'
|
| - |
|
118 |
|
| - |
|
119 |
def filters():
|
| - |
|
120 |
print '********** Inserting records for filters in last 7 day table *********'
|
| - |
|
121 |
conn = getDbConnection()
|
| - |
|
122 |
cursor = conn.cursor()
|
| - |
|
123 |
cursor.execute("select weightage from user_segmentation_weigthage where weightage_type='filters'")
|
| - |
|
124 |
result = cursor.fetchall()
|
| - |
|
125 |
filter_weight=result[0][0]
|
| - |
|
126 |
todayDate=datetime.today().date()
|
| - |
|
127 |
data = session.query((user_filters)).filter_by(type="brand").filter(user_filters.created.between(todayDate-timedelta(days=6),todayDate)).all()
|
| - |
|
128 |
for d in data:
|
| - |
|
129 |
if userMap.has_key(d.user_id):
|
| - |
|
130 |
brandMap = userMap.get(d.user_id)
|
| - |
|
131 |
else:
|
| - |
|
132 |
brandMap = {}
|
| - |
|
133 |
brands = d.filters.split('|')
|
| - |
|
134 |
for brand in brands:
|
| - |
|
135 |
if brandMap.has_key(brand):
|
| - |
|
136 |
brandMap[brand] = brandMap.get(brand) + 1
|
| - |
|
137 |
else:
|
| - |
|
138 |
brandMap[brand] = 1
|
| - |
|
139 |
userMap[d.user_id] = brandMap
|
| - |
|
140 |
for x, y in userMap.iteritems():
|
| - |
|
141 |
for bra,cow in y.iteritems():
|
| - |
|
142 |
user_id=x
|
| - |
|
143 |
brand=bra
|
| - |
|
144 |
count=cow
|
| - |
|
145 |
category_id=3
|
| - |
|
146 |
weight=cow*filter_weight
|
| - |
|
147 |
source='filters'
|
| - |
|
148 |
sql = "insert into userlinkssegmentation7days (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| - |
|
149 |
cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
|
| - |
|
150 |
conn.commit()
|
| - |
|
151 |
|
| - |
|
152 |
conn.close()
|
| - |
|
153 |
print '********** Completed inserting records for filters in last 7 day table *********'
|
| - |
|
154 |
|
| - |
|
155 |
def storeProductView():
|
| - |
|
156 |
print '********** Inserting records for store product view in last 7 day table *********'
|
| - |
|
157 |
user_id=''
|
| - |
|
158 |
brand=''
|
| - |
|
159 |
category_id=''
|
| - |
|
160 |
source=''
|
| - |
|
161 |
count=''
|
| - |
|
162 |
weight=''
|
| 142 |
|
163 |
|
| - |
|
164 |
datesql= USER_STORE_PRODUCT_VIEW
|
| - |
|
165 |
conn = getDbConnection()
|
| - |
|
166 |
cursor = conn.cursor()
|
| - |
|
167 |
cursor.execute(datesql)
|
| - |
|
168 |
result = cursor.fetchall()
|
| - |
|
169 |
|
| - |
|
170 |
for r in result:
|
| - |
|
171 |
skuData = Mongo.getSkuBrandData(int(r[2]))
|
| - |
|
172 |
if len(skuData) !=0:
|
| - |
|
173 |
user_id=r[0]
|
| - |
|
174 |
brand=skuData['brand']
|
| - |
|
175 |
category_id =skuData['category_id']
|
| - |
|
176 |
source=r[3]
|
| - |
|
177 |
count=r[1]
|
| - |
|
178 |
if category_id==3:
|
| - |
|
179 |
if userMobileCategory.has_key(user_id):
|
| - |
|
180 |
brandMap = userMobileCategory.get(user_id)
|
| - |
|
181 |
else:
|
| - |
|
182 |
brandMap = {}
|
| - |
|
183 |
if brandMap.has_key(brand):
|
| - |
|
184 |
brandMap[brand] = brandMap.get(brand) + r[1]
|
| - |
|
185 |
else:
|
| - |
|
186 |
brandMap[brand] = r[1]
|
| - |
|
187 |
userMobileCategory[user_id] = brandMap
|
| - |
|
188 |
if category_id==5:
|
| - |
|
189 |
if userTabletCategory.has_key(user_id):
|
| - |
|
190 |
brandCategoryMap = userTabletCategory.get(user_id)
|
| - |
|
191 |
else:
|
| - |
|
192 |
brandCategoryMap = {}
|
| - |
|
193 |
if brandCategoryMap.has_key(brand):
|
| - |
|
194 |
brandCategoryMap[brand] = brandCategoryMap.get(brand) + r[1]
|
| - |
|
195 |
else:
|
| - |
|
196 |
brandCategoryMap[brand] = r[1]
|
| - |
|
197 |
userTabletCategory[user_id] = brandCategoryMap
|
| - |
|
198 |
else:
|
| - |
|
199 |
print 'Sku bundle Id not present',r[2]
|
| - |
|
200 |
|
| - |
|
201 |
|
| - |
|
202 |
cursor.execute("select weightage from user_segmentation_weigthage where weightage_type='product_view'")
|
| - |
|
203 |
result = cursor.fetchall()
|
| - |
|
204 |
product_view_weight=result[0][0]
|
| - |
|
205 |
|
| - |
|
206 |
for x, y in userMobileCategory.iteritems():
|
| - |
|
207 |
for bra,cow in y.iteritems():
|
| - |
|
208 |
user_id=x
|
| - |
|
209 |
brand= bra
|
| - |
|
210 |
count=cow
|
| - |
|
211 |
weight=cow*product_view_weight
|
| - |
|
212 |
source='product_view'
|
| - |
|
213 |
category_id=3
|
| - |
|
214 |
sql = "insert into userlinkssegmentation7days (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| - |
|
215 |
cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
|
| - |
|
216 |
conn.commit()
|
| - |
|
217 |
|
| - |
|
218 |
for x, y in userTabletCategory.iteritems():
|
| - |
|
219 |
for bra,cow in y.iteritems():
|
| - |
|
220 |
user_id=x
|
| - |
|
221 |
brand= bra
|
| - |
|
222 |
count=cow
|
| - |
|
223 |
weight=cow*product_view_weight
|
| - |
|
224 |
source='product_view'
|
| - |
|
225 |
category_id=5
|
| - |
|
226 |
sql = "insert into userlinkssegmentation7days (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| - |
|
227 |
cursor.execute(sql,(user_id,brand, category_id, source, count,weight))
|
| - |
|
228 |
conn.commit()
|
| 143 |
conn.close()
|
229 |
conn.close()
|
| 144 |
|
230 |
|
| - |
|
231 |
print '********** Completed inserting records for store product view in last 7 day table *********'
|
| - |
|
232 |
|
| 145 |
def deleteUserForLinks():
|
233 |
def findRepeatingOrders():
|
| - |
|
234 |
print '********** Call to sanitize data for orders if any multiple *********'
|
| - |
|
235 |
datesql= "select user_id,brand,category_id from userlinkssegmentation7days where source='orders' group by user_id,brand,category_id having count(source)=2;"
|
| 146 |
conn = getDbConnection()
|
236 |
conn = getDbConnection()
|
| 147 |
cursor = conn.cursor()
|
237 |
cursor = conn.cursor()
|
| - |
|
238 |
rows_count = cursor.execute(datesql)
|
| - |
|
239 |
if rows_count>0:
|
| - |
|
240 |
result = cursor.fetchall()
|
| - |
|
241 |
for r in result:
|
| - |
|
242 |
print r
|
| - |
|
243 |
findDetails(r[0],r[1],r[2])
|
| - |
|
244 |
else:
|
| 148 |
deleteAllUserGroups = "delete from userlinkssegmentation"
|
245 |
print 'No Data to be sanitized for repeating orders'
|
| 149 |
try:
|
246 |
|
| - |
|
247 |
def findDetails(userId,brand,category):
|
| - |
|
248 |
datesql= "select sum(count),sum(weight) from userlinkssegmentation7days where user_id=%s and source='orders' and brand=%s group by source;"
|
| - |
|
249 |
conn = getDbConnection()
|
| - |
|
250 |
cursor = conn.cursor()
|
| - |
|
251 |
cursor.execute(datesql,(userId,brand))
|
| - |
|
252 |
result = cursor.fetchall()
|
| - |
|
253 |
for r in result:
|
| - |
|
254 |
deletesql="delete from userlinkssegmentation7days where user_id=%s and brand=%s and source='orders'"
|
| - |
|
255 |
cursor = conn.cursor()
|
| 150 |
cursor.execute(deleteAllUserGroups)
|
256 |
cursor.execute(deletesql,(userId,brand))
|
| 151 |
conn.commit()
|
257 |
conn.commit()
|
| - |
|
258 |
insertsql="insert into userlinkssegmentation7days (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| - |
|
259 |
cursor = conn.cursor()
|
| - |
|
260 |
cursor.execute(insertsql,(userId,brand,category,'orders',r[0],r[1]))
|
| - |
|
261 |
conn.commit()
|
| - |
|
262 |
|
| - |
|
263 |
def findRepeatingClicks():
|
| - |
|
264 |
print '********** Call to sanitize data for product view if any multiple *********'
|
| - |
|
265 |
datesql= "select user_id,brand,category_id from userlinkssegmentation7days where source='product_view' group by user_id,brand,category_id having count(source)=2;"
|
| - |
|
266 |
conn = getDbConnection()
|
| - |
|
267 |
cursor = conn.cursor()
|
| - |
|
268 |
rows_count = cursor.execute(datesql)
|
| - |
|
269 |
if rows_count>0:
|
| - |
|
270 |
result = cursor.fetchall()
|
| - |
|
271 |
for r in result:
|
| - |
|
272 |
print r
|
| - |
|
273 |
findViewDetails(r[0],r[1],r[2])
|
| 152 |
except:
|
274 |
else:
|
| - |
|
275 |
print 'No Data to be sanitized for product view'
|
| - |
|
276 |
|
| - |
|
277 |
def findViewDetails(userId,brand,category):
|
| - |
|
278 |
datesql= "select sum(count),sum(weight) from userlinkssegmentation7days where user_id=%s and source='product_view' and brand=%s group by source;"
|
| - |
|
279 |
conn = getDbConnection()
|
| 153 |
conn.rollback()
|
280 |
cursor = conn.cursor()
|
| - |
|
281 |
cursor.execute(datesql,(userId,brand))
|
| - |
|
282 |
result = cursor.fetchall()
|
| 154 |
finally:
|
283 |
for r in result:
|
| - |
|
284 |
deletesql="delete from userlinkssegmentation7days where user_id=%s and brand=%s and source='product_view'"
|
| - |
|
285 |
cursor = conn.cursor()
|
| - |
|
286 |
cursor.execute(deletesql,(userId,brand))
|
| - |
|
287 |
conn.commit()
|
| - |
|
288 |
insertsql="insert into userlinkssegmentation7days (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| - |
|
289 |
cursor = conn.cursor()
|
| - |
|
290 |
cursor.execute(insertsql,(userId,brand,category,'product_view',r[0],r[1]))
|
| 155 |
conn.close()
|
291 |
conn.commit()
|
| - |
|
292 |
|
| 156 |
|
293 |
|
| - |
|
294 |
def checkCountForUpdate():
|
| - |
|
295 |
print '********** Checking users brand whose cumulative weight is greater than 3 *********'
|
| - |
|
296 |
datesql= "select user_id,brand,category_id from userlinkssegmentation7days group by user_id,brand having sum(weight) >=3;"
|
| - |
|
297 |
conn = getDbConnection()
|
| - |
|
298 |
cursor = conn.cursor()
|
| - |
|
299 |
cursor.execute(datesql)
|
| - |
|
300 |
result = cursor.fetchall()
|
| - |
|
301 |
for r in result:
|
| - |
|
302 |
checkUserForUpdate(r[0],r[1],r[2])
|
| - |
|
303 |
conn.close()
|
| - |
|
304 |
def checkUserForUpdate(userId,brand,category):
|
| - |
|
305 |
print '********** Checking users brand present or not in the master table *********'
|
| - |
|
306 |
datesql= "select distinct brand from userlinkssegmentation where user_id=%s and brand=%s and category_id=%s"
|
| - |
|
307 |
conn = getDbConnection()
|
| - |
|
308 |
cursor = conn.cursor()
|
| - |
|
309 |
cursor.execute(datesql,(userId,brand,category))
|
| - |
|
310 |
result = cursor.fetchall()
|
| - |
|
311 |
if len(result)==0:
|
| - |
|
312 |
insertNewUserLinkDetails(userId,brand,category)
|
| - |
|
313 |
else:
|
| - |
|
314 |
userReplaceData(userId,brand,category)
|
| - |
|
315 |
conn.close()
|
| - |
|
316 |
|
| - |
|
317 |
def userReplaceData(userId,brand,category):
|
| - |
|
318 |
print 'Data that needs to be updated for', userId,brand,category
|
| - |
|
319 |
conn = getDbConnection()
|
| - |
|
320 |
cursor = conn.cursor()
|
| - |
|
321 |
cursor.execute(REPLACE_QUERY,(userId,brand,category))
|
| - |
|
322 |
conn.commit()
|
| - |
|
323 |
conn.close()
|
| - |
|
324 |
|
| - |
|
325 |
def insertNewUserLinkDetails(userId,brand,category):
|
| - |
|
326 |
print 'Data needs to be inserted as the brand was not present earlier', userId,brand,category
|
| - |
|
327 |
conn = getDbConnection()
|
| - |
|
328 |
selectNewUser="select * from userlinkssegmentation7days where user_id=%s and brand=%s and category_id=%s"
|
| - |
|
329 |
cursor = conn.cursor()
|
| - |
|
330 |
cursor.execute(selectNewUser,(userId,brand,category))
|
| - |
|
331 |
result = cursor.fetchall()
|
| - |
|
332 |
print 'Result while inserting new record in master',result
|
| - |
|
333 |
for r in result:
|
| - |
|
334 |
sql = "insert into userlinkssegmentation (user_id, brand, category_id, source, count,weight) values(%s, %s, %s, %s, %s,%s)"
|
| - |
|
335 |
cursor.execute(sql,(r[0],r[1],r[2], r[3], r[4],r[5]))
|
| - |
|
336 |
conn.commit()
|
| - |
|
337 |
conn.close()
|
| 157 |
def main():
|
338 |
def main():
|
| 158 |
#generateUserSegmentationLinksReport()
|
339 |
print datetime.now()
|
| - |
|
340 |
print '********** Starting to update links for Users *********'
|
| 159 |
deleteUserForLinks()
|
341 |
deleteUserForLinks()
|
| 160 |
addUsersForLinks()
|
342 |
addUsersForLinks()
|
| 161 |
|
- |
|
| 162 |
def sendmail(email, message, fileName, title):
|
- |
|
| 163 |
if email == "":
|
- |
|
| 164 |
return
|
343 |
filters()
|
| 165 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
- |
|
| 166 |
mailServer.ehlo()
|
344 |
storeProductView()
|
| 167 |
mailServer.starttls()
|
345 |
findRepeatingOrders()
|
| 168 |
mailServer.ehlo()
|
346 |
findRepeatingClicks()
|
| 169 |
msg = MIMEMultipart()
|
347 |
checkCountForUpdate()
|
| 170 |
msg['Subject'] = title
|
- |
|
| 171 |
msg.preamble = title
|
- |
|
| 172 |
fileMsg = MIMEBase('application', 'vnd.ms-excel')
|
- |
|
| 173 |
fileMsg.set_payload(file(TMP_FILE).read())
|
- |
|
| 174 |
encoders.encode_base64(fileMsg)
|
- |
|
| 175 |
fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
348 |
print '********** Finished updating records for user links *********'
|
| 176 |
msg.attach(fileMsg)
|
- |
|
| 177 |
MAILTO = ['manas.kapoor@saholic.com']
|
- |
|
| 178 |
mailServer.login(SENDER, PASSWORD)
|
- |
|
| 179 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
- |
|
| 180 |
|
- |
|
| 181 |
|
- |
|
| 182 |
if __name__=='__main__':
|
349 |
if __name__=='__main__':
|
| 183 |
main()
|
350 |
main()
|
| 184 |
|
351 |
|