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.totalMinutes(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(startDate, endDate, ip)
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_in_minutes": 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
             "Number of calls failed permanently": callDetails.unAccountedCalls(startDate, endDate)
104
            })
105
106
def genReport():
107
108
    rep = genJSON()
109
    keys = rep.keys()
110
    template = ''
111
    for key in keys:
112
        if type(rep[key]) is dict:
113
            for item in rep[key]:
114
                rep[key][item] = rep[key][item]
115
        template+= key.replace('_', ' ') + ': ' + str(rep[key])  + '\n'
116
117
    return template
118
119
120
if args.mail is False:
121
    report = genReport()
122
    print report
123
else:
124
    report = genReport()
125
    subject = mailConfig.subject.format(startDate, endDate)
126
    report = mailConfig.text.format(startDate, endDate) + '\n' + report
127
    mail = gmail.GMail(mailConfig.username, mailConfig.password)
128
    message = gmail.Message(subject, mailConfig.to, mailConfig.cc, text=report)
129
    mail.send(message)