| 14772 |
kshitij.so |
1 |
'''
|
|
|
2 |
Created on Mar 10, 2015
|
|
|
3 |
|
|
|
4 |
'''
|
|
|
5 |
from datetime import date, datetime, timedelta
|
|
|
6 |
from dtr.storage.Mysql import getOrdersAfterDate, \
|
|
|
7 |
getOrdersByTag
|
|
|
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 |
from pymongo.mongo_client import MongoClient
|
|
|
13 |
from xlrd import open_workbook
|
|
|
14 |
from xlutils.copy import copy
|
|
|
15 |
from xlwt.Workbook import Workbook
|
|
|
16 |
import MySQLdb
|
|
|
17 |
import smtplib
|
|
|
18 |
import time
|
|
|
19 |
import xlwt
|
| 15228 |
manas |
20 |
import pymongo
|
|
|
21 |
from shop2020.utils.Utils import to_py_date, to_java_date
|
|
|
22 |
from datetime import datetime
|
|
|
23 |
from elixir import *
|
|
|
24 |
from dtr.storage import DataService
|
|
|
25 |
from dtr.storage.DataService import Orders, Users, CallHistory
|
|
|
26 |
from sqlalchemy.sql.expression import func
|
| 14772 |
kshitij.so |
27 |
|
|
|
28 |
|
|
|
29 |
DB_HOST = "localhost"
|
|
|
30 |
DB_USER = "root"
|
|
|
31 |
DB_PASSWORD = "shop2020"
|
|
|
32 |
DB_NAME = "dtr"
|
|
|
33 |
TMP_FILE = "User_Activity_Report.xls"
|
|
|
34 |
|
| 15228 |
manas |
35 |
con = None
|
|
|
36 |
dateWiseOrderMap = {}
|
|
|
37 |
weekWiseOrderMap = {}
|
|
|
38 |
monthWiseOrderMap = {}
|
|
|
39 |
orderIds = []
|
|
|
40 |
cutOff = 1425839400
|
|
|
41 |
#cutOff = 1425234600
|
|
|
42 |
oneDay = 86400
|
|
|
43 |
monthCutOff = 1425148200
|
|
|
44 |
weekCutOff = 1425839400
|
|
|
45 |
#weekCutOff = 1425234600
|
|
|
46 |
|
|
|
47 |
DataService.initialize()
|
|
|
48 |
|
|
|
49 |
|
| 14772 |
kshitij.so |
50 |
# KEY NAMES
|
|
|
51 |
SENDER = "cnc.center@shop2020.in"
|
|
|
52 |
PASSWORD = "5h0p2o2o"
|
|
|
53 |
SUBJECT = "User Activity Report for" + date.today().isoformat()
|
|
|
54 |
SMTP_SERVER = "smtp.gmail.com"
|
|
|
55 |
SMTP_PORT = 587
|
|
|
56 |
|
|
|
57 |
DATE_QUERY="""
|
|
|
58 |
SELECT date(d.visited) from daily_visitors d
|
| 14871 |
manas |
59 |
join users u where u.id=d.user_id AND LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
60 |
AND date(d.visited) > '2015-03-08' group by visited ;
|
|
|
61 |
"""
|
|
|
62 |
|
|
|
63 |
MONTH_QUERY="""
|
|
|
64 |
SELECT month(d.visited) from daily_visitors d
|
| 14871 |
manas |
65 |
join users u on u.id=d.user_id where LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
66 |
group by month(d.visited);
|
|
|
67 |
"""
|
|
|
68 |
|
|
|
69 |
WEEK_QUERY="""
|
|
|
70 |
SELECT CONCAT(date(o.created), ' - ', date(o.created) + INTERVAL 6 DAY) AS week
|
|
|
71 |
FROM order_view o
|
|
|
72 |
JOIN users u ON u.id = o.user_id
|
| 14871 |
manas |
73 |
WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
74 |
AND date(o.created) > '2015-03-08'
|
|
|
75 |
GROUP BY WEEK(date(o.created))
|
|
|
76 |
ORDER BY WEEK(date(o.created))
|
|
|
77 |
"""
|
|
|
78 |
|
|
|
79 |
DNRU_QUERY="""
|
|
|
80 |
SELECT count(*)
|
| 14871 |
manas |
81 |
FROM users u WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
82 |
AND date(u.created) > '2015-03-08'
|
|
|
83 |
group by date(u.created)
|
|
|
84 |
order by date(u.created)
|
|
|
85 |
"""
|
|
|
86 |
|
|
|
87 |
DAU_QUERY="""
|
|
|
88 |
SELECT count(distinct d.user_id)
|
| 14871 |
manas |
89 |
FROM daily_visitors d join users u where u.id=d.user_id AND LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
90 |
AND date(d.visited) > '2015-03-08' group by visited ;
|
|
|
91 |
"""
|
|
|
92 |
|
|
|
93 |
DAB_QUERY="""
|
|
|
94 |
SELECT COUNT(DISTINCT o.user_id )
|
|
|
95 |
FROM order_view o
|
|
|
96 |
JOIN users u ON u.id = o.user_id
|
| 14871 |
manas |
97 |
WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
98 |
AND o.STATUS = 'ORDER_CREATED'
|
|
|
99 |
AND date(o.created) > '2015-03-08'
|
|
|
100 |
group by date(o.created)
|
|
|
101 |
order by date(o.created)
|
|
|
102 |
"""
|
|
|
103 |
|
|
|
104 |
DTO_QUERY="""
|
|
|
105 |
SELECT COUNT( *)
|
|
|
106 |
FROM order_view o
|
|
|
107 |
JOIN users u ON u.id = o.user_id
|
| 14871 |
manas |
108 |
WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
109 |
AND o.STATUS = 'ORDER_CREATED'
|
|
|
110 |
AND date(o.created) > '2015-03-08'
|
|
|
111 |
GROUP BY DATE(o.created)
|
|
|
112 |
order by date(o.created)
|
|
|
113 |
"""
|
|
|
114 |
|
|
|
115 |
MNRU_QUERY="""
|
|
|
116 |
SELECT count(*)
|
| 14871 |
manas |
117 |
FROM users u WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
118 |
AND date(u.created) > '2015-03-08'
|
|
|
119 |
group by month(u.created)
|
|
|
120 |
order by month(u.created)
|
|
|
121 |
"""
|
|
|
122 |
|
|
|
123 |
MAU_QUERY="""
|
|
|
124 |
SELECT count(distinct d.user_id)
|
| 14871 |
manas |
125 |
FROM daily_visitors d join users u where u.id=d.user_id AND LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
126 |
AND date(d.visited) > '2015-03-08'
|
|
|
127 |
group by month(d.visited)
|
|
|
128 |
order by month(d.visited);
|
|
|
129 |
"""
|
|
|
130 |
|
|
|
131 |
MAB_QUERY="""
|
|
|
132 |
SELECT COUNT(DISTINCT o.user_id )
|
|
|
133 |
FROM order_view o
|
|
|
134 |
JOIN users u ON u.id = o.user_id where
|
| 14871 |
manas |
135 |
LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
136 |
AND o.STATUS = 'ORDER_CREATED'
|
|
|
137 |
AND date(o.created) > '2015-03-08'
|
|
|
138 |
group by month(o.created)
|
|
|
139 |
order by month(o.created);
|
|
|
140 |
"""
|
|
|
141 |
|
|
|
142 |
MTO_QUERY="""
|
|
|
143 |
SELECT COUNT( *)
|
|
|
144 |
FROM order_view o
|
|
|
145 |
JOIN users u ON u.id = o.user_id where
|
| 14871 |
manas |
146 |
LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
147 |
AND o.STATUS = 'ORDER_CREATED'
|
|
|
148 |
AND date(o.created) > '2015-03-08'
|
|
|
149 |
GROUP BY MONTH(o.created)
|
|
|
150 |
order by month(o.created);
|
|
|
151 |
"""
|
|
|
152 |
|
|
|
153 |
WNRU_QUERY="""
|
|
|
154 |
SELECT COUNT(*)
|
|
|
155 |
FROM users u WHERE
|
| 14871 |
manas |
156 |
LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
157 |
AND date(u.created) > '2015-03-08'
|
|
|
158 |
GROUP BY WEEK(date(u.created))
|
|
|
159 |
ORDER BY WEEK(date(u.created))
|
|
|
160 |
|
|
|
161 |
"""
|
|
|
162 |
WAU_QUERY="""
|
|
|
163 |
SELECT COUNT(distinct d.user_id) AS total
|
| 14871 |
manas |
164 |
FROM daily_visitors d join users u where u.id=d.user_id AND LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
165 |
AND date(d.visited) > '2015-03-08'
|
|
|
166 |
GROUP BY WEEK(d.visited)
|
|
|
167 |
ORDER BY WEEK(d.visited)
|
|
|
168 |
"""
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
WAB_QUERY="""
|
|
|
172 |
SELECT COUNT( DISTINCT o.user_id )
|
|
|
173 |
FROM order_view o
|
|
|
174 |
JOIN users u ON u.id = o.user_id
|
| 14871 |
manas |
175 |
WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
176 |
AND o.STATUS = 'ORDER_CREATED'
|
|
|
177 |
AND date(o.created) > '2015-03-08'
|
|
|
178 |
GROUP BY WEEK(date(o.created))
|
|
|
179 |
ORDER BY WEEK(date(o.created))
|
|
|
180 |
"""
|
|
|
181 |
|
|
|
182 |
WTO_QUERY="""
|
|
|
183 |
SELECT COUNT(*)
|
|
|
184 |
FROM order_view o
|
|
|
185 |
JOIN users u ON u.id = o.user_id
|
| 14871 |
manas |
186 |
WHERE LOWER(u.referrer) NOT LIKE 'emp%' AND u.activated =1
|
| 14772 |
kshitij.so |
187 |
AND o.STATUS = 'ORDER_CREATED'
|
|
|
188 |
AND date(o.created) > '2015-03-08'
|
|
|
189 |
GROUP BY WEEK(date(o.created))
|
|
|
190 |
ORDER BY WEEK(date(o.created))
|
|
|
191 |
"""
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
date_format = xlwt.XFStyle()
|
|
|
195 |
date_format.num_format_str = 'yyyy/mm/dd'
|
|
|
196 |
|
|
|
197 |
datetime_format = xlwt.XFStyle()
|
|
|
198 |
datetime_format.num_format_str = 'yyyy/mm/dd HH:MM AM/PM'
|
|
|
199 |
|
|
|
200 |
default_format = xlwt.XFStyle()
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
def getDbConnection():
|
|
|
204 |
return MySQLdb.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
def generateDailyReport():
|
|
|
208 |
datesql= DATE_QUERY
|
|
|
209 |
dnruSql = DNRU_QUERY
|
|
|
210 |
dauSql = DAU_QUERY
|
|
|
211 |
dabSql = DAB_QUERY
|
|
|
212 |
dtoSql = DTO_QUERY
|
|
|
213 |
conn = getDbConnection()
|
|
|
214 |
|
|
|
215 |
# prepare a cursor object using cursor() method
|
|
|
216 |
cursor = conn.cursor()
|
|
|
217 |
# Execute the SQL command
|
|
|
218 |
# Fetch source id.
|
|
|
219 |
cursor.execute(datesql)
|
|
|
220 |
result = cursor.fetchall()
|
|
|
221 |
global workbook
|
|
|
222 |
workbook = xlwt.Workbook()
|
|
|
223 |
worksheet = workbook.add_sheet("User")
|
|
|
224 |
boldStyle = xlwt.XFStyle()
|
|
|
225 |
f = xlwt.Font()
|
|
|
226 |
f.bold = True
|
|
|
227 |
boldStyle.font = f
|
|
|
228 |
column = 0
|
|
|
229 |
row = 0
|
|
|
230 |
sumdata=17
|
| 15228 |
manas |
231 |
global z
|
| 14772 |
kshitij.so |
232 |
worksheet.write(row, 0, 'Date', boldStyle)
|
|
|
233 |
worksheet.write(row, 1, 'TRU', boldStyle)
|
|
|
234 |
worksheet.write(row, 2, 'NRU', boldStyle)
|
|
|
235 |
worksheet.write(row, 3, 'DAU', boldStyle)
|
|
|
236 |
worksheet.write(row, 4, 'DAB', boldStyle)
|
|
|
237 |
worksheet.write(row, 5, 'DTO', boldStyle)
|
| 15228 |
manas |
238 |
worksheet.write(row, 6, 'DSO', boldStyle)
|
|
|
239 |
worksheet.write(row, 7, 'DTV', boldStyle)
|
| 14772 |
kshitij.so |
240 |
for r in result:
|
|
|
241 |
row += 1
|
|
|
242 |
column = 0
|
|
|
243 |
for data in r :
|
|
|
244 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
245 |
column += 1
|
|
|
246 |
|
|
|
247 |
row = 0
|
|
|
248 |
cursor.execute(dnruSql)
|
|
|
249 |
result = cursor.fetchall()
|
|
|
250 |
for r in result:
|
|
|
251 |
row += 1
|
|
|
252 |
column = 1
|
|
|
253 |
for data in r :
|
|
|
254 |
print "Data" + str(data)
|
|
|
255 |
sumdata=sumdata+data
|
|
|
256 |
print "Sum Data" + str(sumdata)
|
|
|
257 |
worksheet.write(row, column, int(sumdata) if type(sumdata) is float else sumdata, date_format if type(sumdata) is date else default_format)
|
|
|
258 |
column += 1
|
|
|
259 |
|
|
|
260 |
row = 0
|
|
|
261 |
cursor.execute(dnruSql)
|
|
|
262 |
result = cursor.fetchall()
|
|
|
263 |
for r in result:
|
|
|
264 |
row += 1
|
|
|
265 |
column = 2
|
|
|
266 |
for data in r :
|
|
|
267 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
268 |
column += 1
|
|
|
269 |
|
|
|
270 |
row = 0
|
|
|
271 |
cursor.execute(dauSql)
|
|
|
272 |
result = cursor.fetchall()
|
|
|
273 |
|
|
|
274 |
for r in result:
|
|
|
275 |
row += 1
|
|
|
276 |
column = 3
|
|
|
277 |
for data in r :
|
|
|
278 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
279 |
column += 1
|
|
|
280 |
|
|
|
281 |
row = 0
|
|
|
282 |
cursor.execute(dabSql)
|
|
|
283 |
result = cursor.fetchall()
|
|
|
284 |
|
|
|
285 |
for r in result:
|
|
|
286 |
row += 1
|
|
|
287 |
column = 4
|
|
|
288 |
for data in r :
|
|
|
289 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
290 |
column += 1
|
|
|
291 |
|
|
|
292 |
row = 0
|
|
|
293 |
cursor.execute(dtoSql)
|
|
|
294 |
result = cursor.fetchall()
|
|
|
295 |
|
|
|
296 |
for r in result:
|
|
|
297 |
row += 1
|
|
|
298 |
column = 5
|
|
|
299 |
for data in r :
|
|
|
300 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
301 |
column += 1
|
| 15228 |
manas |
302 |
|
|
|
303 |
row=0
|
| 14772 |
kshitij.so |
304 |
|
| 15228 |
manas |
305 |
z=0
|
|
|
306 |
for x in sorted(dateWiseOrderMap):
|
|
|
307 |
row += 1
|
|
|
308 |
column = 6
|
|
|
309 |
worksheet.write(row,column,dateWiseOrderMap.get(x).count)
|
|
|
310 |
column += 1
|
| 14772 |
kshitij.so |
311 |
|
| 15228 |
manas |
312 |
row =0
|
|
|
313 |
for x in sorted(dateWiseOrderMap):
|
|
|
314 |
row += 1
|
|
|
315 |
column = 7
|
|
|
316 |
worksheet.write(row,column,dateWiseOrderMap.get(x).value)
|
|
|
317 |
column += 1
|
|
|
318 |
|
| 14772 |
kshitij.so |
319 |
def generateMonthlyReport():
|
|
|
320 |
monthSql = MONTH_QUERY
|
|
|
321 |
mnruSql = MNRU_QUERY
|
|
|
322 |
mauSql = MAU_QUERY
|
|
|
323 |
mabSql = MAB_QUERY
|
|
|
324 |
mtoSql = MTO_QUERY
|
|
|
325 |
|
|
|
326 |
conn = getDbConnection()
|
|
|
327 |
|
|
|
328 |
cursor = conn.cursor()
|
|
|
329 |
|
|
|
330 |
cursor.execute(monthSql)
|
|
|
331 |
result = cursor.fetchall()
|
|
|
332 |
rb = open_workbook(TMP_FILE)
|
|
|
333 |
wb = copy(rb)
|
|
|
334 |
worksheet = workbook.add_sheet("Monthly")
|
|
|
335 |
boldStyle = xlwt.XFStyle()
|
|
|
336 |
f = xlwt.Font()
|
|
|
337 |
f.bold = True
|
|
|
338 |
boldStyle.font = f
|
|
|
339 |
column = 0
|
|
|
340 |
row = 0
|
|
|
341 |
sumdata=17
|
|
|
342 |
worksheet.write(row, 0, 'Month', boldStyle)
|
|
|
343 |
worksheet.write(row, 1, 'MTRU', boldStyle)
|
|
|
344 |
worksheet.write(row, 2, 'MNRU', boldStyle)
|
|
|
345 |
worksheet.write(row, 3, 'MAU', boldStyle)
|
|
|
346 |
worksheet.write(row, 4, 'MAB', boldStyle)
|
|
|
347 |
worksheet.write(row, 5, 'MTO', boldStyle)
|
|
|
348 |
|
|
|
349 |
|
|
|
350 |
for r in result:
|
|
|
351 |
row += 1
|
|
|
352 |
column = 0
|
|
|
353 |
for data in r :
|
|
|
354 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
355 |
column += 1
|
|
|
356 |
|
|
|
357 |
row = 0
|
|
|
358 |
cursor.execute(mnruSql)
|
|
|
359 |
result = cursor.fetchall()
|
|
|
360 |
for r in result:
|
|
|
361 |
row += 1
|
|
|
362 |
column = 1
|
|
|
363 |
for data in r :
|
|
|
364 |
print "Data" + str(data)
|
|
|
365 |
sumdata=sumdata+data
|
|
|
366 |
print "Sum Data" + str(sumdata)
|
|
|
367 |
worksheet.write(row, column, int(sumdata) if type(sumdata) is float else sumdata, date_format if type(sumdata) is date else default_format)
|
|
|
368 |
column += 1
|
|
|
369 |
|
|
|
370 |
row = 0
|
|
|
371 |
cursor.execute(mnruSql)
|
|
|
372 |
result = cursor.fetchall()
|
|
|
373 |
for r in result:
|
|
|
374 |
row += 1
|
|
|
375 |
column = 2
|
|
|
376 |
for data in r :
|
|
|
377 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
378 |
column += 1
|
|
|
379 |
|
|
|
380 |
row = 0
|
|
|
381 |
cursor.execute(mauSql)
|
|
|
382 |
result = cursor.fetchall()
|
|
|
383 |
|
|
|
384 |
for r in result:
|
|
|
385 |
row += 1
|
|
|
386 |
column = 3
|
|
|
387 |
for data in r :
|
|
|
388 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
389 |
column += 1
|
|
|
390 |
|
|
|
391 |
row = 0
|
|
|
392 |
cursor.execute(mabSql)
|
|
|
393 |
result = cursor.fetchall()
|
|
|
394 |
|
|
|
395 |
for r in result:
|
|
|
396 |
row += 1
|
|
|
397 |
column = 4
|
|
|
398 |
for data in r :
|
|
|
399 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
400 |
column += 1
|
|
|
401 |
|
|
|
402 |
row = 0
|
|
|
403 |
cursor.execute(mtoSql)
|
|
|
404 |
result = cursor.fetchall()
|
|
|
405 |
|
|
|
406 |
for r in result:
|
|
|
407 |
row += 1
|
|
|
408 |
column = 5
|
|
|
409 |
for data in r :
|
|
|
410 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
411 |
column += 1
|
|
|
412 |
|
|
|
413 |
workbook.save(TMP_FILE)
|
| 14805 |
kshitij.so |
414 |
sendmail(["manas.kapoor@shop2020.in","rajneesh.arora@saholic.com"], "", TMP_FILE, SUBJECT)
|
| 15228 |
manas |
415 |
#sendmail(["manas.kapoor@shop2020.in"], "", TMP_FILE, SUBJECT)
|
| 14772 |
kshitij.so |
416 |
|
|
|
417 |
def generateWeeklyReport():
|
|
|
418 |
weekSql = WEEK_QUERY
|
|
|
419 |
wnruSql = WNRU_QUERY
|
|
|
420 |
wauSql = WAU_QUERY
|
|
|
421 |
wabSql = WAB_QUERY
|
|
|
422 |
wtoSql = WTO_QUERY
|
|
|
423 |
|
|
|
424 |
conn = getDbConnection()
|
|
|
425 |
|
|
|
426 |
cursor = conn.cursor()
|
|
|
427 |
|
|
|
428 |
cursor.execute(weekSql)
|
|
|
429 |
result = cursor.fetchall()
|
|
|
430 |
rb = open_workbook(TMP_FILE)
|
|
|
431 |
wb = copy(rb)
|
|
|
432 |
worksheet = workbook.add_sheet("Weekly")
|
|
|
433 |
boldStyle = xlwt.XFStyle()
|
|
|
434 |
f = xlwt.Font()
|
|
|
435 |
f.bold = True
|
|
|
436 |
boldStyle.font = f
|
|
|
437 |
column = 0
|
|
|
438 |
row = 0
|
|
|
439 |
sumdata=17
|
|
|
440 |
worksheet.write(row, 0, 'Week', boldStyle)
|
|
|
441 |
worksheet.write(row, 1, 'WTRU', boldStyle)
|
|
|
442 |
worksheet.write(row, 2, 'WNRU', boldStyle)
|
|
|
443 |
worksheet.write(row, 3, 'WAU', boldStyle)
|
|
|
444 |
worksheet.write(row, 4, 'WAB', boldStyle)
|
|
|
445 |
worksheet.write(row, 5, 'WTO', boldStyle)
|
| 15228 |
manas |
446 |
worksheet.write(row, 6, 'WTS', boldStyle)
|
|
|
447 |
worksheet.write(row, 7, 'WTV', boldStyle)
|
| 14772 |
kshitij.so |
448 |
|
|
|
449 |
for r in result:
|
|
|
450 |
row += 1
|
|
|
451 |
column = 0
|
|
|
452 |
for data in r :
|
|
|
453 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
454 |
column += 1
|
|
|
455 |
|
|
|
456 |
row = 0
|
|
|
457 |
cursor.execute(wnruSql)
|
|
|
458 |
result = cursor.fetchall()
|
|
|
459 |
for r in result:
|
|
|
460 |
row += 1
|
|
|
461 |
column = 1
|
|
|
462 |
for data in r :
|
|
|
463 |
print "Data" + str(data)
|
|
|
464 |
sumdata=sumdata+data
|
|
|
465 |
print "Sum Data" + str(sumdata)
|
|
|
466 |
worksheet.write(row, column, int(sumdata) if type(sumdata) is float else sumdata, date_format if type(sumdata) is date else default_format)
|
|
|
467 |
column += 1
|
|
|
468 |
|
|
|
469 |
row = 0
|
|
|
470 |
cursor.execute(wnruSql)
|
|
|
471 |
result = cursor.fetchall()
|
|
|
472 |
for r in result:
|
|
|
473 |
row += 1
|
|
|
474 |
column = 2
|
|
|
475 |
for data in r :
|
|
|
476 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
477 |
column += 1
|
|
|
478 |
|
|
|
479 |
row = 0
|
|
|
480 |
cursor.execute(wauSql)
|
|
|
481 |
result = cursor.fetchall()
|
|
|
482 |
|
|
|
483 |
for r in result:
|
|
|
484 |
row += 1
|
|
|
485 |
column = 3
|
|
|
486 |
for data in r :
|
|
|
487 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
488 |
column += 1
|
|
|
489 |
|
|
|
490 |
row = 0
|
|
|
491 |
cursor.execute(wabSql)
|
|
|
492 |
result = cursor.fetchall()
|
|
|
493 |
|
|
|
494 |
for r in result:
|
|
|
495 |
row += 1
|
|
|
496 |
column = 4
|
|
|
497 |
for data in r :
|
|
|
498 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
499 |
column += 1
|
|
|
500 |
|
|
|
501 |
row = 0
|
|
|
502 |
cursor.execute(wtoSql)
|
|
|
503 |
result = cursor.fetchall()
|
|
|
504 |
|
|
|
505 |
for r in result:
|
|
|
506 |
row += 1
|
|
|
507 |
column = 5
|
|
|
508 |
for data in r :
|
|
|
509 |
worksheet.write(row, column, int(data) if type(data) is float else data, date_format if type(data) is date else default_format)
|
|
|
510 |
column += 1
|
| 15228 |
manas |
511 |
|
|
|
512 |
row = 0
|
|
|
513 |
for x in sorted(weekWiseOrderMap):
|
|
|
514 |
row += 1
|
|
|
515 |
column = 6
|
|
|
516 |
worksheet.write(row,column,weekWiseOrderMap.get(x).count)
|
|
|
517 |
column+=1
|
|
|
518 |
row = 0
|
|
|
519 |
for x in sorted(weekWiseOrderMap):
|
|
|
520 |
row += 1
|
|
|
521 |
column = 7
|
|
|
522 |
worksheet.write(row,column,weekWiseOrderMap.get(x).value)
|
|
|
523 |
column+=1
|
| 14772 |
kshitij.so |
524 |
|
|
|
525 |
|
|
|
526 |
def sendmail(email, message, fileName, title):
|
|
|
527 |
if email == "":
|
|
|
528 |
return
|
|
|
529 |
mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
|
|
|
530 |
mailServer.ehlo()
|
|
|
531 |
mailServer.starttls()
|
|
|
532 |
mailServer.ehlo()
|
|
|
533 |
|
|
|
534 |
# Create the container (outer) email message.
|
|
|
535 |
msg = MIMEMultipart()
|
|
|
536 |
msg['Subject'] = title
|
|
|
537 |
msg.preamble = title
|
|
|
538 |
html_msg = MIMEText(message, 'html')
|
|
|
539 |
msg.attach(html_msg)
|
|
|
540 |
|
|
|
541 |
fileMsg = MIMEBase('application', 'vnd.ms-excel')
|
|
|
542 |
fileMsg.set_payload(file(TMP_FILE).read())
|
|
|
543 |
encoders.encode_base64(fileMsg)
|
|
|
544 |
fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
|
|
|
545 |
msg.attach(fileMsg)
|
| 14805 |
kshitij.so |
546 |
|
|
|
547 |
MAILTO = ['manas.kapoor@saholic.com','rajneesh.arora@saholic.com']
|
| 15228 |
manas |
548 |
#MAILTO = ['manas.kapoor@saholic.com']
|
| 14772 |
kshitij.so |
549 |
mailServer.login(SENDER, PASSWORD)
|
|
|
550 |
mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
|
|
|
551 |
|
| 15228 |
manas |
552 |
class __Order:
|
|
|
553 |
|
|
|
554 |
def __init__(self, count, value):
|
|
|
555 |
|
|
|
556 |
self.count = count
|
|
|
557 |
self.value = value
|
|
|
558 |
|
|
|
559 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
560 |
global con
|
|
|
561 |
if con is None:
|
|
|
562 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
563 |
try:
|
|
|
564 |
con = pymongo.MongoClient(host, port)
|
|
|
565 |
except Exception, e:
|
|
|
566 |
print e
|
|
|
567 |
return None
|
|
|
568 |
return con
|
|
|
569 |
|
|
|
570 |
def populateOrderMap():
|
|
|
571 |
global dateWiseOrderMap
|
|
|
572 |
allOrders = get_mongo_connection().Dtr.merchantOrder.find({'createdOnInt':{"$gte":cutOff}}).sort([('createdOnInt',pymongo.ASCENDING)])
|
|
|
573 |
for orders in allOrders:
|
|
|
574 |
if orders.get('orderId') not in order_ids:
|
|
|
575 |
continue
|
|
|
576 |
cdate = ((to_py_date(orders.get('createdOnInt') * 1000)))
|
|
|
577 |
millisec = to_java_date(datetime(cdate.year, cdate.month, cdate.day))
|
|
|
578 |
if dateWiseOrderMap.has_key(millisec):
|
|
|
579 |
orderObj = dateWiseOrderMap.get(millisec)
|
|
|
580 |
q, c = getSubOrderQuantity(orders.get('subOrders'))
|
|
|
581 |
orderObj.count += q
|
|
|
582 |
orderObj.value += c
|
|
|
583 |
else:
|
|
|
584 |
orderObj = __Order(None, None)
|
|
|
585 |
q, c = getSubOrderQuantity(orders.get('subOrders'))
|
|
|
586 |
orderObj.count = q
|
|
|
587 |
orderObj.value = c
|
|
|
588 |
dateWiseOrderMap[millisec] = orderObj
|
|
|
589 |
|
|
|
590 |
def getSubOrderQuantity(subOrders):
|
|
|
591 |
q = 0
|
|
|
592 |
c = 0
|
|
|
593 |
if subOrders is None:
|
|
|
594 |
return q, c
|
|
|
595 |
for subOrder in subOrders:
|
|
|
596 |
q = q + int(subOrder.get('quantity'))
|
|
|
597 |
try:
|
|
|
598 |
c = c + float(subOrder.get('amountPaid').encode('utf-8'))
|
|
|
599 |
except AttributeError:
|
|
|
600 |
c = c + float(subOrder.get('amountPaid'))
|
|
|
601 |
return q, c
|
|
|
602 |
|
|
|
603 |
def populateWeekWiseMap():
|
|
|
604 |
global weekCutOff
|
|
|
605 |
while(True):
|
|
|
606 |
quantity, amount = 0 , 0
|
|
|
607 |
for i in xrange(weekCutOff , (weekCutOff * 7)+1):
|
|
|
608 |
orderObj = dateWiseOrderMap.get(i*1000)
|
|
|
609 |
quantity += orderObj.count
|
|
|
610 |
amount += orderObj.value
|
|
|
611 |
i = i + oneDay
|
|
|
612 |
weekWiseOrderMap[weekCutOff * 1000] = __Order(quantity, amount)
|
|
|
613 |
weekCutOff = weekCutOff * 1000 * 7 + oneDay
|
|
|
614 |
if weekCutOff >= to_py_date(datetime.now()):
|
|
|
615 |
break
|
|
|
616 |
|
|
|
617 |
def populateWeekWiseMap1():
|
|
|
618 |
global weekCutOff
|
|
|
619 |
while(True):
|
|
|
620 |
#print weekCutOff *1000
|
|
|
621 |
#print to_java_date(datetime.now())
|
|
|
622 |
#print "**********************"
|
|
|
623 |
if weekCutOff *1000 >= to_java_date(datetime.now()):
|
|
|
624 |
#print "Breaking outer while"
|
|
|
625 |
break
|
|
|
626 |
init = weekCutOff
|
|
|
627 |
breakPoint = weekCutOff + (6 * oneDay)
|
|
|
628 |
quantity, amount = 0 , 0
|
|
|
629 |
while(True):
|
|
|
630 |
#print to_py_date(weekCutOff*1000)
|
|
|
631 |
#print "weekCutOff ",weekCutOff
|
|
|
632 |
#print "breakPoint ",breakPoint
|
|
|
633 |
orderObj = dateWiseOrderMap.get(weekCutOff * 1000)
|
|
|
634 |
if orderObj is not None:
|
|
|
635 |
quantity += orderObj.count
|
|
|
636 |
amount += orderObj.value
|
|
|
637 |
weekCutOff = weekCutOff + oneDay
|
|
|
638 |
if weekCutOff > breakPoint:
|
|
|
639 |
weekWiseOrderMap[init * 1000] = __Order(quantity, amount)
|
|
|
640 |
#print "Breaking inner while"
|
|
|
641 |
break
|
|
|
642 |
|
|
|
643 |
def populateMonthWiseMap():
|
|
|
644 |
global monthCutOff
|
|
|
645 |
while(True):
|
|
|
646 |
quantity, amount = 0 , 0
|
|
|
647 |
for i in xrange(monthCutOff , (monthCutOff * 30)+1):
|
|
|
648 |
orderObj = dateWiseOrderMap.get(i*1000)
|
|
|
649 |
quantity += orderObj.count
|
|
|
650 |
amount += orderObj.value
|
|
|
651 |
i = i + oneDay
|
|
|
652 |
weekWiseOrderMap[monthCutOff * 1000] = __Order(quantity, amount)
|
|
|
653 |
monthCutOff = monthCutOff * 1000 * 30 + oneDay
|
|
|
654 |
if monthCutOff >= to_py_date(datetime.now()):
|
|
|
655 |
break
|
|
|
656 |
|
|
|
657 |
def populateMonthWiseMap1():
|
|
|
658 |
global weekCutOff
|
|
|
659 |
while(True):
|
|
|
660 |
print weekCutOff *1000
|
|
|
661 |
print to_java_date(datetime.now())
|
|
|
662 |
print "**********************"
|
|
|
663 |
if weekCutOff *1000 >= to_java_date(datetime.now()):
|
|
|
664 |
print "Breaking outer while"
|
|
|
665 |
break
|
|
|
666 |
init = weekCutOff
|
|
|
667 |
breakPoint = weekCutOff + (6 * oneDay)
|
|
|
668 |
quantity, amount = 0 , 0
|
|
|
669 |
while(True):
|
|
|
670 |
print to_py_date(weekCutOff*1000)
|
|
|
671 |
print "weekCutOff ",weekCutOff
|
|
|
672 |
print "breakPoint ",breakPoint
|
|
|
673 |
orderObj = dateWiseOrderMap.get(weekCutOff * 1000)
|
|
|
674 |
if orderObj is None:
|
|
|
675 |
print "None for ", to_py_date(weekCutOff * 1000)
|
|
|
676 |
if orderObj is not None:
|
|
|
677 |
quantity += orderObj.count
|
|
|
678 |
amount += orderObj.value
|
|
|
679 |
weekCutOff = weekCutOff + oneDay
|
|
|
680 |
if weekCutOff > breakPoint:
|
|
|
681 |
weekWiseOrderMap[init * 1000] = __Order(quantity, amount)
|
|
|
682 |
print "Breaking inner while"
|
|
|
683 |
break
|
|
|
684 |
|
|
|
685 |
def populateValidOrders():
|
|
|
686 |
global order_ids
|
|
|
687 |
allOrders = session.query(Orders.id).join((Users,Orders.user_id==Users.id)).filter(~(func.lower(Users.referrer)).like('emp%')).filter(Orders.status=='ORDER_CREATED').all()
|
|
|
688 |
order_ids = list(zip(*allOrders)[0])
|
|
|
689 |
|
| 14772 |
kshitij.so |
690 |
def main():
|
|
|
691 |
#date = raw_input('Enter a date name: ')
|
| 15228 |
manas |
692 |
populateValidOrders()
|
|
|
693 |
populateOrderMap()
|
|
|
694 |
populateWeekWiseMap1()
|
| 14772 |
kshitij.so |
695 |
generateDailyReport()
|
|
|
696 |
generateWeeklyReport()
|
|
|
697 |
generateMonthlyReport()
|
| 15228 |
manas |
698 |
|
| 14772 |
kshitij.so |
699 |
if __name__ == '__main__':
|
|
|
700 |
main()
|
|
|
701 |
|
|
|
702 |
|