iwla

iwla Git Source Tree

Root/plugins/display/referers.py

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 Referers page
29
30Plugin requirements :
31 post_analysis/referers
32
33Conf values needed :
34 max_referers_displayed*
35 create_all_referers_page*
36 max_key_phrases_displayed*
37 create_all_key_phrases_page*
38
39Output files :
40 OUTPUT_ROOT/year/month/referers.html
41 OUTPUT_ROOT/year/month/key_phrases.html
42 OUTPUT_ROOT/year/month/index.html
43
44Statistics creation :
45 None
46
47Statistics update :
48 None
49
50Statistics deletion :
51 None
52"""
53
54class IWLADisplayReferers(IPlugin):
55 def __init__(self, iwla):
56 super(IWLADisplayReferers, self).__init__(iwla)
57 self.API_VERSION = 1
58 self.requires = ['IWLAPostAnalysisReferers']
59 self.max_referers = self.iwla.getConfValue('max_referers_displayed', 0)
60 self.create_all_referers = self.iwla.getConfValue('create_all_referers_page', True)
61 self.max_key_phrases = self.iwla.getConfValue('max_key_phrases_displayed', 0)
62 self.create_all_key_phrases = self.iwla.getConfValue('create_all_key_phrases_page', True)
63
64 def hook(self):
65 display = self.iwla.getDisplay()
66 month_stats = self.iwla.getMonthStats()
67 referers = month_stats.get('referers', {})
68 robots_referers = month_stats.get('robots_referers', {})
69 search_engine_referers = month_stats.get('search_engine_referers', {})
70 key_phrases = month_stats.get('key_phrases', {})
71
72 top_referers = [(k, referers[k]['pages']) for k in referers.keys()]
73 top_referers = sorted(top_referers, key=lambda t: t[1], reverse=True)
74
75 top_robots_referers = [(k, robots_referers[k]['pages']) for k in robots_referers.keys()]
76 top_robots_referers = sorted(top_robots_referers, key=lambda t: t[1], reverse=True)
77
78 top_search_engine_referers = [(k, search_engine_referers[k]['pages']) for k in search_engine_referers.keys()]
79 top_search_engine_referers = sorted(top_search_engine_referers, key=lambda t: t[1], reverse=True)
80
81 top_key_phrases = key_phrases.items()
82 top_key_phrases = sorted(top_key_phrases, key=lambda t: t[1], reverse=True)
83
84 cur_time = self.iwla.getCurTime()
85 index = self.iwla.getDisplayIndex()
86
87 # All referers in a file
88 if self.create_all_referers:
89 title = createCurTitle(self.iwla, u'Connexion from')
90
91 filename = 'referers.html'
92 path = self.iwla.getCurDisplayPath(filename)
93
94 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
95 table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Connexion from'), [self.iwla._(u'Origin'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
96 table.setColsCSSClass(['', 'iwla_page', 'iwla_hit'])
97
98 total_search = [0]*3
99 table.appendRow(['<b>%s</b>' % (self.iwla._(u'Search Engine')), '', ''])
100 new_list = self.max_referers and top_search_engine_referers[:self.max_referers] or top_search_engine_referers
101 for r,_ in new_list:
102 row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
103 total_search[1] += search_engine_referers[r]['pages']
104 total_search[2] += search_engine_referers[r]['hits']
105 table.appendRow(row)
106 if self.max_referers:
107 others = 0
108 for (uri, entrance) in top_referers[self.max_referers:]:
109 others += entrance
110 table.appendRow([self.iwla._(u'Others'), others])
111 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
112
113 total_external = [0]*3
114 table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL')), '', ''])
115 new_list = self.max_referers and top_referers[:self.max_referers] or top_referers
116 for r,_ in new_list:
117 row = [generateHTMLLink(r), referers[r]['pages'], referers[r]['hits']]
118 total_external[1] += referers[r]['pages']
119 total_external[2] += referers[r]['hits']
120 table.appendRow(row)
121 if self.max_referers:
122 others = 0
123 for (uri, entrance) in top_referers[self.max_referers:]:
124 others += entrance
125 table.appendRow([self.iwla._(u'Others'), others])
126 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
127
128 total_robot = [0]*3
129 table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL (robot)')), '', ''])
130 new_list = self.max_referers and top_robots_referers[:self.max_referers] or top_robots_referers
131 for r,_ in new_list:
132 row = [generateHTMLLink(r), robots_referers[r]['pages'], robots_referers[r]['hits']]
133 total_robot[1] += robots_referers[r]['pages']
134 total_robot[2] += robots_referers[r]['hits']
135 table.appendRow(row)
136 if self.max_referers:
137 others = 0
138 for (uri, entrance) in top_referers[self.max_referers:]:
139 others += entrance
140 table.appendRow([self.iwla._(u'Others'), others])
141 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
142
143 page.appendBlock(table)
144
145 display.addPage(page)
146
147 title = self.iwla._(u'Top Referers')
148 if self.create_all_referers:
149 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All Referers'))
150 title = '%s - %s' % (title, link)
151
152 # Top referers in index
153 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Origin'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
154 table.setColsCSSClass(['', 'iwla_page', 'iwla_hit'])
155
156 table.appendRow(['<b>%s</b>' % (self.iwla._(u'Search Engine')), '', ''])
157 for r,_ in top_search_engine_referers[:10]:
158 row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
159 total_search[1] -= search_engine_referers[r]['pages']
160 total_search[2] -= search_engine_referers[r]['hits']
161 table.appendRow(row)
162 if total_search[1] or total_search[2]:
163 total_search[0] = self.iwla._(u'Others')
164 table.appendRow(total_search)
165 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
166
167 table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL')), '', ''])
168 for r,_ in top_referers[:10]:
169 row = [generateHTMLLink(r), referers[r]['pages'], referers[r]['hits']]
170 total_external[1] -= referers[r]['pages']
171 total_external[2] -= referers[r]['hits']
172 table.appendRow(row)
173 if total_external[1] or total_external[2]:
174 total_external[0] = self.iwla._(u'Others')
175 table.appendRow(total_external)
176 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
177
178 table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL (robot)')), '', ''])
179 for r,_ in top_robots_referers[:10]:
180 row = [generateHTMLLink(r), robots_referers[r]['pages'], robots_referers[r]['hits']]
181 total_robot[1] -= robots_referers[r]['pages']
182 total_robot[2] -= robots_referers[r]['hits']
183 table.appendRow(row)
184 if total_robot[1] or total_robot[2]:
185 total_robot[0] = self.iwla._(u'Others')
186 table.appendRow(total_robot)
187 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
188
189 index.appendBlock(table)
190
191 # All key phrases in a file
192 if self.create_all_key_phrases:
193 title = createCurTitle(self.iwla, u'Key Phrases')
194
195 filename = 'key_phrases.html'
196 path = self.iwla.getCurDisplayPath(filename)
197
198 total_search = [0]*2
199 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
200 table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'All key phrases'), [self.iwla._(u'Key phrase'), self.iwla._(u'Search')])
201 table.setColsCSSClass(['', 'iwla_search'])
202 new_list = self.max_key_phrases and top_key_phrases[:self.max_key_phrases] or top_key_phrases
203 for phrase in new_list:
204 table.appendRow([phrase[0], phrase[1]])
205 total_search[1] += phrase[1]
206 page.appendBlock(table)
207
208 display.addPage(page)
209
210 title = self.iwla._(u'Top key phrases')
211 if self.create_all_key_phrases:
212 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All key phrases'))
213 title = '%s - %s' % (title, link)
214
215 # Top key phrases in index
216 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Key phrase'), self.iwla._(u'Search')])
217 table.setColsCSSClass(['', 'iwla_search'])
218 for phrase in top_key_phrases[:10]:
219 table.appendRow([phrase[0], phrase[1]])
220 total_search[1] -= phrase[1]
221 if total_search[1]:
222 total_search[0] = self.iwla._(u'Others')
223 table.appendRow(total_search)
224 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
225 index.appendBlock(table)

Archive Download this file

Branches

Tags