1
import argparse
2
import datetime
3
import query
4
import gmail
5
import config as conf
6
import mailConfig
7
import decimal
8
9
parser = argparse.ArgumentParser(description="""Generate report for the date specified.
10
                                 Start and end date default to present day.
11
                                 Start time defaults to 00:00:00.
12
                                 End time defaults to 23:59:59.""")
13
parser.add_argument('-s','--start-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="Start date (YYYY-MM-DD)")
14
parser.add_argument('-e','--end-date', type=str, default=datetime.datetime.strftime(datetime.date.today(), '%Y-%m-%d'), help="End date (YYYY-MM-DD)")
15
parser.add_argument('-S','--start-time', type=str, default = '00:00:00', help="Start time (HH:MM:SS)")
16
parser.add_argument('-E','--end-time', type=str, default='23:59:59', help="End time (HH:MM:SS)")
17
parser.add_argument('-p','--print', type=bool, default=True, help="Print report on command line")
18
parser.add_argument('-m','--mail', type=bool, default=False, help="Mail report to addressess in mailConfig.py")
19
20
21
args = parser.parse_args()
22
startDate = args.start_date + " " + args.start_time
23
endDate = args.end_date + " " + args.end_time
24
25
postings = query.Query('lb_postings')
26
callDetails = query.Query('cdr')
27
average_call_length = callDetails.average(startDate, endDate)
28
audio_minutes = callDetails.sum(startDate, endDate)
29
mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink')
30
mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata')
31
32
33
def individual_audio_minutes():
34
    """Return the total number of minutes on outgoing calls for every IP
35
    address of modems specified in conf.py
36
37
    """
38
    minutes = {}
39
    for ip in conf.ip_addr:
40
        minutes[ip] = callDetails.totalMinutes(ip, startDate, endDate)
41
    return minutes
42
43
def individual_load():
44
    """ Return the fraction of load on each modem."""
45
    minutes = individual_audio_minutes()
46
    for ip in minutes:
47
        minutes[ip] = round(minutes[ip]/audio_minutes, 4)
48
    return minutes
49
50
def genJSON():
51
    return ({"Number_of_postings_published": postings.posts(startDate,
52
                                                               endDate),
53
54
                "Number_of_recordings_made": postings.recordings(startDate,
55
                endDate),
56
57
                "Number_of_impact_stories": postings.filter_by_title('impact',
58
                startDate, endDate),
59
60
                "Number_of_missed_calls": callDetails.missedCalls(startDate,
61
                                                                  endDate),
62
63
                "Number_of_missed_calls_on_mobilink":
64
                callDetails.missedCalls(startDate, endDate, 'mobilink'),
65
66
                "Number_of_missed_calls_on_mobilinktata":
67
                callDetails.missedCalls(startDate, endDate,'mobilinktata'),
68
69
                "Number_of_calls_answered": callDetails.answeredCalls(startDate,
70
                endDate),
71
72
                "Number_of_calls_lasting_less_than_30_seconds": callDetails.filter_calls_by_duration(startDate, endDate, 30),
73
74
                "Average_length_of_calls": average_call_length,
75
76
                "Total_number_of_audio_minutes_played": str(audio_minutes),
77
78
                "Audio_minutes_on_channel": individual_audio_minutes(),
79
80
                "Load_on_channel": individual_load(),
81
82
                "Busiest_hour_for_mobilink": mobilink_load["maxLoad"],
83
84
                "Number_of_calls_in_busiest_hour_for_mobilink":
85
                mobilink_load["maxCalls"],
86
87
                "Least_active_hour_for_mobilink": mobilink_load["minLoad"],
88
89
                "Busiest_hour_for_mobilinktata": mobilink_tata_load["maxLoad"],
90
91
                "Number_of_calls_in_busiest_hour_for_mobilinktata":
92
                mobilink_tata_load["maxCalls"],
93
94
                "Least_active_hour_for_mobilinktata":
95
                mobilink_tata_load["minLoad"],
96
97
                "Number_of_calls_unanswered":
98
                callDetails.calls_unanswered(startDate, endDate),
99
100
                "Maximum_duration_of_unanswered_call":
101
                callDetails.max_duration_UC(startDate, endDate)
102
            })
103
104
def genReport():
105
106
    rep = genJSON()
107
    keys = rep.keys()
108
    template = ''
109
    for key in keys:
110
        if type(rep[key]) is dict:
111
            for item in rep[key]:
112
                rep[key][item] = str(rep[key][item])
113
        template+= key.replace('_', ' ') + ': ' + str(rep[key])  + '\n'
114
115
    return template
116
117
118
if args.mail is False:
119
    report = genReport()
120
    print report
121
else:
122
    report = genReport()
123
    subject = mailConfig.subject.format(startDate, endDate)
124
    report = mailConfig.text.format(startDate, endDate) + '\n' + report
125
    mail = gmail.GMail(mailConfig.username, mailConfig.password)
126
    message = gmail.Message(subject, mailConfig.to, mailConfig.cc, text=report)
127
    mail.send(message)