iwla

iwla Git Source Tree

Root/plugins/display/top_pages.py

Source at commit 4e02325733e5e8e4f5de2f0046e721f8da7abfff created 6 years 10 months ago.
By Gregory Soutade, Initial commit
1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2015
4
5# This file is part of iwla
6
7# iwla is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# iwla is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with iwla. If not, see <http://www.gnu.org/licenses/>.
19#
20
21from iwla import IWLA
22from iplugin import IPlugin
23from display import *
24
25"""
26Display hook
27
28Create TOP pages page
29
30Plugin requirements :
31 post_analysis/top_pages
32
33Conf values needed :
34 max_pages_displayed*
35 create_all_pages_page*
36
37Output files :
38 OUTPUT_ROOT/year/month/top_pages.html
39 OUTPUT_ROOT/year/month/index.html
40
41Statistics creation :
42 None
43
44Statistics update :
45 None
46
47Statistics deletion :
48 None
49"""
50
51class IWLADisplayTopPages(IPlugin):
52 def __init__(self, iwla):
53 super(IWLADisplayTopPages, self).__init__(iwla)
54 self.API_VERSION = 1
55 self.requires = ['IWLAPostAnalysisTopPages']
56 self.max_pages = self.iwla.getConfValue('max_pages_displayed', 0)
57 self.create_all_pages = self.iwla.getConfValue('create_all_pages_page', True)
58
59 def hook(self):
60 display = self.iwla.getDisplay()
61 top_pages = self.iwla.getMonthStats()['top_pages']
62 top_pages = sorted(top_pages.items(), key=lambda t: t[1], reverse=True)
63
64 # All in a page
65 if self.create_all_pages:
66 title = createCurTitle(self.iwla, u'All Pages')
67 filename = 'top_pages.html'
68 path = self.iwla.getCurDisplayPath(filename)
69
70 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
71 table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'All Pages'), [self.iwla._(u'URI'), self.iwla._(u'Entrance')])
72 table.setColsCSSClass(['', 'iwla_hit'])
73 total_hits = [0]*2
74 new_list = self.max_pages and top_pages[:self.max_pages] or top_pages
75 for (uri, entrance) in new_list:
76 table.appendRow([generateHTMLLink(uri), entrance])
77 total_hits[1] += entrance
78 if self.max_pages:
79 others = 0
80 for (uri, entrance) in top_pages[self.max_pages:]:
81 others += entrance
82 table.appendRow([self.iwla._(u'Others'), others])
83 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
84 page.appendBlock(table)
85
86 display.addPage(page)
87
88 title = self.iwla._(u'Top Pages')
89 if self.create_all_pages:
90 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All Pages'))
91 title = '%s - %s' % (title, link)
92
93 # Top in index
94 index = self.iwla.getDisplayIndex()
95
96 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'URI'), self.iwla._(u'Entrance')])
97 table.setColsCSSClass(['', 'iwla_hit'])
98 for (uri, entrance) in top_pages[:10]:
99 table.appendRow([generateHTMLLink(uri), entrance])
100 total_hits[1] -= entrance
101 if total_hits[1]:
102 total_hits[0] = self.iwla._(u'Others')
103 table.appendRow(total_hits)
104 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
105 index.appendBlock(table)

Archive Download this file

Branches

Tags