c5ab5de by Arvind at 2014-01-09 |
1 |
import logger |
|
2 |
from sqlalchemy.sql import func |
2e37597 by Arvind at 2014-01-14 |
3 |
import datetime |
c5ab5de by Arvind at 2014-01-09 |
4 |
|
|
5 |
class Query: |
|
6 |
"""Objects of query class can be used to run specific queries.""" |
|
7 |
def __init__(self, table): |
|
8 |
self.t = logger.Logger(table) |
|
9 |
|
|
10 |
|
|
11 |
def posts(self, date, dateRange): |
|
12 |
return self.t.lt.query.filter(self.t.lt.status == 3, |
|
13 |
self.t.lt.posted.between(date, dateRange)).count() |
|
14 |
|
|
15 |
def recordings(self, date, dateRange): |
|
16 |
return self.t.lt.query.filter(self.t.lt.posted.between(date, |
|
17 |
dateRange)).count() |
41df78b by Arvind at 2014-03-10 |
18 |
|
c5ab5de by Arvind at 2014-01-09 |
19 |
def filter_by_title(self, title, date, dateRange): |
|
20 |
return self.t.lt.query.filter(self.t.lt.title.like(title+'%'), |
|
21 |
self.t.lt.posted.between(date, dateRange)).count() |
|
22 |
|
41df78b by Arvind at 2014-03-10 |
23 |
|
db3c81d by Arvind at 2014-03-11 |
24 |
def totalMinutes(self, date, dateRange, channel=None): |
|
25 |
query = self.t.lt.query.with_entities( |
|
26 |
func.sum(self.t.lt.billsec).label('sum')).filter( |
|
27 |
self.t.lt.dcontext == "callback", |
|
28 |
self.t.lt.disposition == "ANSWERED", |
|
29 |
self.t.lt.calldate.between(date, dateRange)) |
|
30 |
|
|
31 |
if channel is not None: |
|
32 |
query = query.filter(self.t.lt.channel.like(channel+'%')) |
|
33 |
|
c5ab5de by Arvind at 2014-01-09 |
34 |
sum = 0 |
|
35 |
for res in query: |
41df78b by Arvind at 2014-03-10 |
36 |
if res.sum is not None: |
|
37 |
sum = res.sum/60 |
|
38 |
return round(sum, 4) |
|
39 |
|
c5ab5de by Arvind at 2014-01-09 |
40 |
|
|
41 |
def average(self, date, dateRange): |
db3c81d by Arvind at 2014-03-11 |
42 |
query = self.t.lt.query.with_entities( |
|
43 |
func.avg(self.t.lt.billsec).label('average')).filter( |
|
44 |
self.t.lt.dcontext == "callback", |
|
45 |
self.t.lt.disposition == "ANSWERED", |
|
46 |
self.t.lt.calldate.between(date, dateRange)) |
|
47 |
|
c5ab5de by Arvind at 2014-01-09 |
48 |
average = 0 |
|
49 |
for res in query: |
41df78b by Arvind at 2014-03-10 |
50 |
if res.average is not None: |
|
51 |
average = res.average/60 |
|
52 |
return round(average, 4) |
c5ab5de by Arvind at 2014-01-09 |
53 |
|
db3c81d by Arvind at 2014-03-11 |
54 |
# def sum(self, date, dateRange): |
|
55 |
# query = self.t.lt.query.with_entities( |
|
56 |
# func.sum(self.t.lt.billsec).label('sum')).filter( |
|
57 |
# self.t.lt.dcontext == "callback", |
|
58 |
# self.t.lt.disposition == "ANSWERED", |
|
59 |
# self.t.lt.calldate.between(date, dateRange)) |
|
60 |
# sum = 0 |
|
61 |
# for res in query: |
|
62 |
# if res.sum is not None: |
|
63 |
# sum = res.sum/60 |
|
64 |
# return round(sum, 4) |
41df78b by Arvind at 2014-03-10 |
65 |
|
c5ab5de by Arvind at 2014-01-09 |
66 |
|
9d54d1e by Arvind at 2014-01-09 |
67 |
def missedCalls(self, date, dateRange, modem=None): |
|
68 |
if modem is None: |
|
69 |
return self.t.lt.query.filter(((self.t.lt.dcontext == 'mobilink') | |
db3c81d by Arvind at 2014-03-11 |
70 |
(self.t.lt.dcontext == |
|
71 |
'mobilinktata')) & |
|
72 |
(self.t.lt.calldate.between(date, |
|
73 |
dateRange))).count() |
9d54d1e by Arvind at 2014-01-09 |
74 |
else: |
db3c81d by Arvind at 2014-03-11 |
75 |
return self.t.lt.query.filter(self.t.lt.dcontext == modem, |
|
76 |
self.t.lt.calldate.between(date, |
|
77 |
dateRange)).count() |
|
78 |
|
|
79 |
|
|
80 |
def unAccountedCalls(self, date, dateRange): |
|
81 |
return self.t.lt.query.filter(self.t.lt.dcontext == 'default', |
|
82 |
self.t.lt.calldate.between(date, |
|
83 |
dateRange)).count() |
c5ab5de by Arvind at 2014-01-09 |
84 |
|
41df78b by Arvind at 2014-03-10 |
85 |
|
c5ab5de by Arvind at 2014-01-09 |
86 |
def answeredCalls(self, date, dateRange): |
|
87 |
return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', |
db3c81d by Arvind at 2014-03-11 |
88 |
self.t.lt.disposition == 'ANSWERED', |
|
89 |
self.t.lt.calldate.between(date, |
|
90 |
dateRange)).count() |
9d54d1e by Arvind at 2014-01-09 |
91 |
|
41df78b by Arvind at 2014-03-10 |
92 |
|
db3c81d by Arvind at 2014-03-11 |
93 |
def filter_calls_by_duration(self, date, dateRange, billsec): |
|
94 |
return self.t.lt.query.filter( |
|
95 |
self.t.lt.dcontext == 'callback', self.t.lt.billsec < billsec, |
|
96 |
self.t.lt.calldate.between(date, dateRange)).count() |
2e37597 by Arvind at 2014-01-14 |
97 |
|
41df78b by Arvind at 2014-03-10 |
98 |
|
2e37597 by Arvind at 2014-01-14 |
99 |
def call_distribution(self, date, dateRange, dcontext): |
|
100 |
startTimeStamp = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') |
|
101 |
endTimeStamp = datetime.datetime.strptime(dateRange, '%Y-%m-%d %H:%M:%S') |
db3c81d by Arvind at 2014-03-11 |
102 |
result = self.t.lt.query.filter( |
|
103 |
self.t.lt.dcontext == dcontext, self.t.lt.calldate.between(date, dateRange)) |
2e37597 by Arvind at 2014-01-14 |
104 |
slots = {} |
|
105 |
while(startTimeStamp - endTimeStamp <= datetime.timedelta(0)): |
|
106 |
date1 = startTimeStamp |
|
107 |
date2 = startTimeStamp + datetime.timedelta(seconds=3600) |
|
108 |
startTimeStamp = date2 |
|
109 |
if slots.has_key('{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))): |
db3c81d by Arvind at 2014-03-11 |
110 |
slots['{0}-{1}'.format(date1.strftime('%H'), |
|
111 |
date2.strftime('%H'))] += result.filter( |
|
112 |
self.t.lt.calldate.between(date1, date2)).count() |
2e37597 by Arvind at 2014-01-14 |
113 |
else: |
db3c81d by Arvind at 2014-03-11 |
114 |
slots['{0}-{1}'.format(date1.strftime('%H'), |
|
115 |
date2.strftime('%H'))] = result.filter( |
|
116 |
self.t.lt.calldate.between(date1, date2)).count() |
2e37597 by Arvind at 2014-01-14 |
117 |
|
|
118 |
maxLoad = max(slots, key = lambda x: slots.get(x) ) |
3ea89c3 by Arvind at 2014-01-24 |
119 |
minLoad = [] |
|
120 |
for slot in slots: |
|
121 |
if min(slots, slots[slot]) == 0: |
|
122 |
minLoad.append(slot) |
|
123 |
return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": repr(minLoad)} |
efa17c7 by Arvind at 2014-01-25 |
124 |
|
41df78b by Arvind at 2014-03-10 |
125 |
|
efa17c7 by Arvind at 2014-01-25 |
126 |
def calls_unanswered(self, date, dateRange): |
db3c81d by Arvind at 2014-03-11 |
127 |
return self.t.lt.query.filter(self.t.lt.disposition == 'NO ANSWER', |
|
128 |
self.t.lt.dcontext == 'callback', |
|
129 |
self.t.lt.calldate.between(date,dateRange)).count() |
54dd346 by Arvind at 2014-01-25 |
130 |
|
41df78b by Arvind at 2014-03-10 |
131 |
|
54dd346 by Arvind at 2014-01-25 |
132 |
def max_duration_UC(self, date, dateRange): |
db3c81d by Arvind at 2014-03-11 |
133 |
query = self.t.lt.query.with_entities( |
|
134 |
func.max(self.t.lt.duration).label('duration')).filter( |
|
135 |
self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)) |
54dd346 by Arvind at 2014-01-25 |
136 |
duration = 0 |
|
137 |
for result in query: |
41df78b by Arvind at 2014-03-10 |
138 |
if result.duration is not None: |
|
139 |
duration = result.duration |
54dd346 by Arvind at 2014-01-25 |
140 |
return duration |