primary.py 5.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# copyright 2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

"""cubicweb-awstats views/forms/actions/components for web ui"""

Arthur Lutz's avatar
Arthur Lutz committed
19
from cubicweb.utils import RepeatList
20
21
from cubicweb.view import EntityView
from cubicweb.web.views import primary, navigation
22
23
24
from cubicweb.selectors import is_instance

class StatPeriodPrimaryView(primary.PrimaryView):
25
26
27
28
29
30
31
32
    """

    `column_types_aggr` enables you to combine results by type
    For example BlogEntry and MicroBlogEntries in one table and Cards in separate table :

    column_types_aggr = (('MicroBlogEntry', 'BlogEntry'),
                        ('Card'))
    """
33
    __select__ = is_instance('StatPeriod')
34
    column_types_aggr = None #tuple of tuples of types
35
36

    def cell_call(self, row, col):
Arthur Lutz's avatar
Arthur Lutz committed
37
        _ = self._cw._
38
        req = self._cw
Arthur Lutz's avatar
Arthur Lutz committed
39
        self.w(u'<div id="primarystatperiod">')
40
        entity = self.cw_rset.get_entity(row, col)
Arthur Lutz's avatar
Arthur Lutz committed
41
42
43
44
45
        self.w(u'<h1>%s %s - %s (%s %s)</h1>' % (_('Statistics for period :'),
                                              entity.printable_value('start'),
                                              entity.printable_value('stop'),
                                              (entity.stop - entity.start).days,
                                              _('days')) )
Arthur Lutz's avatar
Arthur Lutz committed
46
        rset = self._cw.execute('DISTINCT Any C WHERE X is Hits, X hit_type C')
47
48
        self.w(u'<a href="%s">%s</a>' % (entity.absolute_url(showall=1),
                                         _('show all results')))
Arthur Lutz's avatar
Arthur Lutz committed
49
50
        for (hit_type,) in rset:
            self.w(u'<h3>%s</h3>' % hit_type)
51
            rql = 'Any X, C ORDERBY C DESC %(limit)s WHERE H stats_about X, H hit_type "%(type)s",'\
Arthur Lutz's avatar
Arthur Lutz committed
52
                  'H count C, H period P, P eid %%(e)s' % {'type': hit_type,
53
                                                           'limit': req.form.get('showall') and ' ' or 'LIMIT 20' }
54
55
56
57
58
59
            if self.column_types_aggr:
                self.w(u'<table><tr>')
                for types in self.column_types_aggr:
                    self.w(u'<td>')
                    typedrql = rql + ', X is in (%s)' % ','.join(types)
                    rset = self._cw.execute(typedrql, {'e':entity.eid})
Arthur Lutz's avatar
Arthur Lutz committed
60
61
62
63
64
                    self.wview('table', rset, 'null')
                    # cf rql/editextensions.py unset_limit
                    nolimit_rql = typedrql.replace('LIMIT 20', '')
                    self.w(u'<a href="%s">Export CSV</a>' % self._cw.build_url('', rql=nolimit_rql % {'e':entity.eid},
                                                                                vid='csvexport'))
Arthur Lutz's avatar
Arthur Lutz committed
65
                    #FIXME TODO not working right now
66
                    self.wview('piechart', rset, 'null')
67
68
69
70
                    self.w(u'</td>')
                self.w(u'</tr></table>')
            else:
                rset = self._cw.execute(rql, {'e':entity.eid})
Arthur Lutz's avatar
Arthur Lutz committed
71
72
73
74
                self.wview('table', rset, 'null')
                nolimit_rql = rql.replace('LIMIT 20', '')
                self.w(u'<a href="%s">Export CSV</a>' % self._cw.build_url('', rql=nolimit_rql % {'e':entity.eid},
                                                                           vid='csvexport'))
75
76
77
78
79
80
81
82
83
        self.w(u'</div>')


class StatPeriodIPrevNextAdapter(navigation.IPrevNextAdapter):
    __select__ = is_instance('StatPeriod')

    def previous_entity(self):
        entity = self.entity
        execute = self._cw.execute
84
85
86
87
        rset = execute("StatPeriod P ORDERBY S DESC LIMIT 1  WHERE P start S, P2 start S2, P2 eid %(e)s HAVING S < S2",
                       {'e':entity.eid})
        if rset:
            return rset.get_entity(0, 0)
88
89
90
91

    def next_entity(self):
        entity = self.entity
        execute = self._cw.execute
92
93
94
95
        rset = execute("StatPeriod P ORDERBY S LIMIT 1  WHERE P start S, P2 start S2, P2 eid %(e)s HAVING S > S2",
                       {'e':entity.eid})
        if rset:
            return rset.get_entity(0, 0)
96
97


98
class StatGraph(EntityView):
99
    __regid__ = 'webstatsgraph'
100
    # select only instances with stats_about relation
101
102

    def cell_call(self, row, col):
Arthur Lutz's avatar
Arthur Lutz committed
103
        _ = self._cw._
104
105
106
        entity = self.cw_rset.get_entity(row, col)
        self.w(u'<h1>%s %s</h1>' % (_('Graph of hits for'),
                                    entity.dc_title()))
Arthur Lutz's avatar
Arthur Lutz committed
107
        self.w(u'<div id="webstats">')
Arthur Lutz's avatar
Arthur Lutz committed
108
        rql = ('DISTINCT Any S, HITS ORDERBY S WITH S, HITS BEING ('
Arthur Lutz's avatar
Arthur Lutz committed
109
              '(Any START, HITS WHERE H count HITS, H hit_type "normal", H period P, P start START, P stop STOP, H stats_about E, E eid %%(e)s %s)'
Arthur Lutz's avatar
Arthur Lutz committed
110
111
              ' UNION '
              '(Any START, 0 WHERE P is StatPeriod, P start START, P stop STOP, NOT EXISTS(H period P, H stats_about E, E eid %%(e)s) %s))')
112
        plot_displayed = False
Arthur Lutz's avatar
Arthur Lutz committed
113
114
        for constraint, label in ((' HAVING STOP-START <= 20', _('Daily')),
                                  (' HAVING STOP-START >= 20', _('Monthly'))):
Arthur Lutz's avatar
Arthur Lutz committed
115
            rset = self._cw.execute(rql % (constraint, constraint), {'e':entity.eid})
116
117
118
119
            if rset:
                self.w(u'<h2>%s</h2>' % label)
                self.w(self._cw.view('plot', rset, 'null'))
                plot_displayed = True
Arthur Lutz's avatar
Arthur Lutz committed
120
        if not plot_displayed:
121
            self.w(u'<h2>%s<h2>' % _('No stats yet'))
Arthur Lutz's avatar
Arthur Lutz committed
122
        self.w(u'</div>')