| Line 49... |
Line 49... |
| 49 |
left join (select distinct user_id from brand_preferences) bp on bp.user_id=u.id
|
49 |
left join (select distinct user_id from brand_preferences) bp on bp.user_id=u.id
|
| 50 |
left join (select *, count(*) as total 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 user_id) as ow on ow.user_id = u.id
|
50 |
left join (select *, count(*) as total 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 user_id) as ow on ow.user_id = u.id
|
| 51 |
left join retailerlinks rl on rl.user_id=u.id left join retailers r on r.id=rl.retailer_id where lower(u.referrer) not like 'emp%' or u.referrer is null) final;
|
51 |
left join retailerlinks rl on rl.user_id=u.id left join retailers r on r.id=rl.retailer_id where lower(u.referrer) not like 'emp%' or u.referrer is null) final;
|
| 52 |
"""
|
52 |
"""
|
| 53 |
|
53 |
|
| - |
|
54 |
GROUP_SEGMENTATION_QUERY = """
|
| - |
|
55 |
select case
|
| - |
|
56 |
when created >= date(now()) - interval 3 day and total > 0 then 'ABU'
|
| - |
|
57 |
when created < date(now()) - interval 3 day and total > 0 then 'IBU'
|
| - |
|
58 |
when last_active >= date(now()) - interval 3 day and total = 0 and date(last_active) <> date(ucreated) then 'ANBU'
|
| - |
|
59 |
when ucreated < date(now()) - interval 3 day and total = 0 and last_active < date(now()) - interval 3 day then 'INU'
|
| - |
|
60 |
when ucreated >= date(now()) - interval 3 day and total = 0 and date(last_active) = date(ucreated) then 'FIU'
|
| - |
|
61 |
else 'OTH'
|
| - |
|
62 |
end
|
| - |
|
63 |
usersegment, final.*
|
| - |
|
64 |
from (select u.id, u.username, u.first_name, u.mobile_number, u.usergroup_id, ug.groupbasis,
|
| - |
|
65 |
max(u.created) ucreated, min(u.created) minucreated, max(ua.last_active) last_active, max(ow.created) created, if(sum(ow.total) is null, 0, sum(ow.total)) as total
|
| - |
|
66 |
from users u left join useractive ua on ua.user_id=u.id left join usergroups ug on ug.id=u.usergroup_id
|
| - |
|
67 |
left join (select *, count(*) as total from (select user_id, created from order_view where status in ('ORDER_CREATED', 'DETAIL_CREATED')
|
| - |
|
68 |
union all (select user_id, created from flipkartorders where date(created) >'2015-03-22') order by created desc) as a1 group by user_id) as ow
|
| - |
|
69 |
on ow.user_id = u.id
|
| - |
|
70 |
where lower(u.referrer) not like 'emp%' or u.referrer is null group by usergroup_id) final;
|
| - |
|
71 |
"""
|
| - |
|
72 |
|
| 54 |
|
73 |
|
| 55 |
date_format = xlwt.XFStyle()
|
74 |
date_format = xlwt.XFStyle()
|
| 56 |
date_format.num_format_str = 'dd/mm/yyyy'
|
75 |
date_format.num_format_str = 'dd/mm/yyyy'
|
| 57 |
|
76 |
|
| 58 |
datetime_format = xlwt.XFStyle()
|
77 |
datetime_format = xlwt.XFStyle()
|
| Line 113... |
Line 132... |
| 113 |
column = 0
|
132 |
column = 0
|
| 114 |
for data in r :
|
133 |
for data in r :
|
| 115 |
worksheet.write(row, column, int(data) if type(data) is float else data, datetime_format if type(data) is datetime.datetime else default_format)
|
134 |
worksheet.write(row, column, int(data) if type(data) is float else data, datetime_format if type(data) is datetime.datetime else default_format)
|
| 116 |
column += 1
|
135 |
column += 1
|
| 117 |
workbook.save(TMP_FILE)
|
136 |
workbook.save(TMP_FILE)
|
| - |
|
137 |
generateGroupSegmentationReport(workbook)
|
| 118 |
sendmail(["rajneesh.arora@saholic.com"], "", TMP_FILE, SUBJECT)
|
138 |
sendmail(["rajneesh.arora@saholic.com"], "", TMP_FILE, SUBJECT)
|
| - |
|
139 |
#sendmail([], "", TMP_FILE, SUBJECT)
|
| - |
|
140 |
except:
|
| - |
|
141 |
traceback.print_exc()
|
| - |
|
142 |
print "Could not create report"
|
| - |
|
143 |
|
| - |
|
144 |
def generateGroupSegmentationReport(workbook):
|
| - |
|
145 |
selectSql = GROUP_SEGMENTATION_QUERY
|
| - |
|
146 |
conn = getDbConnection()
|
| - |
|
147 |
try:
|
| - |
|
148 |
# prepare a cursor object using cursor() method
|
| - |
|
149 |
cursor = conn.cursor()
|
| - |
|
150 |
# Execute the SQL command
|
| - |
|
151 |
# Fetch source id.
|
| - |
|
152 |
cursor.execute(selectSql)
|
| - |
|
153 |
result = cursor.fetchall()
|
| - |
|
154 |
worksheet = workbook.add_sheet("Segmented User Group")
|
| - |
|
155 |
boldStyle = xlwt.XFStyle()
|
| - |
|
156 |
f = xlwt.Font()
|
| - |
|
157 |
f.bold = True
|
| - |
|
158 |
boldStyle.font = f
|
| - |
|
159 |
column = 0
|
| - |
|
160 |
row = 0
|
| - |
|
161 |
worksheet.write(row, 0, 'User Segment', boldStyle)
|
| - |
|
162 |
worksheet.write(row, 1, 'User Id', boldStyle)
|
| - |
|
163 |
worksheet.write(row, 2, 'Username', boldStyle)
|
| - |
|
164 |
worksheet.write(row, 3, 'Name', boldStyle)
|
| - |
|
165 |
worksheet.write(row, 4, 'Mobile', boldStyle)
|
| - |
|
166 |
worksheet.write(row, 5, 'UserGroupId', boldStyle)
|
| - |
|
167 |
worksheet.write(row, 6, 'Group basis', boldStyle)
|
| - |
|
168 |
worksheet.write(row, 7, 'User last created', boldStyle)
|
| - |
|
169 |
worksheet.write(row, 8, 'User first created', boldStyle)
|
| - |
|
170 |
worksheet.write(row, 9, 'User Last Active On', boldStyle)
|
| - |
|
171 |
worksheet.write(row, 10, 'Order Last Created On', boldStyle)
|
| - |
|
172 |
worksheet.write(row, 11, 'Total Orders', boldStyle)
|
| - |
|
173 |
for r in result:
|
| - |
|
174 |
row += 1
|
| - |
|
175 |
column = 0
|
| - |
|
176 |
for data in r :
|
| - |
|
177 |
worksheet.write(row, column, int(data) if type(data) is float else data, datetime_format if type(data) is datetime.datetime else default_format)
|
| - |
|
178 |
column += 1
|
| - |
|
179 |
workbook.save(TMP_FILE)
|
| 119 |
except:
|
180 |
except:
|
| 120 |
traceback.print_exc()
|
181 |
traceback.print_exc()
|
| 121 |
print "Could not create report"
|
182 |
print "Could not create report"
|
| 122 |
|
183 |
|
| 123 |
def sendmail(email, message, fileName, title):
|
184 |
def sendmail(email, message, fileName, title):
|