Commit 2e37597c84bcbd394a1c2fc8aa6fd783f12ea702

  • avatar
  • arvind
  • Tue Jan 14 14:32:47 IST 2014
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
(19 / 1)
  
11import logger
22from sqlalchemy.sql import func
3import datetime
34
4
55class Query:
66 """Objects of query class can be used to run specific queries."""
77 def __init__(self, table):
5454
5555 def filter_calls_by_duration(self, date, dateRange, duration):
5656 return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', self.t.lt.duration < duration, self.t.lt.calldate.between(date, dateRange)).count()
57
58 def call_distribution(self, date, dateRange, dcontext):
59 startTimeStamp = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
60 endTimeStamp = datetime.datetime.strptime(dateRange, '%Y-%m-%d %H:%M:%S')
61 result = self.t.lt.query.filter(self.t.lt.dcontext == dcontext, self.t.lt.calldate.between(date, dateRange))
62 slots = {}
63 while(startTimeStamp - endTimeStamp <= datetime.timedelta(0)):
64 date1 = startTimeStamp
65 date2 = startTimeStamp + datetime.timedelta(seconds=3600)
66 startTimeStamp = date2
67 if slots.has_key('{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))):
68 slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] += result.filter(self.t.lt.calldate.between(date1, date2)).count()
69 else:
70 slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] = result.filter(self.t.lt.calldate.between(date1, date2)).count()
71
72 maxLoad = max(slots, key = lambda x: slots.get(x) )
73 minLoad = min(slots, key = lambda x: slots.get(x) )
74 return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": minLoad}
report.py
(8 / 1)
  
2828channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate)
2929channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate)
3030channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate)
31mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink')
32mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata')
3133
3234
33
3435print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate))
3536print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink'))
3637print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata'))
4545print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes)
4646print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes)
4747print '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes)
48print '{0}: {1}'.format("Busiest hour for mobilink",mobilink_load["maxLoad"])
49print '{0}: {1}'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"])
50print '{0}: {1}'.format("Least active hour for mobilink",mobilink_load["minLoad"])
51print '{0}: {1}'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"])
52print '{0}: {1}'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"])
53print '{0}: {1}'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"])