# -*- coding: utf-8 -*- # # Copyright Grégory Soutadé 2016 # 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 . # 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): def __init__(self, iwla): super(IWLADisplayIPToGeo, self).__init__(iwla) self.API_VERSION = 1 self.requires = ['IWLAPostAnalysisIPToGeo'] 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 @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(): return None visitor = self.visitors[remote_ip] cc = visitor.get('country_code', None) if not cc: return None icon = '%s flag' % (cc, self.icon_path, cc) return '%s %s' % (icon, host) def hook(self): display = self.iwla.getDisplay() geo = self.iwla.getMonthStats().get('geo', {}) geo = sorted(geo.items(), key=lambda t: t[1], reverse=True) self.visitors = self.iwla.getCurrentVisits() # 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 = '%s flag' % (cc, self.icon_path, cc) 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 = '%s' % (filename, self.iwla._(u'All countries')) 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 = '%s flag' % (cc, self.icon_path, cc) table.appendRow([icon, cc, visitors]) table.computeRatio(2) index.appendBlock(table)