1
import logger
2
from sqlalchemy.sql import func
3
import datetime
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
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
23
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
34
        sum = 0
35
        for res in query:
36
            if res.sum is not None:
37
                sum = res.sum/60
38
        return round(sum, 4)
39
40
41
    def average(self, date, dateRange):
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
48
        average = 0
49
        for res in query:
50
            if res.average is not None:
51
                average = res.average/60
52
        return round(average, 4)
53
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)
65
66
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') |
70
                                           (self.t.lt.dcontext ==
71
                                            'mobilinktata')) &
72
                                          (self.t.lt.calldate.between(date,
73
                                                                      dateRange))).count()
74
        else:
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()
84
85
86
    def answeredCalls(self, date, dateRange):
87
        return self.t.lt.query.filter(self.t.lt.dcontext == 'callback',
88
                                      self.t.lt.disposition == 'ANSWERED',
89
                                      self.t.lt.calldate.between(date,
90
                                                                 dateRange)).count()
91
92
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()
97
98
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')
102
        result = self.t.lt.query.filter(
103
            self.t.lt.dcontext == dcontext, self.t.lt.calldate.between(date, dateRange))
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'))):
110
                slots['{0}-{1}'.format(date1.strftime('%H'),
111
                                       date2.strftime('%H'))] += result.filter(
112
                                           self.t.lt.calldate.between(date1, date2)).count()
113
            else:
114
                slots['{0}-{1}'.format(date1.strftime('%H'),
115
                                       date2.strftime('%H'))] = result.filter(
116
                                           self.t.lt.calldate.between(date1, date2)).count()
117
118
        maxLoad = max(slots, key = lambda x: slots.get(x) )
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)}
124
125
126
    def calls_unanswered(self, date, dateRange):
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()
130
131
132
    def max_duration_UC(self, date, dateRange):
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))
136
        duration = 0
137
        for result in query:
138
            if result.duration is not None:
139
                duration = result.duration
140
        return duration