--- a/query.py +++ b/query.py @@ -1,7 +1,7 @@ import logger from sqlalchemy.sql import func +import datetime - class Query: """Objects of query class can be used to run specific queries.""" def __init__(self, table): @@ -54,4 +54,22 @@ 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') + result = self.t.lt.query.filter(self.t.lt.dcontext == dcontext, self.t.lt.calldate.between(date, dateRange)) + slots = {} + while(startTimeStamp - endTimeStamp <= datetime.timedelta(0)): + date1 = startTimeStamp + date2 = startTimeStamp + datetime.timedelta(seconds=3600) + startTimeStamp = date2 + if slots.has_key('{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))): + slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] += result.filter(self.t.lt.calldate.between(date1, date2)).count() + else: + slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] = result.filter(self.t.lt.calldate.between(date1, date2)).count() + + maxLoad = max(slots, key = lambda x: slots.get(x) ) + minLoad = min(slots, key = lambda x: slots.get(x) ) + return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": minLoad} --- a/report.py +++ b/report.py @@ -28,9 +28,10 @@ channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate) channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate) channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate) +mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink') +mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata') - print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink')) print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata')) @@ -44,4 +45,10 @@ print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) print '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) +print '{0}: {1}'.format("Busiest hour for mobilink",mobilink_load["maxLoad"]) +print '{0}: {1}'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"]) +print '{0}: {1}'.format("Least active hour for mobilink",mobilink_load["minLoad"]) +print '{0}: {1}'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"]) +print '{0}: {1}'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"]) +print '{0}: {1}'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"])