1
import argparse
2
import datetime
3
import query
4
import gmail
5
import mailConfig
6
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
if args.mail is False:
34
    print '{0}: {1}'.format("Number of postings published", postings.posts(startDate, endDate))
35
    print '{0}: {1}'.format("Number of recordings made", postings.recordings(startDate, endDate))
36
    print '{0}: {1}'.format("Number of impact stories", postings.filter_by_title('impact', startDate, endDate))
37
    print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate))
38
    print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink'))
39
    print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata'))
40
    print '{0}: {1}'.format("Number of calls answered", callDetails.answeredCalls(startDate, endDate))
41
    print '{0}: {1}'.format("Number of calls lasting less than 30 seconds", callDetails.filter_calls_by_duration(startDate, endDate, 30))
42
    print '{0}: {1:.4f}'.format("Average length of calls", average_call_length)
43
    print '{0}: {1:.4f}'.format("Total number of audio minutes played", audio_minutes)
44
    print '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.20", channel1_minutes)
45
    print '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.21", channel2_minutes)
46
    print '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.22", channel3_minutes)
47
    print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes)
48
    print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes)
49
    print '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes)
50
    print '{0}: {1}'.format("Busiest hour for mobilink",mobilink_load["maxLoad"])
51
    print '{0}: {1}'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"])
52
    print '{0}: {1}'.format("Least active hour for mobilink",mobilink_load["minLoad"])
53
    print '{0}: {1}'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"])
54
    print '{0}: {1}'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"])
55
    print '{0}: {1}'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"])
56
57
else:
58
    report = '{0}: {1}\n'.format("Number of postings published", postings.posts(startDate, endDate)) + '{0}: {1}\n'.format("Number of recordings made", postings.recordings(startDate, endDate)) + '{0}: {1}\n'.format("Number of impact stories", postings.filter_by_title('impact', startDate, endDate)) + '{0}: {1}\n'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) + '{0}: {1}\n'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink'))  + '{0}: {1}\n'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata')) + '{0}: {1}\n'.format("Number of calls answered", callDetails.answeredCalls(startDate, endDate)) + '{0}: {1}\n'.format("Number of calls lasting less than 30 seconds", callDetails.filter_calls_by_duration(startDate, endDate, 30)) + '{0}: {1:.4f}\n'.format("Average length of calls", average_call_length) + '{0}: {1:.4f}\n'.format("Total number of audio minutes played", audio_minutes) + '{0}: {1:.4f}\n'.format("Audio minutes on channel 10.0.0.20", channel1_minutes) + '{0}: {1:.4f}\n'.format("Audio minutes on channel 10.0.0.21", channel2_minutes) + '{0}: {1:.4f}\n'.format("Audio minutes on channel 10.0.0.22", channel3_minutes) + '{0}: {1:.4f}\n'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) + '{0}: {1:.4f}\n'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) + '{0}: {1:.4f}\n'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) + '{0}: {1}\n'.format("Busiest hour for mobilink",mobilink_load["maxLoad"]) + '{0}: {1}\n'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"]) + '{0}: {1}\n'.format("Least active hour for mobilink",mobilink_load["minLoad"]) + '{0}: {1}\n'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"]) + '{0}: {1}\n'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"]) + '{0}: {1}\n'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"])
59
60
61
    mail = gmail.GMail(mailConfig.username, mailConfig.password)
62
    message = gmail.Message(mailConfig.subject, mailConfig.to, text=report)
63
    mail.send(message)