From 2e37597c84bcbd394a1c2fc8aa6fd783f12ea702 Mon Sep 17 00:00:00 2001 From: Arvind Date: Tue, 14 Jan 2014 14:32:47 +0530 Subject: [PATCH] Adding query to find call distribution by number. Query.call_distribution() returns a dictionary containing busiest hours, number of calls in those hours, and least active hour. --- query.py | 22 ++++++++++++++++++++-- report.py | 9 ++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/query.py b/query.py index 0d61283..98bc65a 100644 --- a/query.py +++ b/query.py @@ -1,6 +1,6 @@ import logger from sqlalchemy.sql import func - +import datetime class Query: """Objects of query class can be used to run specific queries.""" @@ -53,4 +53,22 @@ class Query: 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() \ No newline at end of file + 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} diff --git a/report.py b/report.py index 73e1055..acbe514 100644 --- a/report.py +++ b/report.py @@ -28,7 +28,8 @@ audio_minutes = callDetails.sum(startDate, endDate) 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)) @@ -44,3 +45,9 @@ print '{0}: {1:.4f}'.format("Audio minutes on channel 10.0.0.22", channel3_minut 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"]) \ No newline at end of file -- 1.7.10.4