iwla

iwla Git Source Tree

Root/plugins/display/ip_to_geo.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2016
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
21import re
22
23from iwla import IWLA
24from iplugin import IPlugin
25from display import *
26
27import awstats_data
28
29"""
30Display hook
31
32Add geo statistics
33
34Plugin requirements :
35 post_analysis/ip_to_geo
36
37Conf values needed :
38 create_geo_page*
39
40Output files :
41 OUTPUT_ROOT/year/month/index.html
42
43Statistics creation :
44 None
45
46Statistics update :
47 None
48
49Statistics deletion :
50 None
51"""
52
53class IWLADisplayTopGeo(IPlugin):
54 def __init__(self, iwla):
55 super(IWLADisplayTopGeo, self).__init__(iwla)
56 self.API_VERSION = 1
57 self.requires = ['IWLAPostAnalysisIPToGeo']
58
59 def load(self):
60 self.icon_path = self.iwla.getConfValue('icon_path', '/')
61 self.create_geo_page = self.iwla.getConfValue('create_geo_page_page', True)
62
63 display = self.iwla.getDisplay()
64 display.addColumnFilter(self.iwla._(u'Host'), self.FlagFilter, {'self':self})
65
66 return True
67
68 @staticmethod # Needed to have unbound methd
69 def FlagFilter(host, self):
70 cc = None
71 host_name = host.split(' ')[0] # hostname or ip
72 if host_name in self.valid_visitors.keys():
73 cc = self.valid_visitors[host_name]['country_code']
74 else:
75 for visitor in self.valid_visitors.values():
76 if visitor['remote_addr'] == host_name:
77 cc = visitor['country_code']
78 break
79 if not cc or cc == 'ip': return None
80 icon = '<img src="/%s/flags/%s.png"/>' % (self.icon_path, cc)
81 return '%s %s' % (icon ,host)
82
83 def hook(self):
84 display = self.iwla.getDisplay()
85 geo = self.iwla.getMonthStats()['geo']
86 geo = sorted(geo.items(), key=lambda t: t[1], reverse=True)
87 self.valid_visitors = self.iwla.getValidVisitors()
88
89 # All in a page
90 if self.create_geo_page:
91 title = createCurTitle(self.iwla, u'All Coutries')
92 filename = 'geo.html'
93 path = self.iwla.getCurDisplayPath(filename)
94
95 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
96 table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Countries'), ['', self.iwla._(u'Country'), self.iwla._(u'Visitors')])
97 table.setColsCSSClass(['', '', 'iwla_hit'])
98 for (cc, visitors) in geo:
99 icon = '<img src="/%s/flags/%s.png"/>' % (self.icon_path, cc)
100 table.appendRow([icon, cc, visitors])
101 table.computeRatio(2)
102 page.appendBlock(table)
103
104 display.addPage(page)
105
106 # Countries in index
107 title = self.iwla._(u'Countries')
108 if self.create_geo_page:
109 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'Details'))
110 title = '%s - %s' % (title, link)
111
112 index = self.iwla.getDisplayIndex()
113
114 table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Countries'), self.iwla._(u'Visitors')])
115 table.setColsCSSClass(['', '', 'iwla_hit'])
116 for (cc, visitors) in geo[:10]:
117 icon = '<img src="/%s/flags/%s.png"/>' % (self.icon_path, cc)
118 table.appendRow([icon, cc, visitors])
119 table.computeRatio(2)
120 index.appendBlock(table)

Archive Download this file

Branches

Tags