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, channel, date, dateRange):
25
        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))
26
        sum = 0
27
        for res in query:
28
            if res.sum is not None:
29
                sum = res.sum/60
30
        return round(sum, 4)
31
32
33
    def average(self, date, dateRange):
34
        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))
35
        average = 0
36
        for res in query:
37
            if res.average is not None:
38
                average = res.average/60
39
        return round(average, 4)
40
41
    def sum(self, date, dateRange):
42
        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))
43
        sum = 0
44
        for res in query:
45
            if res.sum is not None:
46
                sum = res.sum/60
47
        return round(sum, 4)
48
49
50
    def missedCalls(self, date, dateRange, modem=None):
51
        if modem is None:
52
            return self.t.lt.query.filter(((self.t.lt.dcontext == 'mobilink') |
53
                                     (self.t.lt.dcontext == 'mobilinktata')) &
54
                                    (self.t.lt.calldate.between(date, dateRange))).count()
55
        else:
56
            return self.t.lt.query.filter(self.t.lt.dcontext == modem, self.t.lt.calldate.between(date, dateRange)).count()
57
58
59
    def answeredCalls(self, date, dateRange):
60
        return self.t.lt.query.filter(self.t.lt.dcontext == 'callback',
61
                                    self.t.lt.calldate.between(date, dateRange)).count()
62
63
64
    def filter_calls_by_duration(self, date, dateRange, duration):
65
        return self.t.lt.query.filter(self.t.lt.dcontext == 'callback', self.t.lt.duration < duration, self.t.lt.calldate.between(date, dateRange)).count()
66
67
68
    def call_distribution(self, date, dateRange, dcontext):
69
        startTimeStamp = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
70
        endTimeStamp = datetime.datetime.strptime(dateRange, '%Y-%m-%d %H:%M:%S')
71
        result = self.t.lt.query.filter(self.t.lt.dcontext == dcontext, self.t.lt.calldate.between(date, dateRange))
72
        slots = {}
73
        while(startTimeStamp - endTimeStamp <= datetime.timedelta(0)):
74
            date1 = startTimeStamp
75
            date2 = startTimeStamp + datetime.timedelta(seconds=3600)
76
            startTimeStamp = date2
77
            if slots.has_key('{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))):
78
                slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] += result.filter(self.t.lt.calldate.between(date1, date2)).count()
79
            else:
80
                slots['{0}-{1}'.format(date1.strftime('%H'), date2.strftime('%H'))] = result.filter(self.t.lt.calldate.between(date1, date2)).count()
81
82
        maxLoad = max(slots, key = lambda x: slots.get(x) )
83
        minLoad = []
84
        for slot in slots:
85
            if min(slots, slots[slot]) == 0:
86
                minLoad.append(slot)
87
        return {"maxLoad": maxLoad, "maxCalls": slots[maxLoad], "minLoad": repr(minLoad)}
88
89
90
    def calls_unanswered(self, date, dateRange):
91
        return self.t.lt.query.filter(self.t.lt.dcontext=='default', self.t.lt.calldate.between(date,dateRange)).count()
92
93
94
    def max_duration_UC(self, date, dateRange):
95
        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))
96
        duration = 0
97
        for result in query:
98
            if result.duration is not None:
99
                duration = result.duration
100
        return duration