--- a/query.py +++ b/query.py @@ -1,7 +1,19 @@ import logger from sqlalchemy.sql import func import datetime +import decimal +def toStr(fn): + def wrapped(*args, **kwargs): + response = fn(*args, **kwargs) + if type(response) is decimal.Decimal: + return str(response) + if type(response) is dict: + return repr(response) + else: + return str(response) + return wrapped + class Query: """Objects of query class can be used to run specific queries.""" def __init__(self, table): @@ -15,31 +27,38 @@ def recordings(self, date, dateRange): return self.t.lt.query.filter(self.t.lt.posted.between(date, dateRange)).count() + def filter_by_title(self, title, date, dateRange): return self.t.lt.query.filter(self.t.lt.title.like(title+'%'), self.t.lt.posted.between(date, dateRange)).count() - def load(self, channel, date, dateRange): + + def totalMinutes(self, channel, date, dateRange): query = self.t.lt.query.with_entities(func.sum(self.t.lt.duration).label('sum')).filter(self.t.lt.dcontext == "callback", self.t.lt.channel.like(channel+'%'), self.t.lt.calldate.between(date, dateRange)) sum = 0 for res in query: - sum = res.sum/60 - return sum + if res.sum is not None: + sum = res.sum/60 + return round(sum, 4) + def average(self, date, dateRange): query = self.t.lt.query.with_entities(func.avg(self.t.lt.duration).label('average')).filter(self.t.lt.dcontext == "callback", self.t.lt.calldate.between(date, dateRange)) average = 0 for res in query: - average = res.average/60 - return average + if res.average is not None: + average = res.average/60 + return round(average, 4) def sum(self, date, dateRange): query = self.t.lt.query.with_entities(func.sum(self.t.lt.duration).label('sum')).filter(self.t.lt.dcontext == "callback", self.t.lt.calldate.between(date, dateRange)) sum = 0 for res in query: - sum = res.sum/60 - return sum + if res.sum is not None: + sum = res.sum/60 + return round(sum, 4) + def missedCalls(self, date, dateRange, modem=None): if modem is None: return self.t.lt.query.filter(((self.t.lt.dcontext == 'mobilink') | @@ -48,13 +67,16 @@ else: return self.t.lt.query.filter(self.t.lt.dcontext == modem, self.t.lt.calldate.between(date, dateRange)).count() + def answeredCalls(self, date, dateRange): return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', self.t.lt.calldate.between(date, dateRange)).count() + def filter_calls_by_duration(self, date, dateRange, duration): return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', self.t.lt.duration < duration, self.t.lt.calldate.between(date, dateRange)).count() + def call_distribution(self, date, dateRange, dcontext): startTimeStamp = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') endTimeStamp = datetime.datetime.strptime(dateRange, '%Y-%m-%d %H:%M:%S') @@ -76,13 +98,16 @@ minLoad.append(slot) return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": repr(minLoad)} + def calls_unanswered(self, date, dateRange): return self.t.lt.query.filter(self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)).count() + def max_duration_UC(self, date, dateRange): query = self.t.lt.query.with_entities(func.max(self.t.lt.duration).label('duration')).filter(self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)) duration = 0 for result in query: - duration = result.duration + if result.duration is not None: + duration = result.duration return duration --- a/report.py +++ b/report.py @@ -2,8 +2,10 @@ import datetime import query import gmail +import config as conf import mailConfig -from string import Template +import decimal + parser = argparse.ArgumentParser(description="""Generate report for the date specified. Start and end date default to present day. Start time defaults to 00:00:00. @@ -24,66 +26,94 @@ callDetails = query.Query('cdr') average_call_length = callDetails.average(startDate, endDate) audio_minutes = callDetails.sum(startDate, endDate) -channel1_minutes = callDetails.load('SIP/10.0.0.30', startDate, endDate) -channel2_minutes = callDetails.load('SIP/10.0.0.31', startDate, endDate) -channel3_minutes = callDetails.load('SIP/10.0.0.33', startDate, endDate) mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink') mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata') -def genReport(): +def individual_audio_minutes(): + """Return the total number of minutes on outgoing calls for every IP + address of modems specified in conf.py - posts = '{0}: {1}'.format("Number of postings published", postings.posts(startDate, endDate)) + """ + minutes = {} + for ip in conf.ip_addr: + minutes[ip] = callDetails.totalMinutes(ip, startDate, endDate) + return minutes - recordings = '{0}: {1}'.format("Number of recordings made", postings.recordings(startDate, endDate)) +def individual_load(): + """ Return the fraction of load on each modem.""" + minutes = individual_audio_minutes() + for ip in minutes: + minutes[ip] = round(minutes[ip]/audio_minutes, 4) + return minutes - impact_stories = '{0}: {1}'.format("Number of impact stories", postings.filter_by_title('impact', startDate, endDate)) +def genJSON(): + return ({"Number_of_postings_published": postings.posts(startDate, + endDate), - missed_calls = '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) + "Number_of_recordings_made": postings.recordings(startDate, + endDate), - missed_calls_mobilink = '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink')) + "Number_of_impact_stories": postings.filter_by_title('impact', + startDate, endDate), - missed_calls_mobilink_tata = '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate,'mobilinktata')) + "Number_of_missed_calls": callDetails.missedCalls(startDate, + endDate), - answered_calls = '{0}: {1}'.format("Number of calls answered", callDetails.answeredCalls(startDate, endDate)) + "Number_of_missed_calls_on_mobilink": + callDetails.missedCalls(startDate, endDate, 'mobilink'), - filtered_calls = '{0}: {1}'.format("Number of calls lasting less than 30 seconds", callDetails.filter_calls_by_duration(startDate, endDate, 30)) + "Number_of_missed_calls_on_mobilinktata": + callDetails.missedCalls(startDate, endDate,'mobilinktata'), - average_Call_Length = '{0}: {1:.4f}'.format("Average length of calls", average_call_length) + "Number_of_calls_answered": callDetails.answeredCalls(startDate, + endDate), - audio_Minutes = '{0}: {1:.4f}'.format("Total number of audio minutes played", audio_minutes) + "Number_of_calls_lasting_less_than_30_seconds": callDetails.filter_calls_by_duration(startDate, endDate, 30), - ch1_minutes = '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.20", channel1_minutes) + "Average_length_of_calls": average_call_length, - ch2_minutes = '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.21", channel2_minutes) + "Total_number_of_audio_minutes_played": str(audio_minutes), - ch3_minutes = '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.22", channel3_minutes) + "Audio_minutes_on_channel": individual_audio_minutes(), - ch1_load = '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) + "Load_on_channel": individual_load(), - ch2_load = '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) + "Busiest_hour_for_mobilink": mobilink_load["maxLoad"], - ch3_load = '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) + "Number_of_calls_in_busiest_hour_for_mobilink": + mobilink_load["maxCalls"], - mobilink_max_load = '{0}: {1}'.format("Busiest hour for mobilink",mobilink_load["maxLoad"]) + "Least_active_hour_for_mobilink": mobilink_load["minLoad"], - mobilink_max_calls = '{0}: {1}'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"]) + "Busiest_hour_for_mobilinktata": mobilink_tata_load["maxLoad"], - mobilink_min_load = '{0}: {1}'.format("Least active hour for mobilink",mobilink_load["minLoad"]) + "Number_of_calls_in_busiest_hour_for_mobilinktata": + mobilink_tata_load["maxCalls"], - mobilink_tata_max_load = '{0}: {1}'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"]) + "Least_active_hour_for_mobilinktata": + mobilink_tata_load["minLoad"], - mobilink_tata_max_calls = '{0}: {1}'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"]) + "Number_of_calls_unanswered": + callDetails.calls_unanswered(startDate, endDate), - mobilink_tata_min_load = '{0}: {1}'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"]) + "Maximum_duration_of_unanswered_call": + callDetails.max_duration_UC(startDate, endDate) + }) - unanswered_calls = '{0}: {1}'.format("Number of calls unanswered", callDetails.calls_unanswered(startDate, endDate)) +def genReport(): - max_duration_UC = '{0}: {1}'.format("Maximum duration of unanswered call(in seconds)", callDetails.max_duration_UC(startDate, endDate)) + rep = genJSON() + keys = rep.keys() + template = '' + for key in keys: + if type(rep[key]) is dict: + for item in rep[key]: + rep[key][item] = str(rep[key][item]) + template+= key.replace('_', ' ') + ': ' + str(rep[key]) + '\n' - 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$max_duration_uc\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') - 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,max_duration_uc=max_duration_UC,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) - return report + return template + if args.mail is False: report = genReport()