| 15849 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on Jul 10, 2015
|
|
|
3 |
|
|
|
4 |
@author: amit
|
|
|
5 |
'''
|
|
|
6 |
from datetime import date
|
|
|
7 |
from dtr.reports.usersegmentation import sendmail
|
|
|
8 |
from email import encoders
|
|
|
9 |
from email.mime.base import MIMEBase
|
|
|
10 |
from email.mime.multipart import MIMEMultipart
|
|
|
11 |
from email.mime.text import MIMEText
|
|
|
12 |
import MySQLdb
|
|
|
13 |
import datetime
|
|
|
14 |
import smtplib
|
|
|
15 |
import traceback
|
|
|
16 |
import xlwt
|
|
|
17 |
|
|
|
18 |
'''
|
|
|
19 |
Created on Mar 10, 2015
|
|
|
20 |
|
|
|
21 |
@author: amit
|
|
|
22 |
'''
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
DB_HOST = "localhost"
|
|
|
27 |
DB_USER = "root"
|
|
|
28 |
DB_PASSWORD = "shop2020"
|
|
|
29 |
DB_NAME = "dtr"
|
|
|
30 |
TMP_FILE = "/tmp/cohortreport.xls"
|
|
|
31 |
|
|
|
32 |
# KEY NAMES
|
|
|
33 |
SENDER = "cnc.center@shop2020.in"
|
|
|
34 |
PASSWORD = "5h0p2o2o"
|
|
|
35 |
SUBJECT = "DTR User Segmentation report for " + date.today().isoformat()
|
|
|
36 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
37 |
SMTP_PORT = 587
|
| 15850 |
amit.gupta |
38 |
buyer="""select year(u.created) creationyear,month(u.created) creationmonth, uu.yearly, uu.monthly,count(*) from users u join (select user_id, year(created) yearly, month(created) monthly from (select user_id, created from order_view where status in ('ORDER_CREATED', 'DETAIL_CREATED') union all (select user_id, created from flipkartorders where date(created) >'2015-03-22') order by created desc) as a1 group by a1.user_id, year(a1.created),month(a1.created)) uu on u.id = uu.user_id where u.activated = 1 and (lower(u.referrer) not like 'emp%' or u.utm_campaign is not null) group by year(u.created), month(u.created), uu.yearly, uu.monthly;"""
|
| 15849 |
amit.gupta |
39 |
|
| 17744 |
manas |
40 |
activeuser="""select year(u.created) creationyear, month(u.created) creationmonth, uu.yearly, uu.monthly,count(*) from users u join (select user_id, year(visited) yearly,month(visited) monthly from daily_visitors group by user_id, year(visited), month(visited)) uu on u.id = uu.user_id where u.activated = 1 and (lower(u.referrer) not like 'emp%' or u.utm_campaign is not null) group by year(u.created), month(u.created),uu.yearly, uu.monthly;"""
|
| 15849 |
amit.gupta |
41 |
|
| 17744 |
manas |
42 |
activegroup = """select year(created) creationmonth,month(created) creationmonth, a.yearly, a.monthly, count(*) from (select u.usergroup_id, min(u.created) created, uu.yearly,uu.monthly from users u join (select user_id, year(visited) yearly,month(visited) monthly from daily_visitors group by user_id, year(visited),month(visited)) uu on u.id = uu.user_id where u.activated = 1 and (lower(u.referrer) not like 'emp%' or u.utm_campaign is not null) group by u.usergroup_id, uu.yearly, uu.monthly) a group by year(created), month(created), a.yearly, a.monthly;"""
|
| 15849 |
amit.gupta |
43 |
|
|
|
44 |
|
|
|
45 |
buyergroup = """select year(created) creationmonth, month(created) creationmonth, a.yearly, a.monthly, count(*) from (select u.usergroup_id, min(u.created) created, uu.yearly,uu.monthly from users u join (select user_id, year(created) yearly, month(created) monthly from order_view where status in ('ORDER_CREATED', 'DETAIL_CREATED') union all select user_id, year(created) yearly, month(created) monthly from flipkartorders where date(created) >'2015-03-22' group by user_id, year(created), month(created)) uu on u.id = uu.user_id where u.activated = 1 and (lower(u.referrer) not like 'emp%' or u.utm_campaign is not null) group by u.usergroup_id, uu.monthly) a group by year(created), month(created),a.yearly,a.monthly;"""
|
|
|
46 |
|
| 15850 |
amit.gupta |
47 |
months = {1:"Jan",2:"Feb",3:"Mar",4:"Apr",5:"May",6:"Jun",7:"Jul",8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}
|
| 15849 |
amit.gupta |
48 |
|
|
|
49 |
def getDbConnection():
|
|
|
50 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
51 |
|
|
|
52 |
boldStyle = xlwt.XFStyle()
|
|
|
53 |
f = xlwt.Font()
|
|
|
54 |
f.bold = True
|
|
|
55 |
boldStyle.font = f
|
|
|
56 |
|
|
|
57 |
def main():
|
|
|
58 |
conn = getDbConnection()
|
|
|
59 |
workbook = xlwt.Workbook()
|
|
|
60 |
worksheet = workbook.add_sheet("Cohort Analysis")
|
|
|
61 |
try:
|
|
|
62 |
cursor = conn.cursor()
|
|
|
63 |
cursor.execute(activeuser)
|
|
|
64 |
result = cursor.fetchall()
|
|
|
65 |
nextrow = parseResultToSheet(worksheet,result, "Active Users", 0)
|
|
|
66 |
cursor = conn.cursor()
|
|
|
67 |
cursor.execute(buyer)
|
|
|
68 |
result = cursor.fetchall()
|
|
|
69 |
nextrow = parseResultToSheet(worksheet,result, "Buyers", nextrow)
|
|
|
70 |
cursor = conn.cursor()
|
|
|
71 |
cursor.execute(activegroup)
|
|
|
72 |
result = cursor.fetchall()
|
|
|
73 |
nextrow = parseResultToSheet(worksheet,result, "Active Groups", nextrow)
|
|
|
74 |
cursor = conn.cursor()
|
|
|
75 |
cursor.execute(buyergroup)
|
|
|
76 |
result = cursor.fetchall()
|
|
|
77 |
parseResultToSheet(worksheet,result, "Buyer Groups", nextrow)
|
|
|
78 |
|
|
|
79 |
workbook.save("/tmp/cohortreport.xls")
|
| 20046 |
rajender |
80 |
sendmail(['rajneesh.arora@saholic.com','rajender.singh@shop2020.in'], "", "/tmp/cohortreport.xls", "Report for Cohort analysis")
|
| 15849 |
amit.gupta |
81 |
finally:
|
|
|
82 |
conn.close()
|
|
|
83 |
|
|
|
84 |
def parseResultToSheet(sheet, result, title, startrow):
|
|
|
85 |
sheet.write(startrow, 0, title, boldStyle)
|
|
|
86 |
x=startrow
|
|
|
87 |
y=0
|
|
|
88 |
creationMap={}
|
|
|
89 |
activityMap={}
|
|
|
90 |
for row in result:
|
| 15850 |
amit.gupta |
91 |
creationym = ("%s %s")%(months[row[1]],row[0])
|
|
|
92 |
activityym = ("%s %s")%(months[row[3]], row[2])
|
| 15849 |
amit.gupta |
93 |
if creationym not in creationMap:
|
|
|
94 |
x += 1
|
|
|
95 |
creationMap[creationym] = x
|
|
|
96 |
sheet.write(x,0, creationym, boldStyle)
|
|
|
97 |
if activityym not in activityMap:
|
|
|
98 |
y+=1
|
|
|
99 |
activityMap[activityym] = y
|
|
|
100 |
sheet.write(startrow,y, activityym,boldStyle)
|
|
|
101 |
|
|
|
102 |
sheet.write(creationMap[creationym],activityMap[activityym], row[4])
|
|
|
103 |
return x+2
|
|
|
104 |
|
|
|
105 |
def sendmail(email, message, fileName, title):
|
|
|
106 |
if email == "":
|
|
|
107 |
return
|
|
|
108 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
109 |
mailServer.ehlo()
|
|
|
110 |
mailServer.starttls()
|
|
|
111 |
mailServer.ehlo()
|
|
|
112 |
|
|
|
113 |
# Create the container (outer) email message.
|
|
|
114 |
msg = MIMEMultipart()
|
|
|
115 |
msg['Subject'] = title
|
|
|
116 |
msg.preamble = title
|
|
|
117 |
html_msg = MIMEText(message, 'html')
|
|
|
118 |
msg.attach(html_msg)
|
|
|
119 |
|
|
|
120 |
fileMsg = MIMEBase('application', 'vnd.ms-excel')
|
|
|
121 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
122 |
encoders.encode_base64(fileMsg)
|
|
|
123 |
fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
|
|
124 |
msg.attach(fileMsg)
|
|
|
125 |
email.append('amit.gupta@shop2020.in')
|
|
|
126 |
MAILTO = email
|
|
|
127 |
mailServer.login(SENDER, PASSWORD)
|
|
|
128 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
if __name__ == '__main__':
|
|
|
132 |
main()
|