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
41df78b by Arvind at 2014-03-10 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
c5ab5de by Arvind at 2014-01-09 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()
41df78b by Arvind at 2014-03-10 30
c5ab5de by Arvind at 2014-01-09 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
41df78b by Arvind at 2014-03-10 35
36
    def totalMinutes(self, channel, date, dateRange):
c5ab5de by Arvind at 2014-01-09 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:
41df78b by Arvind at 2014-03-10 40
            if res.sum is not None:
41
                sum = res.sum/60
42
        return round(sum, 4)
43
c5ab5de by Arvind at 2014-01-09 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:
41df78b by Arvind at 2014-03-10 49
            if res.average is not None:
50
                average = res.average/60
51
        return round(average, 4)
c5ab5de by Arvind at 2014-01-09 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:
41df78b by Arvind at 2014-03-10 57
            if res.sum is not None:
58
                sum = res.sum/60
59
        return round(sum, 4)
60
c5ab5de by Arvind at 2014-01-09 61
9d54d1e by Arvind at 2014-01-09 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') |
c5ab5de by Arvind at 2014-01-09 65
                                     (self.t.lt.dcontext == 'mobilinktata')) &
66
                                    (self.t.lt.calldate.between(date, dateRange))).count()
9d54d1e by Arvind at 2014-01-09 67
        else:
68
            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 69
41df78b by Arvind at 2014-03-10 70
c5ab5de by Arvind at 2014-01-09 71
    def answeredCalls(self, date, dateRange):
72
        return self.t.lt.query.filter(self.t.lt.dcontext == 'callback',
9d54d1e by Arvind at 2014-01-09 73
                                    self.t.lt.calldate.between(date, dateRange)).count()
74
41df78b by Arvind at 2014-03-10 75
9d54d1e by Arvind at 2014-01-09 76
    def filter_calls_by_duration(self, date, dateRange, duration):
2e37597 by Arvind at 2014-01-14 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
41df78b by Arvind at 2014-03-10 79
2e37597 by Arvind at 2014-01-14 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) )
3ea89c3 by Arvind at 2014-01-24 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)}
efa17c7 by Arvind at 2014-01-25 100
41df78b by Arvind at 2014-03-10 101
efa17c7 by Arvind at 2014-01-25 102
    def calls_unanswered(self, date, dateRange):
54dd346 by Arvind at 2014-01-25 103
        return self.t.lt.query.filter(self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)).count()
104
41df78b by Arvind at 2014-03-10 105
54dd346 by Arvind at 2014-01-25 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:
41df78b by Arvind at 2014-03-10 110
            if result.duration is not None:
111
                duration = result.duration
54dd346 by Arvind at 2014-01-25 112
        return duration