300e5f4 by Arvind at 2014-01-09 1
import argparse
2
import datetime
3
import query
cad2a99 by Arvind at 2014-01-14 4
import gmail
41df78b by Arvind at 2014-03-10 5
import config as conf
cad2a99 by Arvind at 2014-01-14 6
import mailConfig
41df78b by Arvind at 2014-03-10 7
import decimal
8
200085d by Arvind at 2014-01-09 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.""")
300e5f4 by Arvind at 2014-01-09 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)")
200085d by Arvind at 2014-01-09 16
parser.add_argument('-E','--end-time', type=str, default='23:59:59', help="End time (HH:MM:SS)")
cad2a99 by Arvind at 2014-01-14 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")
9d54d1e by Arvind at 2014-01-09 19
20
300e5f4 by Arvind at 2014-01-09 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)
2e37597 by Arvind at 2014-01-14 29
mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink')
30
mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata')
300e5f4 by Arvind at 2014-01-09 31
cad2a99 by Arvind at 2014-01-14 32
41df78b by Arvind at 2014-03-10 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
d48d677 by Arvind at 2014-01-15 36
41df78b by Arvind at 2014-03-10 37
    """
38
    minutes = {}
39
    for ip in conf.ip_addr:
40
        minutes[ip] = callDetails.totalMinutes(ip, startDate, endDate)
41
    return minutes
d48d677 by Arvind at 2014-01-15 42
41df78b by Arvind at 2014-03-10 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
d48d677 by Arvind at 2014-01-15 49
41df78b by Arvind at 2014-03-10 50
def genJSON():
51
    return ({"Number_of_postings_published": postings.posts(startDate,
52
                                                               endDate),
d48d677 by Arvind at 2014-01-15 53
41df78b by Arvind at 2014-03-10 54
                "Number_of_recordings_made": postings.recordings(startDate,
55
                endDate),
d48d677 by Arvind at 2014-01-15 56
41df78b by Arvind at 2014-03-10 57
                "Number_of_impact_stories": postings.filter_by_title('impact',
58
                startDate, endDate),
d48d677 by Arvind at 2014-01-15 59
41df78b by Arvind at 2014-03-10 60
                "Number_of_missed_calls": callDetails.missedCalls(startDate,
61
                                                                  endDate),
d48d677 by Arvind at 2014-01-15 62
41df78b by Arvind at 2014-03-10 63
                "Number_of_missed_calls_on_mobilink":
64
                callDetails.missedCalls(startDate, endDate, 'mobilink'),
d48d677 by Arvind at 2014-01-15 65
41df78b by Arvind at 2014-03-10 66
                "Number_of_missed_calls_on_mobilinktata":
67
                callDetails.missedCalls(startDate, endDate,'mobilinktata'),
d48d677 by Arvind at 2014-01-15 68
41df78b by Arvind at 2014-03-10 69
                "Number_of_calls_answered": callDetails.answeredCalls(startDate,
70
                endDate),
d48d677 by Arvind at 2014-01-15 71
41df78b by Arvind at 2014-03-10 72
                "Number_of_calls_lasting_less_than_30_seconds": callDetails.filter_calls_by_duration(startDate, endDate, 30),
d48d677 by Arvind at 2014-01-15 73
41df78b by Arvind at 2014-03-10 74
                "Average_length_of_calls": average_call_length,
cad2a99 by Arvind at 2014-01-14 75
41df78b by Arvind at 2014-03-10 76
                "Total_number_of_audio_minutes_played": str(audio_minutes),
d48d677 by Arvind at 2014-01-15 77
41df78b by Arvind at 2014-03-10 78
                "Audio_minutes_on_channel": individual_audio_minutes(),
d48d677 by Arvind at 2014-01-15 79
41df78b by Arvind at 2014-03-10 80
                "Load_on_channel": individual_load(),
d48d677 by Arvind at 2014-01-15 81
41df78b by Arvind at 2014-03-10 82
                "Busiest_hour_for_mobilink": mobilink_load["maxLoad"],
d48d677 by Arvind at 2014-01-15 83
41df78b by Arvind at 2014-03-10 84
                "Number_of_calls_in_busiest_hour_for_mobilink":
85
                mobilink_load["maxCalls"],
d48d677 by Arvind at 2014-01-15 86
41df78b by Arvind at 2014-03-10 87
                "Least_active_hour_for_mobilink": mobilink_load["minLoad"],
d48d677 by Arvind at 2014-01-15 88
41df78b by Arvind at 2014-03-10 89
                "Busiest_hour_for_mobilinktata": mobilink_tata_load["maxLoad"],
d48d677 by Arvind at 2014-01-15 90
41df78b by Arvind at 2014-03-10 91
                "Number_of_calls_in_busiest_hour_for_mobilinktata":
92
                mobilink_tata_load["maxCalls"],
d48d677 by Arvind at 2014-01-15 93
41df78b by Arvind at 2014-03-10 94
                "Least_active_hour_for_mobilinktata":
95
                mobilink_tata_load["minLoad"],
d48d677 by Arvind at 2014-01-15 96
41df78b by Arvind at 2014-03-10 97
                "Number_of_calls_unanswered":
98
                callDetails.calls_unanswered(startDate, endDate),
d48d677 by Arvind at 2014-01-15 99
41df78b by Arvind at 2014-03-10 100
                "Maximum_duration_of_unanswered_call":
101
                callDetails.max_duration_UC(startDate, endDate)
102
            })
103
104
def genReport():
d48d677 by Arvind at 2014-01-15 105
41df78b by Arvind at 2014-03-10 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'
efa17c7 by Arvind at 2014-01-25 114
41df78b by Arvind at 2014-03-10 115
    return template
54dd346 by Arvind at 2014-01-25 116
d48d677 by Arvind at 2014-01-15 117
118
if args.mail is False:
119
    report = genReport()
120
    print report
121
else:
122
    report = genReport()
3410657 by Arvind at 2014-01-15 123
    subject = mailConfig.subject.format(startDate, endDate)
1e18075 by Arvind at 2014-01-14 124
    report = mailConfig.text.format(startDate, endDate) + '\n' + report
cad2a99 by Arvind at 2014-01-14 125
    mail = gmail.GMail(mailConfig.username, mailConfig.password)
4c66686 by Arvind at 2014-01-20 126
    message = gmail.Message(subject, mailConfig.to, mailConfig.cc, text=report)
d48d677 by Arvind at 2014-01-15 127
    mail.send(message)