| 16336 |
manas |
1 |
from dtr.storage import Mongo
|
|
|
2 |
import MySQLdb
|
|
|
3 |
import xlwt
|
|
|
4 |
from email import encoders
|
|
|
5 |
from email.mime.base import MIMEBase
|
|
|
6 |
from email.mime.multipart import MIMEMultipart
|
|
|
7 |
from xlrd import open_workbook
|
|
|
8 |
from xlutils.copy import copy
|
|
|
9 |
import smtplib
|
|
|
10 |
|
|
|
11 |
DB_HOST = "localhost"
|
|
|
12 |
DB_USER = "root"
|
|
|
13 |
DB_PASSWORD = "shop2020"
|
|
|
14 |
DB_NAME = "dtr"
|
|
|
15 |
#TMP_FILE = "/home/kshitij/Downloads/User_Favorites_Report.xls"
|
|
|
16 |
TMP_FILE = "User_Favorites_Report.xls"
|
|
|
17 |
|
|
|
18 |
SENDER = "cnc.center@shop2020.in"
|
|
|
19 |
PASSWORD = "5h0p2o2o"
|
|
|
20 |
SUBJECT = "User Favorite 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 |
|
|
|
32 |
|
|
|
33 |
def getDbConnection():
|
|
|
34 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
35 |
|
|
|
36 |
PRODUCTS_USERS="""
|
|
|
37 |
select store_product_id,action,count(1) from
|
|
|
38 |
user_actions group by store_product_id,action order by store_product_id;
|
|
|
39 |
"""
|
|
|
40 |
|
|
|
41 |
USER_FAVORITES="""
|
|
|
42 |
select ua.store_product_id,ua.user_id,u.email,ua.action
|
|
|
43 |
from user_actions ua join users u on ua.user_id=u.id
|
|
|
44 |
order by ua.user_id,ua.action;
|
|
|
45 |
"""
|
|
|
46 |
|
|
|
47 |
USER_COUNT_LIKES_DISLIKES="""
|
|
|
48 |
SELECT user_id,email,count(*),action
|
|
|
49 |
FROM user_actions ua join users u where ua.user_id = u.id
|
|
|
50 |
group by user_id,action order by user_id;
|
|
|
51 |
"""
|
|
|
52 |
|
|
|
53 |
def generateProductWiseReport():
|
|
|
54 |
productName=''
|
|
|
55 |
datesql= PRODUCTS_USERS
|
|
|
56 |
conn = getDbConnection()
|
|
|
57 |
|
|
|
58 |
global workbook
|
|
|
59 |
workbook = xlwt.Workbook()
|
|
|
60 |
worksheet = workbook.add_sheet("Product Wise")
|
|
|
61 |
boldStyle = xlwt.XFStyle()
|
|
|
62 |
newStyle= xlwt.XFStyle()
|
|
|
63 |
style = xlwt.XFStyle()
|
|
|
64 |
pattern = xlwt.Pattern()
|
|
|
65 |
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
|
|
|
66 |
pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
|
|
|
67 |
style.pattern = pattern
|
|
|
68 |
f = xlwt.Font()
|
|
|
69 |
f.bold = True
|
|
|
70 |
boldStyle.font = f
|
|
|
71 |
|
|
|
72 |
column = 0
|
|
|
73 |
row = 0
|
|
|
74 |
worksheet.write(row,column,'Store Product Id',style)
|
|
|
75 |
worksheet.write(row,column+1,'Product Name',style)
|
|
|
76 |
worksheet.write(row,column+2,'Action',style)
|
|
|
77 |
worksheet.write(row,column+3,'Count of Users',style)
|
|
|
78 |
|
|
|
79 |
cursor = conn.cursor()
|
|
|
80 |
cursor.execute(datesql)
|
|
|
81 |
result = cursor.fetchall()
|
|
|
82 |
|
|
|
83 |
for r in result:
|
|
|
84 |
row=row+1
|
|
|
85 |
skuData = Mongo.getItem(r[0])
|
|
|
86 |
for items in skuData:
|
|
|
87 |
productName = items['source_product_name']
|
|
|
88 |
worksheet.write(row,column,r[0])
|
|
|
89 |
worksheet.write(row,column+1,productName)
|
|
|
90 |
worksheet.write(row,column+2,r[1])
|
|
|
91 |
worksheet.write(row,column+3,r[2])
|
|
|
92 |
|
|
|
93 |
conn.close()
|
|
|
94 |
workbook.save(TMP_FILE)
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
def generateUserProductReport():
|
|
|
98 |
productName=''
|
|
|
99 |
datesql= USER_FAVORITES
|
|
|
100 |
conn = getDbConnection()
|
|
|
101 |
|
|
|
102 |
rb = open_workbook(TMP_FILE,formatting_info=True)
|
|
|
103 |
workbook = copy(rb)
|
|
|
104 |
worksheet = workbook.add_sheet("User Likes and Dislikes")
|
|
|
105 |
boldStyle = xlwt.XFStyle()
|
|
|
106 |
|
|
|
107 |
style = xlwt.XFStyle()
|
|
|
108 |
pattern = xlwt.Pattern()
|
|
|
109 |
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
|
|
|
110 |
pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
|
|
|
111 |
style.pattern = pattern
|
|
|
112 |
f = xlwt.Font()
|
|
|
113 |
f.bold = True
|
|
|
114 |
boldStyle.font = f
|
|
|
115 |
|
|
|
116 |
column = 0
|
|
|
117 |
row = 0
|
|
|
118 |
worksheet.write(row,column,'Store Product Id',style)
|
|
|
119 |
worksheet.write(row,column+1,'Product Name',style)
|
|
|
120 |
worksheet.write(row,column+2,'User Id',style)
|
|
|
121 |
worksheet.write(row,column+3,'Email',style)
|
|
|
122 |
worksheet.write(row,column+4,'Action',style)
|
|
|
123 |
|
|
|
124 |
cursor = conn.cursor()
|
|
|
125 |
cursor.execute(datesql)
|
|
|
126 |
result = cursor.fetchall()
|
|
|
127 |
|
|
|
128 |
for r in result:
|
|
|
129 |
row=row+1
|
|
|
130 |
skuData = Mongo.getItem(r[0])
|
|
|
131 |
for items in skuData:
|
|
|
132 |
productName = items['source_product_name']
|
|
|
133 |
worksheet.write(row,column,r[0])
|
|
|
134 |
worksheet.write(row,column+1,productName)
|
|
|
135 |
worksheet.write(row,column+2,r[1])
|
|
|
136 |
worksheet.write(row,column+3,r[2])
|
|
|
137 |
worksheet.write(row,column+4,r[3])
|
|
|
138 |
|
|
|
139 |
conn.close()
|
|
|
140 |
workbook.save(TMP_FILE)
|
|
|
141 |
|
|
|
142 |
def generateUserCountFavoritesReport():
|
|
|
143 |
datesql= USER_COUNT_LIKES_DISLIKES
|
|
|
144 |
conn = getDbConnection()
|
|
|
145 |
|
|
|
146 |
rb = open_workbook(TMP_FILE,formatting_info=True)
|
|
|
147 |
workbook = copy(rb)
|
|
|
148 |
worksheet = workbook.add_sheet("User Count by Action")
|
|
|
149 |
boldStyle = xlwt.XFStyle()
|
|
|
150 |
|
|
|
151 |
style = xlwt.XFStyle()
|
|
|
152 |
pattern = xlwt.Pattern()
|
|
|
153 |
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
|
|
|
154 |
pattern.pattern_fore_colour = xlwt.Style.colour_map['yellow']
|
|
|
155 |
style.pattern = pattern
|
|
|
156 |
f = xlwt.Font()
|
|
|
157 |
f.bold = True
|
|
|
158 |
boldStyle.font = f
|
|
|
159 |
|
|
|
160 |
column = 0
|
|
|
161 |
row = 0
|
|
|
162 |
worksheet.write(row,column,'User Id',style)
|
|
|
163 |
worksheet.write(row,column+1,'Email',style)
|
|
|
164 |
worksheet.write(row,column+2,'Count',style)
|
|
|
165 |
worksheet.write(row,column+3,'Action',style)
|
|
|
166 |
|
|
|
167 |
cursor = conn.cursor()
|
|
|
168 |
cursor.execute(datesql)
|
|
|
169 |
result = cursor.fetchall()
|
|
|
170 |
|
|
|
171 |
for r in result:
|
|
|
172 |
row=row+1
|
|
|
173 |
worksheet.write(row,column,r[0])
|
|
|
174 |
worksheet.write(row,column+1,r[1])
|
|
|
175 |
worksheet.write(row,column+2,r[2])
|
|
|
176 |
worksheet.write(row,column+3,r[3])
|
|
|
177 |
conn.close()
|
|
|
178 |
workbook.save(TMP_FILE)
|
|
|
179 |
|
|
|
180 |
def sendmail(email, message, fileName, title):
|
|
|
181 |
if email == "":
|
|
|
182 |
return
|
|
|
183 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
184 |
mailServer.ehlo()
|
|
|
185 |
mailServer.starttls()
|
|
|
186 |
mailServer.ehlo()
|
|
|
187 |
msg = MIMEMultipart()
|
|
|
188 |
msg['Subject'] = title
|
|
|
189 |
msg.preamble = title
|
|
|
190 |
fileMsg = MIMEBase('application', 'vnd.ms-excel')
|
|
|
191 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
192 |
encoders.encode_base64(fileMsg)
|
|
|
193 |
fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
|
|
194 |
msg.attach(fileMsg)
|
| 20046 |
rajender |
195 |
MAILTO = ['rajender.singh@saholic.com']
|
| 16336 |
manas |
196 |
mailServer.login(SENDER, PASSWORD)
|
|
|
197 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
198 |
|
|
|
199 |
def main():
|
|
|
200 |
generateProductWiseReport()
|
|
|
201 |
generateUserCountFavoritesReport()
|
|
|
202 |
generateUserProductReport()
|
| 20046 |
rajender |
203 |
sendmail(["rajender.singh@shop2020.in"], "", TMP_FILE, SUBJECT)
|
| 16336 |
manas |
204 |
|
|
|
205 |
if __name__=='__main__':
|
|
|
206 |
main()
|