iwla

iwla Git Source Tree

Root/plugins/display/top_hits.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 hits page
29
30Plugin requirements :
31 post_analysis/top_hits
32
33Conf values needed :
34 max_hits_displayed*
35 create_all_hits_page*
36
37Output files :
38 OUTPUT_ROOT/year/month/top_hits.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 IWLADisplayTopHits(IPlugin):
52 def __init__(self, iwla):
53 super(IWLADisplayTopHits, self).__init__(iwla)
54 self.API_VERSION = 1
55 self.requires = ['IWLAPostAnalysisTopHits']
56 self.max_hits = self.iwla.getConfValue('max_hits_displayed', 0)
57 self.create_all_hits = self.iwla.getConfValue('create_all_hits_page', True)
58
59 def hook(self):
60 display = self.iwla.getDisplay()
61 top_hits = self.iwla.getMonthStats()['top_hits']
62 top_hits = sorted(top_hits.items(), key=lambda t: t[1], reverse=True)
63
64 # All in a file
65 if self.create_all_hits:
66 title = createCurTitle(self.iwla, u'All Hits')
67 filename = 'top_hits.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 Hits'), [self.iwla._(u'URI'), self.iwla._(u'Entrance')])
72 table.setColsCSSClass(['', 'iwla_hit'])
73 total_hits = [0]*2
74 new_list = self.max_hits and top_hits[:self.max_hits] or top_hits
75 for (uri, entrance) in new_list:
76 table.appendRow([generateHTMLLink(uri), entrance])
77 total_hits[1] += entrance
78 if self.max_hits:
79 others = 0
80 for (uri, entrance) in top_hits[self.max_hits:]:
81 others += entrance
82 table.appendRow([self.iwla._(u'Others'), others])
83 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
84
85 page.appendBlock(table)
86
87 display.addPage(page)
88
89 title = 'Top Hits'
90 if self.create_all_hits:
91 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All Hits'))
92 title = '%s - %s' % (title, link)
93
94 # Top in index
95 index = self.iwla.getDisplayIndex()
96
97 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'URI'), self.iwla._(u'Entrance')])
98 table.setColsCSSClass(['', 'iwla_hit'])
99 for (uri, entrance) in top_hits[:10]:
100 table.appendRow([generateHTMLLink(uri), entrance])
101 total_hits[1] -= entrance
102 if total_hits[1]:
103 total_hits[0] = self.iwla._(u'Others')
104 table.appendRow(total_hits)
105 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
106 index.appendBlock(table)

Archive Download this file

Branches

Tags