iwla/plugins/display/ip_to_geo.py

114 lines
3.7 KiB
Python
Raw Permalink Normal View History

2016-02-06 14:41:41 +01:00
# -*- coding: utf-8 -*-
#
2016-02-06 14:47:47 +01:00
# Copyright Grégory Soutadé 2016
2016-02-06 14:41:41 +01:00
# 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
from display import *
"""
Display hook
Add geo statistics
Plugin requirements :
post_analysis/ip_to_geo
Conf values needed :
create_geo_page*
Output files :
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayIPToGeo(IPlugin):
2016-02-06 14:41:41 +01:00
def __init__(self, iwla):
super(IWLADisplayIPToGeo, self).__init__(iwla)
2016-02-06 14:41:41 +01:00
self.API_VERSION = 1
self.requires = ['IWLAPostAnalysisIPToGeo']
2016-02-06 14:41:41 +01:00
def load(self):
self.icon_path = self.iwla.getConfValue('icon_path', '/')
self.create_geo_page = self.iwla.getConfValue('create_geo_page_page', True)
display = self.iwla.getDisplay()
display.addColumnFilter(self.iwla._(u'Host'), self.FlagFilter, {'self':self})
return True
2016-04-12 08:17:34 +02:00
@staticmethod # Needed to have unbound method
def FlagFilter(host, remote_ip, self):
if remote_ip is None or not remote_ip in self.visitors.keys():
2023-04-28 16:17:47 +02:00
return None
visitor = self.visitors[remote_ip]
2023-04-28 16:17:47 +02:00
cc = visitor.get('country_code', None)
if not cc: return None
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
2023-04-28 16:17:47 +02:00
return '%s %s' % (icon, host)
2016-02-06 14:41:41 +01:00
def hook(self):
display = self.iwla.getDisplay()
geo = self.iwla.getMonthStats().get('geo', {})
2016-02-06 14:41:41 +01:00
geo = sorted(geo.items(), key=lambda t: t[1], reverse=True)
self.visitors = self.iwla.getCurrentVisits()
2016-02-06 14:41:41 +01:00
# All in a page
if self.create_geo_page:
title = createCurTitle(self.iwla, u'All Coutries')
filename = 'geo.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Countries'), ['', self.iwla._(u'Country'), self.iwla._(u'Visitors')])
table.setColsCSSClass(['', '', 'iwla_hit'])
for (cc, visitors) in geo:
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
2016-02-06 14:41:41 +01:00
table.appendRow([icon, cc, visitors])
table.computeRatio(2)
page.appendBlock(table)
display.addPage(page)
# Countries in index
title = self.iwla._(u'Countries')
if self.create_geo_page:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All countries'))
2016-02-06 14:41:41 +01:00
title = '%s - %s' % (title, link)
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Countries'), self.iwla._(u'Visitors')])
table.setColsCSSClass(['', '', 'iwla_hit'])
for (cc, visitors) in geo[:10]:
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
2016-02-06 14:41:41 +01:00
table.appendRow([icon, cc, visitors])
table.computeRatio(2)
index.appendBlock(table)