Commit 2e37597c84bcbd394a1c2fc8aa6fd783f12ea702
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.
| | | | 1 | import logger | 1 | import logger |
---|
2 | from sqlalchemy.sql import func | 2 | from sqlalchemy.sql import func |
---|
| | 3 | import datetime |
---|
3 | | 4 | |
---|
4 | | | |
---|
5 | class Query: | 5 | class 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): |
---|
… | | … | |
---|
54 | | 54 | |
---|
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} |
---|
| | | | 28 | channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate) | 28 | channel1_minutes = callDetails.load('SIP/10.0.0.20', startDate, endDate) |
---|
29 | channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate) | 29 | channel2_minutes = callDetails.load('SIP/10.0.0.21', startDate, endDate) |
---|
30 | channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate) | 30 | channel3_minutes = callDetails.load('SIP/10.0.0.22', startDate, endDate) |
---|
| | 31 | mobilink_load = callDetails.call_distribution(startDate, endDate, 'mobilink') |
---|
| | 32 | mobilink_tata_load = callDetails.call_distribution(startDate, endDate, 'mobilinktata') |
---|
31 | | 33 | |
---|
32 | | 34 | |
---|
33 | | | |
---|
34 | print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) | 35 | print '{0}: {1}'.format("Number of missed calls", callDetails.missedCalls(startDate, endDate)) |
---|
35 | print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink')) | 36 | print '{0}: {1}'.format("Number of missed calls on 'mobilink'", callDetails.missedCalls(startDate, endDate, 'mobilink')) |
---|
36 | print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata')) | 37 | print '{0}: {1}'.format("Number of missed calls on 'mobilinktata'", callDetails.missedCalls(startDate, endDate, 'mobilinktata')) |
---|
… | | … | |
---|
45 | print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) | 45 | print '{0}: {1:.4f}'.format("Load on channel 10.0.0.20", channel1_minutes/audio_minutes) |
---|
46 | print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) | 46 | print '{0}: {1:.4f}'.format("Load on channel 10.0.0.21", channel2_minutes/audio_minutes) |
---|
47 | print '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) | 47 | print '{0}: {1:.4f}'.format("Load on channel 10.0.0.22", channel3_minutes/audio_minutes) |
---|
| | 48 | print '{0}: {1}'.format("Busiest hour for mobilink",mobilink_load["maxLoad"]) |
---|
| | 49 | print '{0}: {1}'.format("Number of calls in busiest hour for mobilink",mobilink_load["maxCalls"]) |
---|
| | 50 | print '{0}: {1}'.format("Least active hour for mobilink",mobilink_load["minLoad"]) |
---|
| | 51 | print '{0}: {1}'.format("Busiest hour for mobilinktata",mobilink_tata_load["maxLoad"]) |
---|
| | 52 | print '{0}: {1}'.format("Number of calls in busiest hour for mobilinktata",mobilink_tata_load["maxCalls"]) |
---|
| | 53 | print '{0}: {1}'.format("Least active hour for mobilinktata",mobilink_tata_load["minLoad"]) |
---|