1 |
import logger |
2 |
from sqlalchemy.sql import func |
3 |
import datetime |
4 |
import decimal |
5 |
|
6 |
def toStr(fn): |
7 |
def wrapped(*args, **kwargs): |
8 |
response = fn(*args, **kwargs) |
9 |
if type(response) is decimal.Decimal: |
10 |
return str(response) |
11 |
if type(response) is dict: |
12 |
return repr(response) |
13 |
else: |
14 |
return str(response) |
15 |
return wrapped |
16 |
|
17 |
class Query: |
18 |
"""Objects of query class can be used to run specific queries.""" |
19 |
def __init__(self, table): |
20 |
self.t = logger.Logger(table) |
21 |
|
22 |
|
23 |
def posts(self, date, dateRange): |
24 |
return self.t.lt.query.filter(self.t.lt.status == 3, |
25 |
self.t.lt.posted.between(date, dateRange)).count() |
26 |
|
27 |
def recordings(self, date, dateRange): |
28 |
return self.t.lt.query.filter(self.t.lt.posted.between(date, |
29 |
dateRange)).count() |
30 |
|
31 |
def filter_by_title(self, title, date, dateRange): |
32 |
return self.t.lt.query.filter(self.t.lt.title.like(title+'%'), |
33 |
self.t.lt.posted.between(date, dateRange)).count() |
34 |
|
35 |
|
36 |
def totalMinutes(self, channel, date, dateRange): |
37 |
query = self.t.lt.query.with_entities(func.sum(self.t.lt.duration).label('sum')).filter(self.t.lt.dcontext == "callback", self.t.lt.channel.like(channel+'%'), self.t.lt.calldate.between(date, dateRange)) |
38 |
sum = 0 |
39 |
for res in query: |
40 |
if res.sum is not None: |
41 |
sum = res.sum/60 |
42 |
return round(sum, 4) |
43 |
|
44 |
|
45 |
def average(self, date, dateRange): |
46 |
query = self.t.lt.query.with_entities(func.avg(self.t.lt.duration).label('average')).filter(self.t.lt.dcontext == "callback", self.t.lt.calldate.between(date, dateRange)) |
47 |
average = 0 |
48 |
for res in query: |
49 |
if res.average is not None: |
50 |
average = res.average/60 |
51 |
return round(average, 4) |
52 |
|
53 |
def sum(self, date, dateRange): |
54 |
query = self.t.lt.query.with_entities(func.sum(self.t.lt.duration).label('sum')).filter(self.t.lt.dcontext == "callback", self.t.lt.calldate.between(date, dateRange)) |
55 |
sum = 0 |
56 |
for res in query: |
57 |
if res.sum is not None: |
58 |
sum = res.sum/60 |
59 |
return round(sum, 4) |
60 |
|
61 |
|
62 |
def missedCalls(self, date, dateRange, modem=None): |
63 |
if modem is None: |
64 |
return self.t.lt.query.filter(((self.t.lt.dcontext == 'mobilink') | |
65 |
(self.t.lt.dcontext == 'mobilinktata')) & |
66 |
(self.t.lt.calldate.between(date, dateRange))).count() |
67 |
else: |
68 |
return self.t.lt.query.filter(self.t.lt.dcontext == modem, self.t.lt.calldate.between(date, dateRange)).count() |
69 |
|
70 |
|
71 |
def answeredCalls(self, date, dateRange): |
72 |
return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', |
73 |
self.t.lt.calldate.between(date, dateRange)).count() |
74 |
|
75 |
|
76 |
def filter_calls_by_duration(self, date, dateRange, duration): |
77 |
return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', self.t.lt.duration < duration, self.t.lt.calldate.between(date, dateRange)).count() |
78 |
|
79 |
|
80 |
def call_distribution(self, date, dateRange, dcontext): |
81 |
startTimeStamp = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') |
82 |
endTimeStamp = datetime.datetime.strptime(dateRange, '%Y-%m-%d %H:%M:%S') |
83 |
result = self.t.lt.query.filter(self.t.lt.dcontext == dcontext, self.t.lt.calldate.between(date, dateRange)) |
84 |
slots = {} |
85 |
while(startTimeStamp - endTimeStamp <= datetime.timedelta(0)): |
86 |
date1 = startTimeStamp |
87 |
date2 = startTimeStamp + datetime.timedelta(seconds=3600) |
88 |
startTimeStamp = date2 |
89 |
if slots.has_key('{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))): |
90 |
slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] += result.filter(self.t.lt.calldate.between(date1, date2)).count() |
91 |
else: |
92 |
slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] = result.filter(self.t.lt.calldate.between(date1, date2)).count() |
93 |
|
94 |
maxLoad = max(slots, key = lambda x: slots.get(x) ) |
95 |
minLoad = [] |
96 |
for slot in slots: |
97 |
if min(slots, slots[slot]) == 0: |
98 |
minLoad.append(slot) |
99 |
return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": repr(minLoad)} |
100 |
|
101 |
|
102 |
def calls_unanswered(self, date, dateRange): |
103 |
return self.t.lt.query.filter(self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)).count() |
104 |
|
105 |
|
106 |
def max_duration_UC(self, date, dateRange): |
107 |
query = self.t.lt.query.with_entities(func.max(self.t.lt.duration).label('duration')).filter(self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)) |
108 |
duration = 0 |
109 |
for result in query: |
110 |
if result.duration is not None: |
111 |
duration = result.duration |
112 |
return duration |