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.
  • Diff rendering mode:
  • inline
  • side by side

query.py

1import logger1import logger
2from sqlalchemy.sql import func2from sqlalchemy.sql import func
3import datetime
34
4
5class Query:5class Query:
6 """Objects of query class can be used to run specific queries."""6 """Objects of query class can be used to run specific queries."""
7 def __init__(self, table):7 def __init__(self, table):
5454
55 def filter_calls_by_duration(self, date, dateRange, duration):55 def filter_calls_by_duration(self, date, dateRange, duration):
56 return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', self.t.lt.duration < duration, self.t.lt.calldate.between(date, dateRange)).count()56 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

28channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate)28channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate)
29channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate)29channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate)
30channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate)30channel3_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
34print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate))35print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate))
35print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink'))36print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink'))
36print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata'))37print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata'))
45print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes)45print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes)
46print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes)46print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes)
47print '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes)47print '{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"])