iwla

iwla Git Source Tree

Root/plugins/display/referers.py

1import time
2
3from iwla import IWLA
4from iplugin import IPlugin
5from display import *
6
7#
8# Display hook
9#
10# Create Referers page
11#
12# Plugin requirements :
13# post_analysis/referers
14#
15# Conf values needed :
16# max_referers_displayed*
17# create_all_referers_page*
18# max_key_phrases_displayed*
19# create_all_key_phrases_page*
20#
21# Output files :
22# OUTPUT_ROOT/year/month/referers.html
23# OUTPUT_ROOT/year/month/key_phrases.html
24# OUTPUT_ROOT/year/month/index.html
25#
26# Statistics creation :
27# None
28#
29# Statistics update :
30# None
31#
32# Statistics deletion :
33# None
34#
35
36class IWLADisplayReferers(IPlugin):
37 def __init__(self, iwla):
38 super(IWLADisplayReferers, self).__init__(iwla)
39 self.API_VERSION = 1
40 self.requires = ['IWLAPostAnalysisReferers']
41 self.max_referers = self.iwla.getConfValue('max_referers_displayed', 0)
42 self.create_all_referers = self.iwla.getConfValue('create_all_referers_page', True)
43 self.max_key_phrases = self.iwla.getConfValue('max_key_phrases_displayed', 0)
44 self.create_all_key_phrases = self.iwla.getConfValue('create_all_key_phrases_page', True)
45
46 def hook(self):
47 display = self.iwla.getDisplay()
48 month_stats = self.iwla.getMonthStats()
49 referers = month_stats.get('referers', {})
50 robots_referers = month_stats.get('robots_referers', {})
51 search_engine_referers = month_stats.get('search_engine_referers', {})
52 key_phrases = month_stats.get('key_phrases', {})
53
54 top_referers = [(k, referers[k]['pages']) for k in referers.keys()]
55 top_referers = sorted(top_referers, key=lambda t: t[1], reverse=True)
56
57 top_robots_referers = [(k, robots_referers[k]['pages']) for k in robots_referers.keys()]
58 top_robots_referers = sorted(top_robots_referers, key=lambda t: t[1], reverse=True)
59
60 top_search_engine_referers = [(k, search_engine_referers[k]['pages']) for k in search_engine_referers.keys()]
61 top_search_engine_referers = sorted(top_search_engine_referers, key=lambda t: t[1], reverse=True)
62
63 top_key_phrases = key_phrases.items()
64 top_key_phrases = sorted(top_key_phrases, key=lambda t: t[1], reverse=True)
65
66 cur_time = self.iwla.getCurTime()
67 index = self.iwla.getDisplayIndex()
68
69 # All referers in a file
70 if self.create_all_referers:
71 title = time.strftime('Connexion from - %B %Y', cur_time)
72
73 filename = 'referers.html'
74 path = self.iwla.getCurDisplayPath(filename)
75
76 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
77 table = display.createBlock(DisplayHTMLBlockTable, 'Connexion from', ['Origin', 'Pages', 'Hits'])
78 table.setColsCSSClass(['', 'iwla_page', 'iwla_hit'])
79
80 total_search = [0]*3
81 table.appendRow(['<b>Search Engine</b>', '', ''])
82 new_list = self.max_referers and top_search_engine_referers[:self.max_referers] or top_search_engine_referers
83 for r,_ in new_list:
84 row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
85 total_search[1] += search_engine_referers[r]['pages']
86 total_search[2] += search_engine_referers[r]['hits']
87 table.appendRow(row)
88
89 total_external = [0]*3
90 table.appendRow(['<b>External URL</b>', '', ''])
91 new_list = self.max_referers and top_referers[:self.max_referers] or top_referers
92 for r,_ in new_list:
93 row = [generateHTMLLink(r), referers[r]['pages'], referers[r]['hits']]
94 total_external[1] += referers[r]['pages']
95 total_external[2] += referers[r]['hits']
96 table.appendRow(row)
97
98 total_robot = [0]*3
99 table.appendRow(['<b>External URL (robot)</b>', '', ''])
100 new_list = self.max_referers and top_robots_referers[:self.max_referers] or top_robots_referers
101 for r,_ in new_list:
102 row = [generateHTMLLink(r), robots_referers[r]['pages'], robots_referers[r]['hits']]
103 total_robot[1] += robots_referers[r]['pages']
104 total_robot[2] += robots_referers[r]['hits']
105 table.appendRow(row)
106
107 page.appendBlock(table)
108
109 display.addPage(page)
110
111 title = 'Top Referers'
112 if self.create_all_referers:
113 link = '<a href=\'%s\'>All Referers</a>' % (filename)
114 title = '%s - %s' % (title, link)
115
116 # Top referers in index
117 table = display.createBlock(DisplayHTMLBlockTable, title, ['Origin', 'Pages', 'Hits'])
118 table.setColsCSSClass(['', 'iwla_page', 'iwla_hit'])
119
120 table.appendRow(['<b>Search Engine</b>', '', ''])
121 for r,_ in top_search_engine_referers[:10]:
122 row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
123 total_search[1] -= search_engine_referers[r]['pages']
124 total_search[2] -= search_engine_referers[r]['hits']
125 table.appendRow(row)
126 if total_search[1] or total_search[2]:
127 total_search[0] = 'Others'
128 table.appendRow(total_search)
129 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
130
131 table.appendRow(['<b>External URL</b>', '', ''])
132 for r,_ in top_referers[:10]:
133 row = [generateHTMLLink(r), referers[r]['pages'], referers[r]['hits']]
134 total_external[1] -= referers[r]['pages']
135 total_external[2] -= referers[r]['hits']
136 table.appendRow(row)
137 if total_external[1] or total_external[2]:
138 total_external[0] = 'Others'
139 table.appendRow(total_external)
140 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
141
142 table.appendRow(['<b>External URL (robot)</b>', '', ''])
143 for r,_ in top_robots_referers[:10]:
144 row = [generateHTMLLink(r), robots_referers[r]['pages'], robots_referers[r]['hits']]
145 total_robot[1] -= robots_referers[r]['pages']
146 total_robot[2] -= robots_referers[r]['hits']
147 table.appendRow(row)
148 if total_robot[1] or total_robot[2]:
149 total_robot[0] = 'Others'
150 table.appendRow(total_robot)
151 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
152
153 index.appendBlock(table)
154
155 # All key phrases in a file
156 if self.create_all_key_phrases:
157 title = time.strftime('Key Phrases - %B %Y', cur_time)
158
159 filename = 'key_phrases.html'
160 path = self.iwla.getCurDisplayPath(filename)
161
162 total_search = [0]*2
163 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
164 table = display.createBlock(DisplayHTMLBlockTable, 'Top key phrases', ['Key phrase', 'Search'])
165 table.setColsCSSClass(['', 'iwla_search'])
166 new_list = self.max_key_phrases and top_key_phrases[:self.max_key_phrases] or top_key_phrases
167 for phrase in new_list:
168 table.appendRow([phrase[0], phrase[1]])
169 total_search[1] += phrase[1]
170 page.appendBlock(table)
171
172 display.addPage(page)
173
174 title = 'Top key phrases'
175 if self.create_all_key_phrases:
176 link = '<a href=\'%s\'>All key phrases</a>' % (filename)
177 title = '%s - %s' % (title, link)
178
179 # Top key phrases in index
180 table = display.createBlock(DisplayHTMLBlockTable, title, ['Key phrase', 'Search'])
181 table.setColsCSSClass(['', 'iwla_search'])
182 for phrase in top_key_phrases[:10]:
183 table.appendRow([phrase[0], phrase[1]])
184 total_search[1] -= phrase[1]
185 if total_search[1]:
186 total_search[0] = 'Others'
187 table.appendRow(total_search)
188 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
189 index.appendBlock(table)

Archive Download this file

Branches

Tags