1 |
import argparse |
2 |
import datetime |
3 |
import query |
4 |
import gmail |
5 |
import mailConfig |
6 |
from string import Template |
7 |
parser = argparse.ArgumentParser(description="""Generate report for the date specified. |
8 |
Start and end date default to present day. |
9 |
Start time defaults to 00:00:00. |
10 |
End time defaults to 23:59:59.""") |
11 |
parser.add_argument('-s','--start-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="Start date (YYYY-MM-DD)") |
12 |
parser.add_argument('-e','--end-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="End date (YYYY-MM-DD)") |
13 |
parser.add_argument('-S','--start-time', type=str, default = '00:00:00', help="Start time (HH:MM:SS)") |
14 |
parser.add_argument('-E','--end-time', type=str, default='23:59:59', help="End time (HH:MM:SS)") |
15 |
parser.add_argument('-p','--print', type=bool, default=True, help="Print report on command line") |
16 |
parser.add_argument('-m','--mail', type=bool, default=False, help="Mail report to addressess in mailConfig.py") |
17 |
|
18 |
|
19 |
args = parser.parse_args() |
20 |
startDate = args.start_date + " " + args.start_time |
21 |
endDate = args.end_date + " " + args.end_time |
22 |
|
23 |
postings = query.Query('lb_postings') |
24 |
callDetails = query.Query('cdr') |
25 |
average_call_length = callDetails.average(startDate, endDate) |
26 |
audio_minutes = callDetails.sum(startDate, endDate) |
27 |
channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate) |
28 |
channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate) |
29 |
channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate) |
30 |
mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink') |
31 |
mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata') |
32 |
|
33 |
|
34 |
def genReport(): |
35 |
|
36 |
posts = '{0}: {1}'.format("Number of postings published", postings.posts(startDate, endDate)) |
37 |
|
38 |
recordings = '{0}: {1}'.format("Number of recordings made", postings.recordings(startDate, endDate)) |
39 |
|
40 |
impact_stories = '{0}: {1}'.format("Number of impact stories", postings.filter_by_title('impact', startDate, endDate)) |
41 |
|
42 |
missed_calls = '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) |
43 |
|
44 |
missed_calls_mobilink = '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink')) |
45 |
|
46 |
missed_calls_mobilink_tata = '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate,'mobilinktata')) |
47 |
|
48 |
answered_calls = '{0}: {1}'.format("Number of calls answered", callDetails.answeredCalls(startDate, endDate)) |
49 |
|
50 |
filtered_calls = '{0}: {1}'.format("Number of calls lasting less than 30 seconds", callDetails.filter_calls_by_duration(startDate, endDate, 30)) |
51 |
|
52 |
average_Call_Length = '{0}: {1:.4f}'.format("Average length of calls", average_call_length) |
53 |
|
54 |
audio_Minutes = '{0}: {1:.4f}'.format("Total number of audio minutes played", audio_minutes) |
55 |
|
56 |
ch1_minutes = '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.20", channel1_minutes) |
57 |
|
58 |
ch2_minutes = '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.21", channel2_minutes) |
59 |
|
60 |
ch3_minutes = '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.22", channel3_minutes) |
61 |
|
62 |
ch1_load = '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) |
63 |
|
64 |
ch2_load = '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) |
65 |
|
66 |
ch3_load = '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) |
67 |
|
68 |
mobilink_max_load = '{0}: {1}'.format("Busiest hour for mobilink",mobilink_load["maxLoad"]) |
69 |
|
70 |
mobilink_max_calls = '{0}: {1}'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"]) |
71 |
|
72 |
mobilink_min_load = '{0}: {1}'.format("Least active hour for mobilink",mobilink_load["minLoad"]) |
73 |
|
74 |
mobilink_tata_max_load = '{0}: {1}'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"]) |
75 |
|
76 |
mobilink_tata_max_calls = '{0}: {1}'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"]) |
77 |
|
78 |
mobilink_tata_min_load = '{0}: {1}'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"]) |
79 |
|
80 |
unanswered_calls = '{0}: {1}'.format("Number of calls unanswered", callDetails.calls_unanswered(startDate, endDate)) |
81 |
|
82 |
t =Template('$posts\n$recordings\n$impact_stories\n$missed_calls\n$missed_calls_mobilink\n$missed_calls_mobilink_tata\n$answered_calls\n$unanswered_calls\n$filtered_calls\n$average_call_length\n$audio_minutes\n$ch1_minutes\n$ch2_minutes\n$ch3_minutes\n$ch1_load\n$ch2_load\n$ch3_load\n$mobilink_max_load\n$mobilink_max_calls\n$mobilink_min_load\n$mobilink_tata_max_load\n$mobilink_tata_max_calls\n$mobilink_tata_min_load') |
83 |
report = t.substitute(posts=posts,recordings=recordings,impact_stories=impact_stories,missed_calls=missed_calls,missed_calls_mobilink=missed_calls_mobilink,missed_calls_mobilink_tata=missed_calls_mobilink_tata,answered_calls=answered_calls,unanswered_calls=unanswered_calls,filtered_calls=filtered_calls,average_call_length=average_Call_Length,audio_minutes=audio_Minutes,ch1_minutes=ch1_minutes,ch2_minutes=ch2_minutes,ch3_minutes=ch3_minutes,ch1_load=ch1_load,ch2_load=ch2_load,ch3_load=ch3_load,mobilink_max_load=mobilink_max_load,mobilink_max_calls=mobilink_max_calls,mobilink_min_load=mobilink_min_load,mobilink_tata_max_load=mobilink_tata_max_load,mobilink_tata_max_calls=mobilink_tata_max_calls,mobilink_tata_min_load=mobilink_tata_min_load) |
84 |
return report |
85 |
|
86 |
if args.mail is False: |
87 |
report = genReport() |
88 |
print report |
89 |
else: |
90 |
report = genReport() |
91 |
subject = mailConfig.subject.format(startDate, endDate) |
92 |
report = mailConfig.text.format(startDate, endDate) + '\n' + report |
93 |
mail = gmail.GMail(mailConfig.username, mailConfig.password) |
94 |
message = gmail.Message(subject, mailConfig.to, mailConfig.cc, text=report) |
95 |
mail.send(message) |