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() |
|
18 |
def filter_by_title(self, title, date, dateRange): |
|
19 |
return self.t.lt.query.filter(self.t.lt.title.like(title+'%'), |
|
20 |
self.t.lt.posted.between(date, dateRange)).count() |
|
21 |
|
|
22 |
def load(self, channel, date, dateRange): |
|
23 |
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)) |
|
24 |
sum = 0 |
|
25 |
for res in query: |
|
26 |
sum = res.sum/60 |
|
27 |
return sum |
|
28 |
|
|
29 |
def average(self, date, dateRange): |
|
30 |
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)) |
|
31 |
average = 0 |
|
32 |
for res in query: |
|
33 |
average = res.average/60 |
|
34 |
return average |
|
35 |
|
|
36 |
def sum(self, 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.calldate.between(date, dateRange)) |
|
38 |
sum = 0 |
|
39 |
for res in query: |
|
40 |
sum = res.sum/60 |
|
41 |
return sum |
|
42 |
|
9d54d1e by Arvind at 2014-01-09 |
43 |
def missedCalls(self, date, dateRange, modem=None): |
|
44 |
if modem is None: |
|
45 |
return self.t.lt.query.filter(((self.t.lt.dcontext == 'mobilink') | |
c5ab5de by Arvind at 2014-01-09 |
46 |
(self.t.lt.dcontext == 'mobilinktata')) & |
|
47 |
(self.t.lt.calldate.between(date, dateRange))).count() |
9d54d1e by Arvind at 2014-01-09 |
48 |
else: |
|
49 |
return self.t.lt.query.filter(self.t.lt.dcontext == modem, self.t.lt.calldate.between(date, dateRange)).count() |
c5ab5de by Arvind at 2014-01-09 |
50 |
|
|
51 |
def answeredCalls(self, date, dateRange): |
|
52 |
return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', |
9d54d1e by Arvind at 2014-01-09 |
53 |
self.t.lt.calldate.between(date, dateRange)).count() |
|
54 |
|
|
55 |
def filter_calls_by_duration(self, date, dateRange, duration): |
2e37597 by Arvind at 2014-01-14 |
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) ) |
3ea89c3 by Arvind at 2014-01-24 |
73 |
minLoad = [] |
|
74 |
for slot in slots: |
|
75 |
if min(slots, slots[slot]) == 0: |
|
76 |
minLoad.append(slot) |
|
77 |
return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": repr(minLoad)} |