iwla

iwla Commit Details

Date:2015-01-08 21:01:33 (6 years 6 months ago)
Author:Grégory Soutadé
Branch:dev, master
Commit:2baf4173fdfe044319252a618aa47dfda6cb3159
Parents: a40f116c71df4135730f7fa9665c708a91bab30f
Message:Add Browsers post analysis and display

Changes:
Aplugins/display/browsers.py (full)
Aplugins/post_analysis/browsers.py (full)

File differences

plugins/display/browsers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
from iwla import IWLA
from iplugin import IPlugin
from display import *
import awstats_data
"""
Display hook
Create browsers page
Plugin requirements :
post_analysis/browsers
Conf values needed :
max_browsers_displayed*
create_browsers_page*
Output files :
OUTPUT_ROOT/year/month/browsers.html
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayBrowsers(IPlugin):
def __init__(self, iwla):
super(IWLADisplayBrowsers, self).__init__(iwla)
self.API_VERSION = 1
self.requires = ['IWLAPostAnalysisBrowsers']
def load(self):
self.icon_path = self.iwla.getConfValue('icon_path', '/')
self.max_browsers = self.iwla.getConfValue('max_browsers_displayed', 0)
self.create_browsers = self.iwla.getConfValue('create_browsers_page', True)
self.icon_names = {v:k for (k, v) in awstats_data.browsers_hashid.items()}
return True
def hook(self):
display = self.iwla.getDisplay()
browsers = self.iwla.getMonthStats()['browsers']
browsers = sorted(browsers.items(), key=lambda t: t[1], reverse=True)
# All in a file
if self.create_browsers:
title = createCurTitle(self.iwla, u'Browsers')
filename = 'browsers.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Browsers'), ['', self.iwla._(u'Browser'), self.iwla._(u'Entrance')])
table.setColsCSSClass(['', '', 'iwla_hit'])
total_browsers = [0]*3
new_list = self.max_browsers and browsers[:self.max_browsers] or browsers
for (browser, entrance) in new_list:
if browser != 'unknown':
icon = '<img src="/%s/browser/%s.png"/>' % (self.icon_path, awstats_data.browsers_icons[self.icon_names[browser]])
else:
icon = '<img src="/%s/browser/unknown.png"/>' % (self.icon_path)
browser = 'Unknown'
table.appendRow([icon, browser, entrance])
total_browsers[1] += entrance
if self.max_browsers:
others = 0
for (browser, entrance) in browsers[self.max_browsers:]:
others += entrance
table.appendRow(['', self.iwla._(u'Others'), others])
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
page.appendBlock(table)
display.addPage(page)
title = 'Top Browsers'
if self.create_browsers:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All Browsers'))
title = '%s - %s' % (title, link)
# Top in index
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Browser'), self.iwla._(u'Entrance')])
table.setColsCSSClass(['', '', 'iwla_hit'])
for (browser, entrance) in browsers[:10]:
if browser != 'unknown':
icon = '<img src="/%s/browser/%s.png"/>' % (self.icon_path, awstats_data.browsers_icons[self.icon_names[browser]])
else:
icon = '<img src="/%s/browser/unknown.png"/>' % (self.icon_path)
browser = 'Unknown'
table.appendRow([icon, browser, entrance])
total_browsers[1] -= entrance
if total_browsers[1]:
total_browsers[0] = self.iwla._(u'Others')
table.appendRow(total_browsers)
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
index.appendBlock(table)
plugins/post_analysis/browsers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
import re
from iwla import IWLA
from iplugin import IPlugin
import awstats_data
"""
Post analysis hook
Detect browser information from requests
Plugin requirements :
None
Conf values needed :
None
Output files :
None
Statistics creation :
visits :
remote_addr =>
browser
month_stats :
browsers =>
browser => count
Statistics update :
None
Statistics deletion :
None
"""
class IWLAPostAnalysisBrowsers(IPlugin):
def __init__(self, iwla):
super(IWLAPostAnalysisBrowsers, self).__init__(iwla)
self.API_VERSION = 1
def load(self):
self.browsers = []
for hashid in awstats_data.browsers:
hashid_re = re.compile(r'.*%s.*' % (hashid), re.IGNORECASE)
if hashid in awstats_data.browsers_hashid.keys():
self.browsers.append((hashid_re, awstats_data.browsers_hashid[hashid]))
return True
def hook(self):
stats = self.iwla.getValidVisitors()
month_stats = self.iwla.getMonthStats()
browsers = month_stats.get('browsers', {})
browsers_stats = {}
for (k, super_hit) in stats.items():
if not 'browser' in super_hit:
for r in super_hit['requests'][::-1]:
user_agent = r['http_user_agent']
if not user_agent: continue
browser_name = 'unknown'
for (hashid_re, browser) in self.browsers:
if hashid_re.match(user_agent):
browser_name = browser
break
super_hit['browser'] = browser_name
break
else:
browser_name = super_hit['browser']
if not browser_name in browsers_stats.keys():
browsers_stats[browser_name] = 1
else:
browsers_stats[browser_name] += 1
month_stats['browsers'] = browsers_stats

Archive Download the corresponding diff file

Branches

Tags